Skip to content

Commit

Permalink
Remove duplicate parse IDs code, avoid coreinternal dependency (open-…
Browse files Browse the repository at this point in the history
…telemetry#16393)

The main reason to do this, is to allow coreinternal/processor/filter* to use ottl.

Signed-off-by: Bogdan Drutu <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Nov 21, 2022
1 parent a21144b commit 9229e9e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 86 deletions.
16 changes: 16 additions & 0 deletions .chloggen/rmdup.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: Remove duplicate parse IDs code, avoid coreinternal dependency

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

# (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: The "[trace|span]_id_string" func returns "000..000" string for invalid ids.
46 changes: 46 additions & 0 deletions pkg/ottl/contexts/internal/ottlcommon/ids.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:https://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 ottlcommon // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ottlcommon"

import (
"encoding/hex"
"errors"

"go.opentelemetry.io/collector/pdata/pcommon"
)

func ParseSpanID(spanIDStr string) (pcommon.SpanID, error) {
var id pcommon.SpanID
if hex.DecodedLen(len(spanIDStr)) != len(id) {
return pcommon.SpanID{}, errors.New("span ids must be 16 hex characters")
}
_, err := hex.Decode(id[:], []byte(spanIDStr))
if err != nil {
return pcommon.SpanID{}, err
}
return id, nil
}

func ParseTraceID(traceIDStr string) (pcommon.TraceID, error) {
var id pcommon.TraceID
if hex.DecodedLen(len(traceIDStr)) != len(id) {
return pcommon.TraceID{}, errors.New("trace ids must be 32 hex characters")
}
_, err := hex.Decode(id[:], []byte(traceIDStr))
if err != nil {
return pcommon.TraceID{}, err
}
return id, nil
}
71 changes: 71 additions & 0 deletions pkg/ottl/contexts/internal/ottlcommon/ids_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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:https://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 ottlcommon

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseSpanIDError(t *testing.T) {
tests := []struct {
name string
input string
wantErr string
}{
{
name: "incorrect size",
input: "0123456789abcde",
wantErr: "span ids must be 16 hex characters",
},
{
name: "incorrect characters",
input: "0123456789Xbcdef",
wantErr: "encoding/hex: invalid byte: U+0058 'X'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseSpanID(tt.input)
assert.EqualError(t, err, tt.wantErr)
})
}
}

