-
Notifications
You must be signed in to change notification settings - Fork 38
/
context.go
50 lines (43 loc) · 1.19 KB
/
context.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 telemetry
import "context"
type telemetryKeyType int
const (
routePatternKey telemetryKeyType = iota
statusCodeKey
)
// ContextWithRoutePattern adds the route pattern to the context. See
// NewOtelHTTPMiddleware for usage
func ContextWithRoutePattern(ctx context.Context, routePattern *string) context.Context {
return context.WithValue(ctx, routePatternKey, routePattern)
}
// RoutePatternFromContext returns the route pattern if found, or the
// empty string
func RoutePatternFromContext(ctx context.Context) *string {
val := ctx.Value(routePatternKey)
if val == nil {
return nil
}
routePattern, ok := val.(*string)
if ok {
return routePattern
}
return nil
}
// ContextWithStatusCode adds the status code to the context. See
// NewOtelHTTPMiddleware for usage
func ContextWithStatusCode(ctx context.Context, statusCode *int) context.Context {
return context.WithValue(ctx, statusCodeKey, statusCode)
}
// StatusCodeFromContext returns the route pattern if found, or the
// empty string
func StatusCodeFromContext(ctx context.Context) *int {
val := ctx.Value(statusCodeKey)
if val == nil {
return nil
}
statusCode, ok := val.(*int)
if ok {
return statusCode
}
return nil
}