Debian Codegraph @

examples/middleware/main.go

raw
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 // Simple example of auto-instrumentation by using an HTTP Middleware with relevant metrics.
15
16 package main
17
18 import (
19 "log"
20 "net/http"
21
22 "github.com/prometheus/client_golang/examples/middleware/httpmiddleware"
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 func main() {
29 // Create non-global registry.
30 registry := prometheus.NewRegistry()
31
32 // Add go runtime metrics and process collectors.
33 registry.MustRegister(
34 collectors.NewGoCollector(),
35 collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
36 )
37
38 // Expose /metrics HTTP endpoint using the created custom registry.
39 http.Handle(
40 "/metrics",
41 httpmiddleware.New(
42 registry, nil).
43 WrapHandler("/metrics", promhttp.HandlerFor(
44 registry,
45 promhttp.HandlerOpts{}),
46 ))
47
48 log.Fatalln(http.ListenAndServe(":8080", nil))
49 }