-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[reflect] sdk/log/xlog: Add FilterProcessor and EnabledParameters #6286
Closed
Closed
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
96a79ee
mv sdk/log/internal/x sdk/xlog
pellared 6282e71
Introduce xlog module with FilterProcessor and EnabledParameters
pellared 69ff66d
Update changelog
pellared bf61d44
Add trace context fields
pellared 5b3c8ca
Test FilterProcessor functionality
pellared f24fc64
Update changelog
pellared fffc077
go mod tidy
pellared 55adcc4
Merge branch 'main' into sdk/xlogs
pellared 008cf4d
Merge branch 'main' into sdk/xlogs
pellared 4df6669
Remove span context fields from EnabledParameters
pellared d31f329
Simplify doc comments
pellared fe5ae63
go mod tidy
pellared 4e54827
Move sdk/xlog to sdk/log/xlog
pellared abe7833
Fix hyperlink
pellared d5817c6
Update README.md
pellared c23714b
Merge branch 'main' into sdk/xlogs
pellared cb91d27
Merge branch 'main' into sdk/xlogs
pellared 71376d1
Update sdk/log/example_test.go
pellared e6b34ae
Update sdk/log/logger.go
pellared 4ff41e1
Format comment
pellared 628f916
Add missing period
pellared 98d6811
Do not release xlog
pellared 5cf1eaf
Improve Go doc comments
pellared 9037771
sdk/log to use internal copy of interface instead of xlog
pellared 3a07761
Cleanup go mod
pellared c56e9da
code snippet: TestReflectFilterProcessorPlayground
pellared 91d84a9
Support xlog with duck typing using reflect
pellared 685a417
Remove TestReflectFilterProcessorPlayground
pellared 0547924
Fix typos
pellared 7075931
Update CHANGELOG.md
pellared 648e21f
Update CHANGELOG.md
pellared 2941fb3
Merge branch 'main' into xlog-not-used-by-sdk
pellared 2a93c9f
Merge branch 'main' into xlog-not-used-by-sdk
pellared 9da340a
Apply suggestions from code review
pellared d55948e
Merge branch 'main' into xlog-not-used-by-sdk
pellared 08ac146
Remove usage of xlog in SDK
pellared 9475ed8
Merge branch 'main' into xlog-not-used-by-sdk
pellared 25937e0
Add otel-experimental depguard rule
pellared e4d1ebf
Merge branch 'main' into xlog-not-used-by-sdk
pellared 2ac7f71
Merge branch 'main' into xlog-not-used-by-sdk
pellared File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,88 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package log // import "go.opentelemetry.io/otel/sdk/log" | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"go.opentelemetry.io/otel/log" | ||
"go.opentelemetry.io/otel/sdk/instrumentation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
// filterProcessor uses reflect to support [go.opentelemetry.io/otel/sdk/log/xlog.FilterProcessor] | ||
// via duck typing. | ||
type filterProcessor struct { | ||
enabledFn reflect.Value | ||
paramType reflect.Type | ||
} | ||
|
||
func asFilterProccessor(p any) (filterProcessor, bool) { | ||
fp := reflect.ValueOf(p) | ||
m := fp.MethodByName("Enabled") | ||
if m == (reflect.Value{}) { | ||
// No Enabled method. | ||
return filterProcessor{}, false | ||
} | ||
mty := m.Type() | ||
if mty.NumOut() != 1 { | ||
// Should return one output parameter. | ||
return filterProcessor{}, false | ||
} | ||
if reflect.Bool != mty.Out(0).Kind() { | ||
// Should return bool. | ||
return filterProcessor{}, false | ||
} | ||
if mty.NumIn() != 2 { | ||
// Should have two input parameters. | ||
return filterProcessor{}, false | ||
} | ||
if mty.In(0) != reflect.TypeFor[context.Context]() { | ||
// Should have context.Context as first input parameter. | ||
return filterProcessor{}, false | ||
} | ||
// Duck typing of EnabledParameters | ||
pt := mty.In(1) | ||
if res, ok := pt.FieldByName("Resource"); !ok || res.Type != reflect.TypeFor[resource.Resource]() { | ||
// The second parameter should have Resource resource.Resource field. | ||
return filterProcessor{}, false | ||
} | ||
if res, ok := pt.FieldByName("InstrumentationScope"); !ok || res.Type != reflect.TypeFor[instrumentation.Scope]() { | ||
// The second parameter should have InstrumentationScope instrumentation.Scope field. | ||
return filterProcessor{}, false | ||
} | ||
if res, ok := pt.FieldByName("Severity"); !ok || res.Type != reflect.TypeFor[log.Severity]() { | ||
// The second parameter should have Severity log.Severity field. | ||
return filterProcessor{}, false | ||
} | ||
|
||
return filterProcessor{ | ||
enabledFn: m, | ||
paramType: pt, | ||
}, true | ||
} | ||
|
||
func (f filterProcessor) Enabled(ctx context.Context, param enabledParameters) bool { | ||
p := reflect.New(f.paramType).Elem() | ||
p.FieldByName("Resource").Set(reflect.ValueOf(param.Resource)) | ||
p.FieldByName("InstrumentationScope").Set(reflect.ValueOf(param.InstrumentationScope)) | ||
p.FieldByName("Severity").Set(reflect.ValueOf(param.Severity)) | ||
|
||
ctxV := reflect.ValueOf(ctx) | ||
if ctxV == (reflect.Value{}) { | ||
// In order to not get panic: reflect: Call using zero Value argument. | ||
ctxV = reflect.Zero(reflect.TypeFor[context.Context]()) | ||
} | ||
|
||
ret := f.enabledFn.Call([]reflect.Value{ctxV, p}) | ||
return ret[0].Bool() | ||
} | ||
|
||
// enabledParameters represents payload for Enabled method. | ||
type enabledParameters struct { | ||
Resource resource.Resource | ||
InstrumentationScope instrumentation.Scope | ||
Severity log.Severity | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is interesting if the dependency is used only used in tests then it does not come up as an indirect dependency for modules using
go.opentelemetry.io/otel/sdk/log
🎉It looks like the Go module graph pruning has been improved at some point.
I think that it means that we could get rid of
test
modules 😉There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit strange. I've just tried that for otelhttp, and the SDK does come as an indirect dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think we cannot rely on the Go modules pruning and I will get rid of this dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adressed 08ac146
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmathieu , I tried it once more and actually it does NOT come as an indirect dependency.
See: open-telemetry/opentelemetry-go-contrib#6763
I had to run
make go-mod-tidy
twice. Probably if we usedcrosslink tidylist
then it would not be necessaryWe can could consider revert 08ac146. But I am fine with the current approach as well. It looks cleaner. I propose to keep the current way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it looks like the difference is that you used a different package name. So that'd be the trick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not follow. The SDK is used in tests of both of
otelhttp
andotelhttp_test
packages.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but when I tried it, I kept the
otelhttp
package name for the tests. I think that's why the dependency still showed up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tests still use it. Can you double check it? Make sure to run
make go-mod-tidy
more than once 😉