-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (58 loc) · 1.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/MrAlias/otel-otlp-metric-example/setup"
"go.opentelemetry.io/otel/metric"
)
const (
// instName needs to be the name of the package providing instrumentation.
instName = "go.opentelemetry.io/otel/example/otlp"
instVer = "v0.1.1"
)
type App struct {
MeterProvider metric.MeterProvider
reqDuration metric.Int64Histogram
}
func NewApp(mp metric.MeterProvider) (*App, error) {
app := &App{MeterProvider: mp}
meter := mp.Meter(instName, metric.WithInstrumentationVersion(instVer))
var err error
app.reqDuration, err = meter.Int64Histogram(
"request.duration",
metric.WithDescription("Time taken to perfrom a user request"),
metric.WithUnit("ms"),
)
return app, err
}
func (a *App) Run(addr string) {
log.Printf("serving metrics at %s/", addr)
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
defer func(start time.Time) {
d := time.Since(start).Milliseconds()
a.reqDuration.Record(req.Context(), d)
}(time.Now())
w.WriteHeader(http.StatusOK)
})
if err := http.ListenAndServe(addr, nil); err != nil {
log.Println(err)
}
}
func main() {
ctx := context.Background()
meterProvider, err := setup.NewMeterProvider(ctx)
if err != nil {
log.Fatalln(err)
}
app, err := NewApp(meterProvider)
if err != nil {
log.Fatalln(err)
}
go app.Run(":8080")
ctx, _ = signal.NotifyContext(ctx, os.Interrupt)
<-ctx.Done()
}