-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add auth data to pdata resource #3745
Changes from all commits
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,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package configauth | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
type ctxKey struct{} | ||
|
||
type AuthContext interface { | ||
Equal(other interface{}) bool | ||
GetAttribute(attrName string) interface{} | ||
GetAttributeNames() []string | ||
} | ||
|
||
func InjectAuthContext(ctx context.Context, ac AuthContext) context.Context { | ||
return context.WithValue(ctx, ctxKey{}, ac) | ||
} | ||
|
||
func ExtractAuthContext(ctx context.Context) (AuthContext, bool) { | ||
ac, ok := ctx.Value(ctxKey{}).(AuthContext) | ||
if !ok { | ||
return nil, false | ||
} | ||
return ac, true | ||
} | ||
|
||
func InjectPDataContext(pda pdata.PDataContext, ac AuthContext) { | ||
pda.Set(ctxKey{}, ac) | ||
} | ||
|
||
func ExtractPDataContext(pda pdata.PDataContext) AuthContext { | ||
return pda.Get(ctxKey{}).(AuthContext) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package configauth |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package data | ||
|
||
type PDataContext struct { | ||
List []PDataContextKeyValue | ||
} | ||
|
||
type PDataContextKeyValue struct { | ||
Key interface{} | ||
Value interface{} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pdata | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/model/internal/data" | ||
) | ||
|
||
type PDataContext struct { | ||
orig *data.PDataContext | ||
} | ||
|
||
func newPDataContext(orig *data.PDataContext) PDataContext { | ||
return PDataContext{orig} | ||
} | ||
|
||
func (pdc PDataContext) Set(key, val interface{}) { | ||
pdc.orig.List = append(pdc.orig.List, data.PDataContextKeyValue{Key: key, Value: val}) | ||
} | ||
|
||
func (pdc PDataContext) Get(key interface{}) interface{} { | ||
for _, kv := range pdc.orig.List { | ||
if kv.Key == key { | ||
return kv.Value | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (pdc PDataContext) Range(f func(k interface{}, v interface{})) { | ||
for _, kv := range pdc.orig.List { | ||
f(kv.Key, kv.Value) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,3 +152,7 @@ func (ms SpanStatus) SetCode(v StatusCode) { | |
ms.orig.DeprecatedCode = otlptrace.Status_DEPRECATED_STATUS_CODE_UNKNOWN_ERROR | ||
} | ||
} | ||
|
||
func (ms ResourceSpans) PDataContext() PDataContext { | ||
return newPDataContext(&(*ms.orig).Context) | ||
} | ||
Comment on lines
+156
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be auto-generated. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package authcontext | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/config/configauth" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
func NewTracesInjector(next consumer.Traces) consumer.Traces { | ||
return injectorForTraces{next} | ||
} | ||
|
||
type injectorForTraces struct { | ||
next consumer.Traces | ||
} | ||
|
||
func (tc injectorForTraces) Capabilities() consumer.Capabilities { | ||
return consumer.Capabilities{MutatesData: false} | ||
} | ||
|
||
func (tc injectorForTraces) ConsumeTraces(ctx context.Context, td pdata.Traces) error { | ||
authContext, ok := configauth.ExtractAuthContext(ctx) | ||
if ok && authContext != nil { | ||
for i := 0; i < td.ResourceSpans().Len(); i++ { | ||
rss := td.ResourceSpans().At(i) | ||
configauth.InjectPDataContext(rss.PDataContext(), authContext) | ||
} | ||
} | ||
|
||
return tc.next.ConsumeTraces(ctx, td) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"go.opentelemetry.io/collector/component/componenterror" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/service/internal/authcontext" | ||
"go.opentelemetry.io/collector/service/internal/components" | ||
"go.opentelemetry.io/collector/service/internal/fanoutconsumer" | ||
) | ||
|
@@ -182,6 +183,7 @@ func attachReceiverToPipelines( | |
switch dataType { | ||
case config.TracesDataType: | ||
junction := buildFanoutTraceConsumer(builtPipelines) | ||
junction = authcontext.NewTracesInjector(junction) | ||
createdReceiver, err = factory.CreateTracesReceiver(ctx, set, cfg, junction) | ||
|
||
case config.MetricsDataType: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support only TraceDataType? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a first iteration, yes. Once we validate the approach and get it out for testing, we might (will) expand it to other data types. |
||
|
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.
Can we use the already defined "AnyValue"/Attributes?
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.
Not for the
Key
, it needs to be an arbitraryinterface{}
(and not just a string), like it is done forcontext.Context
, but for theValue
it is an interesting idea, since it may make some code that needs to deal with both telemetry attributes and PDataContext easier to write. It is worth exploring.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.
Did you mean pdata.AttributeValue? If so, that would cause an import cycle that is not allowed.