|
11 | 11 | // See the License for the specific language governing permissions and |
12 | 12 | // limitations under the License. |
13 | 13 |
|
14 | | -// A minimal example of how to include Prometheus instrumentation. |
| 14 | +// A minimal example of exposing Prometheus metrics from a Go application. |
15 | 15 | package main |
16 | 16 |
|
17 | 17 | import ( |
18 | 18 | "flag" |
19 | 19 | "log" |
20 | 20 | "net/http" |
21 | 21 |
|
22 | | - "github.com/prometheus/client_golang/prometheus/collectors" |
23 | | - |
24 | 22 | "github.com/prometheus/client_golang/prometheus" |
| 23 | + "github.com/prometheus/client_golang/prometheus/collectors" |
25 | 24 | "github.com/prometheus/client_golang/prometheus/promhttp" |
26 | 25 | ) |
27 | 26 |
|
| 27 | +// Address for exposing Prometheus metrics. |
28 | 28 | var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") |
29 | 29 |
|
30 | 30 | func main() { |
31 | 31 | flag.Parse() |
32 | 32 |
|
33 | | - // Create non-global registry. |
| 33 | + // Create a custom registry instead of using the global default registry. |
| 34 | + // This helps isolate application metrics from automatically registered metrics. |
34 | 35 | reg := prometheus.NewRegistry() |
35 | 36 |
|
36 | | - // Add go runtime metrics and process collectors. |
| 37 | + // Register Go runtime metrics (such as goroutine count |
| 38 | + // and GC statistics) and process-level metrics (such as |
| 39 | + // CPU and memory usage). |
37 | 40 | reg.MustRegister( |
38 | 41 | collectors.NewGoCollector(), |
39 | 42 | collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), |
40 | 43 | ) |
41 | 44 |
|
42 | | - // Expose /metrics HTTP endpoint using the created custom registry. |
| 45 | + // Expose the registered metrics at /metrics for Prometheus scraping. |
43 | 46 | http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg})) |
| 47 | + |
44 | 48 | log.Fatal(http.ListenAndServe(*addr, nil)) |
45 | 49 | } |
0 commit comments