-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
orchestrion: setup GLS-stored context
Signed-off-by: Eliott Bouhana <[email protected]>
- Loading branch information
1 parent
28d4288
commit 07c2865
Showing
11 changed files
with
107 additions
and
14 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
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
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,37 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
package orchestrion | ||
|
||
import "context" | ||
|
||
// CtxGLS returns the context stored in the compilation-injected field of the runtime.g struct by orchestrion | ||
func CtxGLS() context.Context { | ||
return getDDGLS().(context.Context) | ||
} | ||
|
||
// CtxOrGLS implements the logic to choose between a user given context.Context or the context.Context inserted in | ||
// the Goroutine Global Storage (GLS) by orchestrion. If Orchestrion is not enabled it's only returning the given context. | ||
func CtxOrGLS(ctx context.Context) context.Context { | ||
if !Enabled() || (ctx != nil && ctx != context.Background()) { | ||
return ctx | ||
} | ||
|
||
gls := CtxGLS() | ||
if gls != nil { | ||
return gls | ||
} | ||
|
||
ctx = context.Background() | ||
setDDGLS(ctx) | ||
return ctx | ||
} | ||
|
||
// CtxWithValue runs context.WithValue, adds the result to the GLS slot of orchestrion, and returns it. | ||
func CtxWithValue(ctx context.Context, key, val any) context.Context { | ||
ctx = context.WithValue(CtxOrGLS(ctx), key, val) | ||
setDDGLS(ctx) | ||
return ctx | ||
} |
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,33 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
package orchestrion | ||
|
||
import _ "unsafe" // for go:linkname | ||
|
||
var ( | ||
// getDDGLS returns the current value from the field inserted in runtime.g by orchestrion. | ||
// Or nil if orchestrion is not enabled. | ||
getDDGLS = func() any { return nil } | ||
// setDDGLS sets the value in the field inserted in runtime.g by orchestrion. | ||
// Or does nothing if orchestrion is not enabled. | ||
setDDGLS = func(any) {} | ||
) | ||
|
||
//go:linkname _DdOrchestrionGlsGet __dd_orchestrion_gls_get | ||
var _DdOrchestrionGlsGet func() any | ||
|
||
//go:linkname _DdOrchestrionGlsSet __dd_orchestrion_gls_set | ||
var _DdOrchestrionGlsSet func(any) | ||
|
||
// Check at Go init time that the two function variable values created by the | ||
// orchestrion are present, and set the get/set variables to their | ||
// values. | ||
func init() { | ||
if _DdOrchestrionGlsGet != nil && _DdOrchestrionGlsSet != nil { | ||
getDDGLS = _DdOrchestrionGlsGet | ||
setDDGLS = _DdOrchestrionGlsSet | ||
} | ||
} |
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,12 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
package orchestrion | ||
|
||
// Enabled returns whether the current build was compiled with orchestrion or not. | ||
// This function is modified at compile time by orchestrion to be true if orchestrion is used. | ||
func Enabled() bool { | ||
return false | ||
} |
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