-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
93 lines (81 loc) · 2.16 KB
/
endpoint.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"errors"
"log/slog"
"time"
"github.com/jan-xyz/box"
"go.opentelemetry.io/contrib/bridges/otelslog"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)
var (
logger = otelslog.NewLogger(service)
tracer = otel.Tracer(service)
meter = otel.Meter(service)
)
var (
requestCounter metric.Int64Counter
errorCounter metric.Int64Counter
)
func init() {
var err error
requestCounter, err = meter.Int64Counter("request.counter",
metric.WithDescription("The number of requests"),
metric.WithUnit("{request}"))
if err != nil {
panic(err)
}
errorCounter, err = meter.Int64Counter("error.counter",
metric.WithDescription("The number of errors"),
metric.WithUnit("{error}"))
if err != nil {
panic(err)
}
}
func metricMiddleware() box.Middleware[string, string] {
return func(next box.Endpoint[string, string]) box.Endpoint[string, string] {
return func(ctx context.Context, req string) (string, error) {
requestCounter.Add(ctx, 1)
out, err := next(ctx, req)
if err != nil {
errorCounter.Add(ctx, 1)
}
return out, err
}
}
}
func tracingMiddleware() box.Middleware[string, string] {
return func(next box.Endpoint[string, string]) box.Endpoint[string, string] {
return func(ctx context.Context, req string) (string, error) {
ctx = context.WithValue(ctx, &reqID{}, req)
ctx, span := tracer.Start(ctx, "Handle")
defer span.End()
return next(ctx, req)
}
}
}
func loggingMiddleware() box.Middleware[string, string] {
return func(next box.Endpoint[string, string]) box.Endpoint[string, string] {
return func(ctx context.Context, req string) (string, error) {
resp, err := next(ctx, req)
logger.ErrorContext(ctx,
"something failed",
slog.String("error", errors.New("hello world").Error()),
slog.String("foo", "bar"))
return resp, err
}
}
}
func Endpoint(ctx context.Context, _ string) (string, error) {
for i := 0; i < 10; i++ {
process(ctx, i)
}
return "", nil
}
func process(ctx context.Context, i int) {
ctx, span := tracer.Start(ctx, "proccessing")
defer span.End()
logger.InfoContext(ctx, "tick!", slog.Int("iteration", i))
<-time.After(3 * time.Millisecond)
}