forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove duplicate parse IDs code, avoid coreinternal dependency (open-…
…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
1 parent
a21144b
commit 9229e9e
Showing
10 changed files
with
180 additions
and
86 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
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. |
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,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 | ||
} |
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: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) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.