/
endpoint.go
56 lines (49 loc) · 1.35 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
package main
import (
"context"
"errors"
"time"
"github.com/jan-xyz/box"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
)
var tracingMiddleware = func() 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 := otel.Tracer(service).Start(ctx, "Handle")
defer span.End()
return next(ctx, req)
}
}
}
var loggingMiddleware = func() 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)
logrus.WithContext(ctx).
WithError(errors.New("hello world")).
WithField("foo", "bar").
Error("something failed")
return resp, err
}
}
}
func Endpoint(ctx context.Context, _ string) (string, error) {
meter := otel.Meter("")
for i := 0; i < 10; i++ {
process(ctx, i)
}
if c, err := meter.Int64Counter("request_handled"); err == nil {
c.Add(ctx, 1)
}
return "", nil
}
func process(ctx context.Context, i int) {
ctx, span := otel.Tracer(service).Start(ctx, "proccessing")
defer span.End()
logrus.WithContext(ctx).
WithField("iteration", i).
Info("tick!")
<-time.After(3 * time.Millisecond)
}