-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/transform] Add SpanID and TraceID to the grammar (#10487)
* Add SpanID and TraceID to the grammar * Updated NewGetter * Updated readme * Update NewFunctionCall to handle SpanID and TraceID * Update Changelog * Update processor/transformprocessor/internal/common/functions.go Co-authored-by: Pablo Baeyens <[email protected]> * Updated error message * Fix lint issue * Add byte slice type to grammar * update tests * Add TraceID function * Add SpanID function * Updated changelog * Updated readme * Add error check * Fixing build checks * Fix lint issues Co-authored-by: Pablo Baeyens <[email protected]> Co-authored-by: Daniel Jaglowski <[email protected]>
- Loading branch information
1 parent
eac0998
commit 5e799df
Showing
15 changed files
with
377 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
processor/transformprocessor/internal/common/func_span_id.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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 common // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common" | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
func spanID(bytes []byte) (ExprFunc, error) { | ||
if len(bytes) != 8 { | ||
return nil, errors.New("span ids must be 8 bytes") | ||
} | ||
var idArr [8]byte | ||
copy(idArr[:8], bytes) | ||
id := pcommon.NewSpanID(idArr) | ||
return func(ctx TransformContext) interface{} { | ||
return id | ||
}, nil | ||
} |
71 changes: 71 additions & 0 deletions
71
processor/transformprocessor/internal/common/func_span_id_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://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 common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common/testhelper" | ||
) | ||
|
||
func Test_spanID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bytes []byte | ||
want pcommon.SpanID | ||
}{ | ||
{ | ||
name: "create span id", | ||
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8}, | ||
want: pcommon.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
ctx := testhelper.TestTransformContext{} | ||
|
||
exprFunc, _ := spanID(tt.bytes) | ||
actual := exprFunc(ctx) | ||
|
||
assert.Equal(t, tt.want, actual) | ||
}) | ||
} | ||
} | ||
|
||
func Test_spanID_validation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bytes []byte | ||
}{ | ||
{ | ||
name: "byte slice less than 8", | ||
bytes: []byte{1, 2, 3, 4, 5, 6, 7}, | ||
}, | ||
{ | ||
name: "byte slice longer than 8", | ||
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := traceID(tt.bytes) | ||
assert.Error(t, err, "span ids must be 8 bytes") | ||
}) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
processor/transformprocessor/internal/common/func_trace_id.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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 common // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common" | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
func traceID(bytes []byte) (ExprFunc, error) { | ||
if len(bytes) != 16 { | ||
return nil, errors.New("traces ids must be 16 bytes") | ||
} | ||
var idArr [16]byte | ||
copy(idArr[:16], bytes) | ||
id := pcommon.NewTraceID(idArr) | ||
return func(ctx TransformContext) interface{} { | ||
return id | ||
}, nil | ||
} |
71 changes: 71 additions & 0 deletions
71
processor/transformprocessor/internal/common/func_trace_id_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://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 common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common/testhelper" | ||
) | ||
|
||
func Test_traceID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bytes []byte | ||
want pcommon.TraceID | ||
}{ | ||
{ | ||
name: "create trace id", | ||
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, | ||
want: pcommon.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
ctx := testhelper.TestTransformContext{} | ||
|
||
exprFunc, _ := traceID(tt.bytes) | ||
actual := exprFunc(ctx) | ||
|
||
assert.Equal(t, tt.want, actual) | ||
}) | ||
} | ||
} | ||
|
||
func Test_traceID_validation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bytes []byte | ||
}{ | ||
{ | ||
name: "byte slice less than 16", | ||
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, | ||
}, | ||
{ | ||
name: "byte slice longer than 16", | ||
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := traceID(tt.bytes) | ||
assert.Error(t, err, "traces ids must be 16 bytes") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.