Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/telemetrygen] Add exemplars support for telemetrygen #33320

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/add-traceid-spanid-exemplars-telemetrygen.yaml
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: []
41 changes: 41 additions & 0 deletions cmd/telemetrygen/internal/common/validate.go
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
}
70 changes: 70 additions & 0 deletions cmd/telemetrygen/internal/common/validate_test.go
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)
})
}
}
19 changes: 4 additions & 15 deletions cmd/telemetrygen/internal/logs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package logs

import (
"encoding/hex"

"github.com/spf13/pflag"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common"
Expand Down Expand Up @@ -39,23 +37,14 @@ func (c *Config) Flags(fs *pflag.FlagSet) {
// Validate validates the test scenario parameters.
func (c *Config) Validate() error {
if c.TraceID != "" {
if len(c.TraceID) != 32 {
return errInvalidTraceIDLenght
}

_, err := hex.DecodeString(c.TraceID)
if err != nil {
return errInvalidTraceID
if err := common.ValidateTraceID(c.TraceID); err != nil {
return err
}
}

if c.SpanID != "" {
if len(c.SpanID) != 16 {
return errInvalidSpanIDLenght
}
_, err := hex.DecodeString(c.SpanID)
if err != nil {
return errInvalidSpanID
if err := common.ValidateSpanID(c.SpanID); err != nil {
return err
}
}

Expand Down
66 changes: 0 additions & 66 deletions cmd/telemetrygen/internal/logs/config_test.go

This file was deleted.

7 changes: 0 additions & 7 deletions cmd/telemetrygen/internal/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common"
)

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'")
)

// Start starts the log telemetry generator
func Start(cfg *Config) error {
logger, err := common.CreateLogger(cfg.SkipSettingGRPCLogger)
Expand Down
23 changes: 23 additions & 0 deletions cmd/telemetrygen/internal/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Config struct {
NumMetrics int
MetricName string
MetricType metricType
SpanID string
TraceID string
}

// Flags registers config flags.
Expand All @@ -29,4 +31,25 @@ func (c *Config) Flags(fs *pflag.FlagSet) {

fs.Var(&c.MetricType, "metric-type", "Metric type enum. must be one of 'Gauge' or 'Sum'")
fs.IntVar(&c.NumMetrics, "metrics", 1, "Number of metrics to generate in each worker (ignored if duration is provided)")

fs.StringVar(&c.TraceID, "trace-id", "", "TraceID to use as exemplar")
fs.StringVar(&c.SpanID, "span-id", "", "SpanID to use as exemplar")

}

// Validate validates the test scenario parameters.
func (c *Config) Validate() error {
if c.TraceID != "" {
if err := common.ValidateTraceID(c.TraceID); err != nil {
return err
}
}

if c.SpanID != "" {
if err := common.ValidateSpanID(c.SpanID); err != nil {
return err
}
}

return nil
}
31 changes: 31 additions & 0 deletions cmd/telemetrygen/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package metrics

import (
"context"
"encoding/hex"
"fmt"
"sync"
"sync/atomic"
Expand All @@ -14,6 +15,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/zap"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -95,6 +97,7 @@ func Run(c *Config, exp func() (sdkmetric.Exporter, error), logger *zap.Logger)
numMetrics: c.NumMetrics,
metricName: c.MetricName,
metricType: c.MetricType,
exemplars: exemplarsFromConfig(c),
limitPerSecond: limit,
totalDuration: c.TotalDuration,
running: running,
Expand All @@ -112,3 +115,31 @@ func Run(c *Config, exp func() (sdkmetric.Exporter, error), logger *zap.Logger)
wg.Wait()
return nil
}

func exemplarsFromConfig(c *Config) []metricdata.Exemplar[int64] {
if c.TraceID != "" || c.SpanID != "" {
var exemplars []metricdata.Exemplar[int64]

exemplar := metricdata.Exemplar[int64]{
Value: 1,
Time: time.Now(),
}

if c.TraceID != "" {
// we validated this already during the Validate() function for config
// nolint: errcheck
traceID, _ := hex.DecodeString(c.TraceID)
exemplar.TraceID = traceID
}

if c.SpanID != "" {
// we validated this already during the Validate() function for config
// nolint: errcheck
spanID, _ := hex.DecodeString(c.SpanID)
exemplar.SpanID = spanID
}

return append(exemplars, exemplar)
}
return nil
}
Loading