-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose query in the context of the span formatter (#27)
- Loading branch information
Showing
15 changed files
with
993 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package otelsql | ||
|
||
import "context" | ||
|
||
type queryCtxKey struct{} | ||
|
||
// QueryFromContext gets the query from context. | ||
func QueryFromContext(ctx context.Context) string { | ||
query, ok := ctx.Value(queryCtxKey{}).(string) | ||
if !ok { | ||
return "" | ||
} | ||
|
||
return query | ||
} | ||
|
||
// ContextWithQuery attaches the query to the parent context. | ||
func ContextWithQuery(ctx context.Context, query string) context.Context { | ||
return context.WithValue(ctx, queryCtxKey{}, query) | ||
} |
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 @@ | ||
package otelsql_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/nhatthm/otelsql" | ||
) | ||
|
||
func TestQueryContext(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := otelsql.QueryFromContext(context.Background()) | ||
assert.Empty(t, actual) | ||
|
||
ctx := otelsql.ContextWithQuery(context.Background(), "SELECT 1") | ||
actual = otelsql.QueryFromContext(ctx) | ||
expected := "SELECT 1" | ||
|
||
assert.Equal(t, expected, actual) | ||
} |
Oops, something went wrong.