()
| 29 | ) |
| 30 | |
| 31 | func main() { |
| 32 | requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 33 | Name: "http_request_duration_seconds", |
| 34 | Help: "A histogram of the HTTP request durations in seconds.", |
| 35 | Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5), |
| 36 | }) |
| 37 | |
| 38 | // Create non-global registry. |
| 39 | registry := prometheus.NewRegistry() |
| 40 | |
| 41 | // Add go runtime metrics and process collectors. |
| 42 | registry.MustRegister( |
| 43 | collectors.NewGoCollector(), |
| 44 | collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), |
| 45 | requestDurations, |
| 46 | ) |
| 47 | |
| 48 | go func() { |
| 49 | for { |
| 50 | // Record fictional latency. |
| 51 | now := time.Now() |
| 52 | requestDurations.(prometheus.ExemplarObserver).ObserveWithExemplar( |
| 53 | time.Since(now).Seconds(), prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))}, |
| 54 | ) |
| 55 | time.Sleep(600 * time.Millisecond) |
| 56 | } |
| 57 | }() |
| 58 | |
| 59 | // Expose /metrics HTTP endpoint using the created custom registry. |
| 60 | http.Handle( |
| 61 | "/metrics", promhttp.HandlerFor( |
| 62 | registry, |
| 63 | promhttp.HandlerOpts{ |
| 64 | EnableOpenMetrics: true, |
| 65 | }), |
| 66 | ) |
| 67 | // To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics |
| 68 | log.Fatalln(http.ListenAndServe(":8080", nil)) |
| 69 | } |
nothing calls this directly
no test coverage detected