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.
[cmd/telemetrygen] Add exemplars support for telemetrygen (open-telem…
…etry#33320) This PR adds support for passing a traceID and spanID to be used as exemplars on the generated data points. This was verified with an e2e test using OTel Collector and the debug exporter in detailed verbosity, resulting in this: ``` 2024-05-31T11:41:51.378+0200 info ResourceMetrics #0 Resource SchemaURL: https://opentelemetry.io/schemas/1.13.0 ScopeMetrics #0 ScopeMetrics SchemaURL: InstrumentationScope Metric #0 Descriptor: -> Name: gen -> Description: -> Unit: -> DataType: Gauge NumberDataPoints #0 StartTimestamp: 1970-01-01 00:00:00 +0000 UTC Timestamp: 2024-05-31 09:41:51.374564535 +0000 UTC Value: 0 Exemplars: Exemplar #0 -> Trace ID: ae87dadd90e9935a4bc9660628efd569 -> Span ID: 5828fa4960140870 -> Timestamp: 2024-05-31 09:41:51.374432029 +0000 UTC -> Value: 1 ``` Given that I needed to do the validation of the traceID/spanID on the metrics config as well, I refactored the current validation done in the logging part of the command so that it's now part of the common package. Signed-off-by: Juraci Paixão Kröhling <[email protected]> --------- Signed-off-by: Juraci Paixão Kröhling <[email protected]>
- Loading branch information
1 parent
0978757
commit 9f4d61e
Showing
10 changed files
with
289 additions
and
97 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# 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: cmd/telemetrygen | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add support for adding spanID and traceID as exemplars to datapoints generated by telemetrygen | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33320] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
errInvalidTraceIDLenght = fmt.Errorf("TraceID must be a 32 character hex string, like: 'ae87dadd90e9935a4bc9660628efd569'") | ||
errInvalidSpanIDLenght = fmt.Errorf("SpanID must be a 16 character hex string, like: '5828fa4960140870'") | ||
errInvalidTraceID = fmt.Errorf("failed to create traceID byte array from the given traceID, make sure the traceID is a hex representation of a [16]byte, like: 'ae87dadd90e9935a4bc9660628efd569'") | ||
errInvalidSpanID = fmt.Errorf("failed to create SpanID byte array from the given SpanID, make sure the SpanID is a hex representation of a [8]byte, like: '5828fa4960140870'") | ||
) | ||
|
||
func ValidateTraceID(traceID string) error { | ||
if len(traceID) != 32 { | ||
return errInvalidTraceIDLenght | ||
} | ||
|
||
_, err := hex.DecodeString(traceID) | ||
if err != nil { | ||
return errInvalidTraceID | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ValidateSpanID(spanID string) error { | ||
if len(spanID) != 16 { | ||
return errInvalidSpanIDLenght | ||
} | ||
_, err := hex.DecodeString(spanID) | ||
if err != nil { | ||
return errInvalidSpanID | ||
} | ||
|
||
return 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,70 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateTraceID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
traceID string | ||
expected error | ||
}{ | ||
{ | ||
name: "Valid", | ||
traceID: "ae87dadd90e9935a4bc9660628efd569", | ||
expected: nil, | ||
}, | ||
{ | ||
name: "InvalidLength", | ||
traceID: "invalid-length", | ||
expected: errInvalidTraceIDLenght, | ||
}, | ||
{ | ||
name: "InvalidTraceID", | ||
traceID: "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", | ||
expected: errInvalidTraceID, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateTraceID(tt.traceID) | ||
assert.ErrorIs(t, err, tt.expected) | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateSpanID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
spanID string | ||
expected error | ||
}{ | ||
{ | ||
name: "Valid", | ||
spanID: "5828fa4960140870", | ||
expected: nil, | ||
}, | ||
{ | ||
name: "InvalidLength", | ||
spanID: "invalid-length", | ||
expected: errInvalidSpanIDLenght, | ||
}, | ||
{ | ||
name: "InvalidTraceID", | ||
spanID: "zzzzzzzzzzzzzzzz", | ||
expected: errInvalidSpanID, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateSpanID(tt.spanID) | ||
assert.ErrorIs(t, err, tt.expected) | ||
}) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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.