-
Notifications
You must be signed in to change notification settings - Fork 581
/
start_time_context.go
29 lines (23 loc) · 1.04 KB
/
start_time_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
import (
"context"
"time"
)
type startTimeContextKeyType int
const startTimeContextKey startTimeContextKeyType = 0
// ContextWithStartTime returns a new context with the provided start time. The
// start time will be used for metrics and traces emitted by the
// instrumentation. Only one labeller can be injected into the context.
// Injecting it multiple times will override the previous calls.
func ContextWithStartTime(parent context.Context, start time.Time) context.Context {
return context.WithValue(parent, startTimeContextKey, start)
}
// StartTimeFromContext retrieves a time.Time from the provided context if one
// is available. If no start time was found in the provided context, a new,
// zero start time is returned and the second return value is false.
func StartTimeFromContext(ctx context.Context) time.Time {
t, _ := ctx.Value(startTimeContextKey).(time.Time)
return t
}