-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
32 lines (28 loc) · 923 Bytes
/
middleware.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
package mojito_extension_tracing
import (
"context"
"github.com/go-mojito/mojito"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
func Middleware(ctx mojito.Context, next func() error) error {
contextCtx, span := otel.Tracer(serviceName).Start(context.Background(), "Handle HTTP Request")
span.SetAttributes(
attribute.String("host", ctx.Request().GetRequest().Host),
attribute.String("method", ctx.Request().GetRequest().Method),
attribute.String("uri", ctx.Request().GetRequest().RequestURI),
attribute.String("user-agent", ctx.Request().GetRequest().UserAgent()),
)
defer span.End()
_ = ctx.Metadata().Set(METADATA_NAME, contextCtx)
span.AddEvent("Execute next() Function")
err := next()
span.AddEvent("next() Function complete")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return err
}
return nil
}