|
| 1 | +// Copyright 2022 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +// A minimal example of how to include Prometheus instrumentation. |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | + "github.com/prometheus/client_golang/prometheus/collectors" |
| 25 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 26 | +) |
| 27 | + |
| 28 | +var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") |
| 29 | + |
| 30 | +func main() { |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + // Create a new registry. |
| 34 | + reg := prometheus.NewRegistry() |
| 35 | + |
| 36 | + // Add Go module build info. |
| 37 | + reg.MustRegister(collectors.NewBuildInfoCollector()) |
| 38 | + reg.MustRegister(collectors.NewGoCollector( |
| 39 | + collectors.WithGoCollections(collectors.GoRuntimeMemStatsCollection | collectors.GoRuntimeMetricsCollection), |
| 40 | + )) |
| 41 | + |
| 42 | + // Expose the registered metrics via HTTP. |
| 43 | + http.Handle("/metrics", promhttp.HandlerFor( |
| 44 | + reg, |
| 45 | + promhttp.HandlerOpts{ |
| 46 | + // Opt into OpenMetrics to support exemplars. |
| 47 | + EnableOpenMetrics: true, |
| 48 | + }, |
| 49 | + )) |
| 50 | + fmt.Println("Hello world from new Go Collector!") |
| 51 | + log.Fatal(http.ListenAndServe(*addr, nil)) |
| 52 | +} |
0 commit comments