Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions config/configauth/pdatacontext.go
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)
}
15 changes: 15 additions & 0 deletions config/configauth/pdatacontext_test.go
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
7 changes: 7 additions & 0 deletions internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (b *dataBuffer) logAttr(label string, value string) {
b.logEntry(" %-15s: %s", label, value)
}

func (b *dataBuffer) logPDataContext(label string, am pdata.PDataContext) {
b.logEntry("%s:", label)
am.Range(func(_ interface{}, value interface{}) {
b.logEntry(" -> %v", value) // the context key is likely an empty struct
})
}

func (b *dataBuffer) logAttributeMap(label string, am pdata.AttributeMap) {
if am.Len() == 0 {
return
Expand Down
1 change: 1 addition & 0 deletions internal/otlptext/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (textTracesMarshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
for i := 0; i < rss.Len(); i++ {
buf.logEntry("ResourceSpans #%d", i)
rs := rss.At(i)
buf.logPDataContext("Pipeline Data Context", rs.PDataContext())
buf.logAttributeMap("Resource labels", rs.Resource().Attributes())
ilss := rs.InstrumentationLibrarySpans()
for j := 0; j < ilss.Len(); j++ {
Expand Down
24 changes: 24 additions & 0 deletions model/internal/data/pdatacontext.go
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
Copy link
Member

@bogdandrutu bogdandrutu Aug 3, 2021

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?

Copy link
Member

@tigrannajaryan tigrannajaryan Aug 4, 2021

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 arbitrary interface{} (and not just a string), like it is done for context.Context, but for the Value 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.

Copy link
Member Author

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.

}

type PDataContextKeyValue struct {
Key interface{}
Value interface{}
}
3 changes: 3 additions & 0 deletions model/internal/data/protogen/trace/v1/trace.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions model/pdata/pdatacontext.go
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)
}
}
4 changes: 4 additions & 0 deletions model/pdata/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be auto-generated.

47 changes: 47 additions & 0 deletions service/internal/authcontext/authcontext.go
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)
}
2 changes: 2 additions & 0 deletions service/internal/builder/receivers_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support only TraceDataType?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Expand Down