MCPcopy
hub / github.com/prometheus/client_golang / main

Function main

examples/exemplars/main.go:31–69  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29)
30
31func 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}

Callers

nothing calls this directly

Calls 9

MustRegisterMethod · 0.95
NewHistogramFunction · 0.92
ExponentialBucketsFunction · 0.92
NewRegistryFunction · 0.92
NewGoCollectorFunction · 0.92
NewProcessCollectorFunction · 0.92
HandlerForFunction · 0.92
HandleMethod · 0.80
ObserveWithExemplarMethod · 0.65

Tested by

no test coverage detected