Skip to content

Commit

Permalink
[pkg/ottl] Add string access for span kind (#21773)
Browse files Browse the repository at this point in the history
* add string kind accessor

* add changelog

* Run make crosslink

* Switch to pdata strings

* Update readme

* run make crosslink

* Add support for deprecated string format

* make gotidy

* make crosslink

* Print for unknown span kind

* Fix string accessors to behave like other accessors
  • Loading branch information
TylerHelmuth authored May 18, 2023
1 parent 4018c5e commit 80961b0
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 37 deletions.
16 changes: 16 additions & 0 deletions .chloggen/ottl-path-accessor-for-string-kind.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add access to get and set span kind using a string

# One or more tracking issues related to the change
issues: [21773]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
73 changes: 72 additions & 1 deletion pkg/ottl/contexts/internal/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/trace"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

Expand Down Expand Up @@ -68,7 +69,15 @@ func SpanPathGetSetter[K SpanContext](path []ottl.Field) (ottl.GetSetter[K], err
case "name":
return accessSpanName[K](), nil
case "kind":
return accessKind[K](), nil
if len(path) == 1 {
return accessKind[K](), nil
}
if path[1].Name == "string" {
return accessStringKind[K](), nil
}
if path[1].Name == "deprecated_string" {
return accessDeprecatedStringKind[K](), nil
}
case "start_time_unix_nano":
return accessStartTimeUnixNano[K](), nil
case "end_time_unix_nano":
Expand Down Expand Up @@ -289,6 +298,68 @@ func accessKind[K SpanContext]() ottl.StandardGetSetter[K] {
}
}

func accessStringKind[K SpanContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (interface{}, error) {
return tCtx.GetSpan().Kind().String(), nil
},
Setter: func(ctx context.Context, tCtx K, val interface{}) error {
if s, ok := val.(string); ok {
var kind ptrace.SpanKind
switch s {
case "Unspecified":
kind = ptrace.SpanKindUnspecified
case "Internal":
kind = ptrace.SpanKindInternal
case "Server":
kind = ptrace.SpanKindServer
case "Client":
kind = ptrace.SpanKindClient
case "Producer":
kind = ptrace.SpanKindProducer
case "Consumer":
kind = ptrace.SpanKindConsumer
default:
return fmt.Errorf("unknown span kind string, %v", s)
}
tCtx.GetSpan().SetKind(kind)
}
return nil
},
}
}

func accessDeprecatedStringKind[K SpanContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (interface{}, error) {
return traceutil.SpanKindStr(tCtx.GetSpan().Kind()), nil
},
Setter: func(ctx context.Context, tCtx K, val interface{}) error {
if s, ok := val.(string); ok {
var kind ptrace.SpanKind
switch s {
case "SPAN_KIND_UNSPECIFIED":
kind = ptrace.SpanKindUnspecified
case "SPAN_KIND_INTERNAL":
kind = ptrace.SpanKindInternal
case "SPAN_KIND_SERVER":
kind = ptrace.SpanKindServer
case "SPAN_KIND_CLIENT":
kind = ptrace.SpanKindClient
case "SPAN_KIND_PRODUCER":
kind = ptrace.SpanKindProducer
case "SPAN_KIND_CONSUMER":
kind = ptrace.SpanKindConsumer
default:
return fmt.Errorf("unknown span kind deprecated string, %v", s)
}
tCtx.GetSpan().SetKind(kind)
}
return nil
},
}
}

func accessStartTimeUnixNano[K SpanContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (interface{}, error) {
Expand Down
32 changes: 32 additions & 0 deletions pkg/ottl/contexts/internal/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,38 @@ func TestSpanPathGetSetter(t *testing.T) {
span.SetKind(ptrace.SpanKindClient)
},
},
{
name: "string kind",
path: []ottl.Field{
{
Name: "kind",
},
{
Name: "string",
},
},
orig: "Server",
newVal: "Client",
modified: func(span ptrace.Span) {
span.SetKind(ptrace.SpanKindClient)
},
},
{
name: "deprecated string kind",
path: []ottl.Field{
{
Name: "kind",
},
{
Name: "deprecated_string",
},
},
orig: "SPAN_KIND_SERVER",
newVal: "SPAN_KIND_CLIENT",
modified: func(span ptrace.Span) {
span.SetKind(ptrace.SpanKindClient)
},
},
{
name: "start_time_unix_nano",
path: []ottl.Field{
Expand Down
74 changes: 38 additions & 36 deletions pkg/ottl/contexts/ottlspan/README.md

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions pkg/ottl/contexts/ottlspan/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,38 @@ func Test_newPathGetSetter(t *testing.T) {
span.SetKind(ptrace.SpanKindClient)
},
},
{
name: "string kind",
path: []ottl.Field{
{
Name: "kind",
},
{
Name: "string",
},
},
orig: "Server",
newVal: "Client",
modified: func(span ptrace.Span, il pcommon.InstrumentationScope, resource pcommon.Resource, cache pcommon.Map) {
span.SetKind(ptrace.SpanKindClient)
},
},
{
name: "deprecated string kind",
path: []ottl.Field{
{
Name: "kind",
},
{
Name: "deprecated_string",
},
},
orig: "SPAN_KIND_SERVER",
newVal: "SPAN_KIND_CLIENT",
modified: func(span ptrace.Span, il pcommon.InstrumentationScope, resource pcommon.Resource, cache pcommon.Map) {
span.SetKind(ptrace.SpanKindClient)
},
},
{
name: "start_time_unix_nano",
path: []ottl.Field{
Expand Down
7 changes: 7 additions & 0 deletions pkg/ottl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/json-iterator/go v1.1.12
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.77.0
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/collector/component v0.77.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0011
Expand Down Expand Up @@ -43,8 +44,14 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

retract (
v0.76.2
v0.76.1
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../pdatatest
7 changes: 7 additions & 0 deletions processor/routingprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.1.17 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.77.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/featuregate v0.77.0 // indirect
Expand All @@ -64,3 +65,9 @@ retract (
v0.76.1
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest
7 changes: 7 additions & 0 deletions processor/transformprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.77.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/featuregate v0.77.0 // indirect
Expand All @@ -53,3 +54,9 @@ retract (
v0.76.1
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

0 comments on commit 80961b0

Please sign in to comment.