|
| 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 simple example of how to add fixed custom labels to all metrics exported by the origin collector. |
| 15 | +// For more details, see the documentation: https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#WrapRegistererWith |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "log" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | + "github.com/prometheus/client_golang/prometheus/collectors" |
| 26 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 27 | +) |
| 28 | + |
| 29 | +var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") |
| 30 | + |
| 31 | +func main() { |
| 32 | + flag.Parse() |
| 33 | + |
| 34 | + // Create a new registry. |
| 35 | + reg := prometheus.NewRegistry() |
| 36 | + prometheus.WrapRegistererWith(prometheus.Labels{"serviceName": "my-service-name"}, reg).MustRegister( |
| 37 | + collectors.NewGoCollector(), |
| 38 | + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), |
| 39 | + ) |
| 40 | + |
| 41 | + // Expose the registered metrics via HTTP. |
| 42 | + http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) |
| 43 | + log.Fatal(http.ListenAndServe(*addr, nil)) |
| 44 | +} |
0 commit comments