Skip to content

Commit 07b1397

Browse files
authored
examples: Add exemplars and middleware examples (#1173)
* Add exemplars simple example, add go runtime and process collectors to simple example Signed-off-by: Jéssica Lins <[email protected]> * Add middleware example Signed-off-by: Jéssica Lins <[email protected]> * Lint Signed-off-by: Jéssica Lins <[email protected]> Signed-off-by: Jéssica Lins <[email protected]>
1 parent 8b6e680 commit 07b1397

File tree

6 files changed

+723
-0
lines changed

6 files changed

+723
-0
lines changed

examples/exemplars/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 record a latency metric with exemplars, using a fictional id
15+
// as a prometheus label.
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"math/rand"
23+
"net/http"
24+
"time"
25+
26+
"github.com/prometheus/client_golang/prometheus"
27+
"github.com/prometheus/client_golang/prometheus/collectors"
28+
"github.com/prometheus/client_golang/prometheus/promhttp"
29+
)
30+
31+
func main() {
32+
requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
33+
Name: "http_request_duration_seconds",
34+
Help: "A histogram of the HTTP request durations in seconds.",
35+
Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
36+
})
37+
38+
// Create non-global registry.
39+
registry := prometheus.NewRegistry()
40+
41+
// Add go runtime metrics and process collectors.
42+
registry.MustRegister(
43+
collectors.NewGoCollector(),
44+
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
45+
requestDurations,
46+
)
47+
48+
go func() {
49+
for {
50+
// Record fictional latency.
51+
now := time.Now()
52+
requestDurations.(prometheus.ExemplarObserver).ObserveWithExemplar(
53+
time.Since(now).Seconds(), prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
54+
)
55+
time.Sleep(600 * time.Millisecond)
56+
}
57+
}()
58+
59+
// Expose /metrics HTTP endpoint using the created custom registry.
60+
http.Handle(
61+
"/metrics", promhttp.HandlerFor(
62+
registry,
63+
promhttp.HandlerOpts{
64+
EnableOpenMetrics: true,
65+
}),
66+
)
67+
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics
68+
log.Fatalln(http.ListenAndServe(":8080", nil))
69+
}

examples/middleware/go.mod

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/jessicalins/instrumentation-practices-examples/middleware
2+
3+
go 1.17
4+
5+
require github.com/prometheus/client_golang v1.13.1
6+
7+
require (
8+
github.com/beorn7/perks v1.0.1 // indirect
9+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
10+
github.com/golang/protobuf v1.5.2 // indirect
11+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
12+
github.com/prometheus/client_model v0.2.0 // indirect
13+
github.com/prometheus/common v0.37.0 // indirect
14+
github.com/prometheus/procfs v0.8.0 // indirect
15+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
16+
google.golang.org/protobuf v1.28.1 // indirect
17+
)

0 commit comments

Comments
 (0)