-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
otelhttp: Allow setting start time using context (#6137)
Fixes #5318 This allows overriding the start time for http server metrics and traces. See the linked issue for the motivation. --------- Co-authored-by: Robert Pająk <[email protected]>
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
23 changes: 23 additions & 0 deletions
23
instrumentation/net/http/otelhttp/start_time_context_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelhttp | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStartTimeFromContext(t *testing.T) { | ||
ctx := context.Background() | ||
startTime := StartTimeFromContext(ctx) | ||
assert.True(t, startTime.IsZero()) | ||
|
||
now := time.Now() | ||
ctx = ContextWithStartTime(ctx, now) | ||
startTime = StartTimeFromContext(ctx) | ||
assert.True(t, startTime.Equal(now)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters