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

Function runMain

tutorials/whatsup/reference/main.go:57–159  ·  view source on GitHub ↗
(opts internal.Config)

Source from the content-addressed store, hash-verified

55}
56
57func runMain(opts internal.Config) (err error) {
58 // Create tracer, so the application can be instrumented with traces.
59 var exporter tracing.ExporterBuilder
60 switch opts.TraceEndpoint {
61 case "stdout":
62 exporter = tracing.NewWriterExporter(os.Stdout)
63 default:
64 exporter = otlp.Exporter(opts.TraceEndpoint, otlp.WithInsecure())
65 }
66 tracer, closeFn, err := tracing.NewTracer(
67 exporter,
68 tracing.WithSampler(tracing.TraceIDRatioBasedSampler(opts.TraceSamplingRatio)),
69 tracing.WithServiceName("client_golang-tutorial:whatsup"),
70 )
71 if err != nil {
72 return err
73 }
74 defer errcapture.Do(&err, closeFn, "close tracers")
75
76 // Create registry for Prometheus metrics.
77 reg := prometheus.NewRegistry()
78 reg.MustRegister(
79 collectors.NewGoCollector(), // Metrics from Go runtime.
80 collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), // Metrics about the current UNIX process.
81 )
82
83 handled := promauto.With(reg).NewCounter(prometheus.CounterOpts{
84 Name: "whatsup_queries_handled_total",
85 Help: "Number of queries handed.",
86 })
87 lastNumElems := promauto.With(reg).NewGauge(prometheus.GaugeOpts{
88 Name: "whatsup_last_response_elements",
89 Help: "Number of elements in response for the last call.",
90 })
91 _ = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{
92 Name: "build_info",
93 Help: "Build information.",
94 ConstLabels: map[string]string{
95 "version": "vYOLO",
96 "language": "Go 1.20",
97 "owner": "@me",
98 },
99 }, func() float64 {
100 return 1
101 })
102 handledDuration := promauto.With(reg).NewHistogram(
103 prometheus.HistogramOpts{
104 Name: "whatsup_queries_duration_seconds",
105 Help: "Tracks the latencies for calls.",
106 Buckets: []float64{0.1, 0.3, 0.6, 1, 3, 6, 9, 20},
107 },
108 )
109
110 m := http.NewServeMux()
111 // Create HTTP handler for Prometheus metrics.
112 m.Handle("/metrics", promhttp.HandlerFor(
113 reg,
114 promhttp.HandlerOpts{

Callers 1

mainFunction · 0.70

Calls 15

MustRegisterMethod · 0.95
NewRegistryFunction · 0.92
NewGoCollectorFunction · 0.92
NewProcessCollectorFunction · 0.92
WithFunction · 0.92
HandlerForFunction · 0.92
NewClientFunction · 0.92
NewAPIFunction · 0.92
NewCounterMethod · 0.80
NewGaugeMethod · 0.80
NewGaugeFuncMethod · 0.80
NewHistogramMethod · 0.80

Tested by

no test coverage detected