| 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 |
"time" |
| 24 |
|
| 25 |
"github.com/prometheus/client_golang/prometheus" |
| 26 |
"github.com/prometheus/client_golang/prometheus/collectors" |
| 27 |
"github.com/prometheus/client_golang/prometheus/promauto" |
| 28 |
"github.com/prometheus/client_golang/prometheus/promhttp" |
| 29 |
) |
| 30 |
|
| 31 |
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") |
| 32 |
|
| 33 |
func main() { |
| 34 |
flag.Parse() |
| 35 |
|
| 36 |
// Create a new registry. |
| 37 |
reg := prometheus.NewRegistry() |
| 38 |
reg.MustRegister( |
| 39 |
collectors.NewGoCollector(), |
| 40 |
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), |
| 41 |
) |
| 42 |
|
| 43 |
// We should see the following metrics with an extra source label. But |
| 44 |
// other collectors registered above are expected not to have the extra |
| 45 |
// label. |
| 46 |
// See also https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels |
| 47 |
startFireKeeper(prometheus.WrapRegistererWith(prometheus.Labels{"component": "FireKeeper"}, reg)) |
| 48 |
startSparkForge(prometheus.WrapRegistererWith(prometheus.Labels{"component": "SparkForge"}, reg)) |
| 49 |
|
| 50 |
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) |
| 51 |
log.Fatal(http.ListenAndServe(*addr, nil)) |
| 52 |
} |
| 53 |
|
| 54 |
func startFireKeeper(reg prometheus.Registerer) { |
| 55 |
firesMaintained := promauto.With(reg).NewCounter(prometheus.CounterOpts{ |
| 56 |
Name: "fires_maintained_total", |
| 57 |
Help: "Total number of fires maintained", |
| 58 |
}) |
| 59 |
|
| 60 |
sparksDistributed := promauto.With(reg).NewCounter(prometheus.CounterOpts{ |
| 61 |
Name: "sparks_distributed_total", |
| 62 |
Help: "Total number of sparks distributed", |
| 63 |
}) |
| 64 |
|
| 65 |
go func() { |
| 66 |
for { |
| 67 |
time.Sleep(5 * time.Second) |
| 68 |
firesMaintained.Inc() |
| 69 |
log.Println("FireKeeper maintained a fire") |
| 70 |
} |
| 71 |
}() |
| 72 |
|
| 73 |
go func() { |
| 74 |
for { |
| 75 |
time.Sleep(7 * time.Second) |
| 76 |
sparksDistributed.Inc() |
| 77 |
log.Println("FireKeeper distributed a spark") |
| 78 |
} |
| 79 |
}() |
| 80 |
} |
| 81 |
|
| 82 |
func startSparkForge(reg prometheus.Registerer) { |
| 83 |
itemsForged := promauto.With(reg).NewCounter(prometheus.CounterOpts{ |
| 84 |
Name: "items_forged_total", |
| 85 |
Help: "Total number of items forged", |
| 86 |
}) |
| 87 |
|
| 88 |
go func() { |
| 89 |
for { |
| 90 |
time.Sleep(6 * time.Second) |
| 91 |
itemsForged.Inc() |
| 92 |
log.Println("SparkForge forged an item") |
| 93 |
} |
| 94 |
}() |
| 95 |
} |