func TestParseTraceIDError(t *testing.T) {
tests := []struct {
name string
input string
wantErr string
}{
{
name: "incorrect size",
input: "0123456789abcdef0123456789abcde",
wantErr: "trace ids must be 32 hex characters",
},
{
name: "incorrect characters",
input: "0123456789Xbcdef0123456789abcdef",
wantErr: "encoding/hex: invalid byte: U+0058 'X'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseTraceID(tt.input)
assert.EqualError(t, err, tt.wantErr)
})
}
}
55 changes: 18 additions & 37 deletions pkg/ottl/contexts/internal/ottlcommon/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ package ottlcommon // import "github.com/open-telemetry/opentelemetry-collector-
import (
"context"
"encoding/hex"
"errors"
"fmt"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
"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 @@ -148,13 +146,16 @@ func accessTraceID[K SpanContext]() ottl.StandardGetSetter[K] {
func accessStringTraceID[K SpanContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (interface{}, error) {
return traceutil.TraceIDToHexOrEmptyString(tCtx.GetSpan().TraceID()), nil
id := tCtx.GetSpan().TraceID()
return hex.EncodeToString(id[:]), nil
},
Setter: func(ctx context.Context, tCtx K, val interface{}) error {
if str, ok := val.(string); ok {
if traceID, err := parseTraceID(str); err == nil {
tCtx.GetSpan().SetTraceID(traceID)
id, err := ParseTraceID(str)
if err != nil {
return err
}
tCtx.GetSpan().SetTraceID(id)
}
return nil
},
Expand All @@ -178,13 +179,16 @@ func accessSpanID[K SpanContext]() ottl.StandardGetSetter[K] {
func accessStringSpanID[K SpanContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (interface{}, error) {
return traceutil.SpanIDToHexOrEmptyString(tCtx.GetSpan().SpanID()), nil
id := tCtx.GetSpan().SpanID()
return hex.EncodeToString(id[:]), nil
},
Setter: func(ctx context.Context, tCtx K, val interface{}) error {
if str, ok := val.(string); ok {
if spanID, err := parseSpanID(str); err == nil {
tCtx.GetSpan().SetSpanID(spanID)
id, err := ParseSpanID(str)
if err != nil {
return err
}
tCtx.GetSpan().SetSpanID(id)
}
return nil
},
Expand Down Expand Up @@ -243,13 +247,16 @@ func accessParentSpanID[K SpanContext]() ottl.StandardGetSetter[K] {
func accessStringParentSpanID[K SpanContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (interface{}, error) {
return traceutil.SpanIDToHexOrEmptyString(tCtx.GetSpan().ParentSpanID()), nil
id := tCtx.GetSpan().ParentSpanID()
return hex.EncodeToString(id[:]), nil
},
Setter: func(ctx context.Context, tCtx K, val interface{}) error {
if str, ok := val.(string); ok {
if spanID, err := parseSpanID(str); err == nil {
tCtx.GetSpan().SetParentSpanID(spanID)
id, err := ParseSpanID(str)
if err != nil {
return err
}
tCtx.GetSpan().SetParentSpanID(id)
}
return nil
},
Expand Down Expand Up @@ -455,29 +462,3 @@ func accessStatusMessage[K SpanContext]() ottl.StandardGetSetter[K] {
},
}
}

func parseSpanID(spanIDStr string) (pcommon.SpanID, error) {
id, err := hex.DecodeString(spanIDStr)
if err != nil {
return pcommon.SpanID{}, err
}
if len(id) != 8 {
return pcommon.SpanID{}, errors.New("span ids must be 8 bytes")
}
var idArr [8]byte
copy(idArr[:8], id)
return pcommon.SpanID(idArr), nil
}

func parseTraceID(traceIDStr string) (pcommon.TraceID, error) {
id, err := hex.DecodeString(traceIDStr)
if err != nil {
return pcommon.TraceID{}, err
}
if len(id) != 16 {
return pcommon.TraceID{}, errors.New("traces ids must be 16 bytes")
}
var idArr [16]byte
copy(idArr[:16], id)
return pcommon.TraceID(idArr), nil
}
46 changes: 12 additions & 34 deletions pkg/ottl/contexts/ottllog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ package ottllog // import "github.com/open-telemetry/opentelemetry-collector-con
import (
"context"
"encoding/hex"
"errors"
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ottlcommon"
)
Expand Down Expand Up @@ -292,13 +290,16 @@ func accessTraceID() ottl.StandardGetSetter[TransformContext] {
func accessStringTraceID() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
return traceutil.TraceIDToHexOrEmptyString(tCtx.GetLogRecord().TraceID()), nil
id := tCtx.GetLogRecord().TraceID()
return hex.EncodeToString(id[:]), nil
},
Setter: func(ctx context.Context, tCtx TransformContext, val interface{}) error {
if str, ok := val.(string); ok {
if traceID, err := parseTraceID(str); err == nil {
tCtx.GetLogRecord().SetTraceID(traceID)
id, err := ottlcommon.ParseTraceID(str)
if err != nil {
return err
}
tCtx.GetLogRecord().SetTraceID(id)
}
return nil
},
Expand All @@ -322,41 +323,18 @@ func accessSpanID() ottl.StandardGetSetter[TransformContext] {
func accessStringSpanID() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
return traceutil.SpanIDToHexOrEmptyString(tCtx.GetLogRecord().SpanID()), nil
id := tCtx.GetLogRecord().SpanID()
return hex.EncodeToString(id[:]), nil
},
Setter: func(ctx context.Context, tCtx TransformContext, val interface{}) error {
if str, ok := val.(string); ok {
if spanID, err := parseSpanID(str); err == nil {
tCtx.GetLogRecord().SetSpanID(spanID)
id, err := ottlcommon.ParseSpanID(str)
if err != nil {
return err
}
tCtx.GetLogRecord().SetSpanID(id)
}
return nil
},
}
}

func parseSpanID(spanIDStr string) (pcommon.SpanID, error) {
id, err := hex.DecodeString(spanIDStr)
if err != nil {
return pcommon.SpanID{}, err
}
if len(id) != 8 {
return pcommon.SpanID{}, errors.New("span ids must be 8 bytes")
}
var idArr [8]byte
copy(idArr[:8], id)
return pcommon.SpanID(idArr), nil
}

func parseTraceID(traceIDStr string) (pcommon.TraceID, error) {
id, err := hex.DecodeString(traceIDStr)
if err != nil {
return pcommon.TraceID{}, err
}
if len(id) != 16 {
return pcommon.TraceID{}, errors.New("traces ids must be 16 bytes")
}
var idArr [16]byte
copy(idArr[:16], id)
return pcommon.TraceID(idArr), nil
}
9 changes: 4 additions & 5 deletions pkg/ottl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/alecthomas/participle/v2 v2.0.0-beta.5
github.com/gobwas/glob v0.2.3
github.com/iancoleman/strcase v0.2.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/collector/component v0.0.0-20221119033128-e3509cd9f772
go.opentelemetry.io/collector/pdata v0.64.2-0.20221119033128-e3509cd9f772
Expand All @@ -18,18 +17,19 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.4.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
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/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
go.opentelemetry.io/collector v0.64.2-0.20221119033128-e3509cd9f772 // indirect
go.opentelemetry.io/collector/consumer v0.0.0-20221119033128-e3509cd9f772 // indirect
go.opentelemetry.io/collector/featuregate v0.0.0-20221119033128-e3509cd9f772 // indirect
Expand All @@ -42,7 +42,6 @@ require (
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

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

0 comments on commit 9229e9e

Please sign in to comment.