-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[reflect] sdk/log/xlog: Add FilterProcessor and EnabledParameters #6286
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
Changes from 33 commits
96a79ee
6282e71
69ff66d
bf61d44
5b3c8ca
f24fc64
fffc077
55adcc4
008cf4d
4df6669
d31f329
fe5ae63
4e54827
abe7833
d5817c6
c23714b
cb91d27
71376d1
e6b34ae
4ff41e1
628f916
98d6811
5cf1eaf
9037771
3a07761
c56e9da
91d84a9
685a417
0547924
7075931
648e21f
2941fb3
2a93c9f
9da340a
d55948e
08ac146
9475ed8
25937e0
e4d1ebf
2ac7f71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ require ( | |
| go.opentelemetry.io/otel v1.34.0 | ||
| go.opentelemetry.io/otel/log v0.10.0 | ||
| go.opentelemetry.io/otel/sdk v1.34.0 | ||
| go.opentelemetry.io/otel/sdk/log/xlog v0.0.0-00010101000000-000000000000 | ||
|
||
| go.opentelemetry.io/otel/trace v1.34.0 | ||
| ) | ||
|
|
||
|
|
@@ -29,6 +30,8 @@ replace go.opentelemetry.io/otel/trace => ../../trace | |
|
|
||
| replace go.opentelemetry.io/otel/sdk => ../ | ||
|
|
||
| replace go.opentelemetry.io/otel/sdk/log/xlog => ./xlog | ||
|
|
||
| replace go.opentelemetry.io/otel/log => ../../log | ||
|
|
||
| replace go.opentelemetry.io/otel => ../.. | ||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.