()
| 25 | ) |
| 26 | |
| 27 | func main() { |
| 28 | requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 29 | Name: "http_request_duration_seconds", |
| 30 | Help: "A histogram of the HTTP request durations in seconds.", |
| 31 | Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5), |
| 32 | }) |
| 33 | |
| 34 | // Create non-global registry. |
| 35 | registry := prometheus.NewRegistry() |
| 36 | registry.MustRegister( |
| 37 | requestDurations, |
| 38 | ) |
| 39 | |
| 40 | go func() { |
| 41 | for { |
| 42 | // Record fictional latency. |
| 43 | now := time.Now() |
| 44 | requestDurations.Observe(time.Since(now).Seconds()) |
| 45 | time.Sleep(600 * time.Millisecond) |
| 46 | } |
| 47 | }() |
| 48 | |
| 49 | // Expose /metrics HTTP endpoint using the created custom registry. |
| 50 | http.Handle( |
| 51 | "/metrics", promhttp.HandlerFor( |
| 52 | registry, |
| 53 | promhttp.HandlerOpts{ |
| 54 | EnableOpenMetrics: true, |
| 55 | EnableOpenMetricsTextCreatedSamples: true, |
| 56 | }), |
| 57 | ) |
| 58 | // To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics |
| 59 | log.Fatalln(http.ListenAndServe(":8080", nil)) |
| 60 | } |
nothing calls this directly
no test coverage detected