-
Notifications
You must be signed in to change notification settings - Fork 5
/
gokit.go
50 lines (40 loc) · 1.38 KB
/
gokit.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
package trace
import (
"context"
"github.com/arquivei/foundationkit/request"
"github.com/go-kit/kit/endpoint"
"go.opentelemetry.io/otel/attribute"
)
// EndpointMiddleware returns a new gokit endpoint.Middleware that wraps
// the next Endpoint with a span with the given name. The RequestID, if
// present, is injected as a tag and if the next Endpoint returns an
// error, it is registered in the span.
func EndpointMiddleware(name string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (resp interface{}, err error) {
ctx, span := Start(ctx, name)
requestID := request.GetIDFromContext(ctx)
if !requestID.IsEmpty() {
span.SetAttributes(attribute.String("request.id", requestID.String()))
}
if ta, ok := req.(TraceAttributer); ok {
span.SetAttributes(ta.TraceAttributes()...)
}
resp, err = next(ctx, req)
if err != nil {
span.RecordError(err)
}
if ta, ok := resp.(TraceAttributer); ok {
span.SetAttributes(ta.TraceAttributes()...)
}
span.End()
return resp, err
}
}
}
// TraceAttributer is a interface for returning OpenTelemetry span attributes.
// If the Request or Response (or both) implement this interface the EndpointMiddleware
// will set theses attributes on the span.
type TraceAttributer interface {
TraceAttributes() []attribute.KeyValue
}