Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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/telemetrygen-signal-attributes.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 custom telemetry attributes

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26505]

# (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: [user]
28 changes: 22 additions & 6 deletions cmd/telemetrygen/internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ type Config struct {
ReportingInterval time.Duration

// OTLP config
Endpoint string
Insecure bool
UseHTTP bool
HTTPPath string
Headers KeyValue
ResourceAttributes KeyValue
Endpoint string
Insecure bool
UseHTTP bool
HTTPPath string
Headers KeyValue
ResourceAttributes KeyValue
TelemetryAttributes KeyValue
}

func (c *Config) GetAttributes() []attribute.KeyValue {
Expand All @@ -69,6 +70,17 @@ func (c *Config) GetAttributes() []attribute.KeyValue {
return attributes
}

func (c *Config) GetTelemetryAttributes() []attribute.KeyValue {
var attributes []attribute.KeyValue

if len(c.TelemetryAttributes) > 0 {
for k, v := range c.TelemetryAttributes {
attributes = append(attributes, attribute.String(k, v))
}
}
return attributes
}

// CommonFlags registers common config flags.
func (c *Config) CommonFlags(fs *pflag.FlagSet) {
fs.IntVar(&c.WorkerCount, "workers", 1, "Number of workers (goroutines) to run")
Expand All @@ -92,4 +104,8 @@ func (c *Config) CommonFlags(fs *pflag.FlagSet) {
fs.Var(&c.ResourceAttributes, "otlp-attributes", "Custom resource attributes to use. The value is expected in the format key=\"value\"."+
"Note you may need to escape the quotes when using the tool from a cli."+
"Flag may be repeated to set multiple attributes (e.g -otlp-attributes key1=\"value1\" -otlp-attributes key2=\"value2\")")

c.TelemetryAttributes = make(map[string]string)
fs.Var(&c.TelemetryAttributes, "telemetry-attributes", "Custom telemetry attributes to use. The value is expected in the format \"key=\\\"value\\\"\". "+
"Flag may be repeated to set multiple attributes (e.g --telemetry-attributes \"key1=\\\"value1\\\"\" --telemetry-attributes \"key2=\\\"value2\\\"\")")
}
2 changes: 1 addition & 1 deletion cmd/telemetrygen/internal/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Run(c *Config, exp exporter, logger *zap.Logger) error {
index: i,
}

go w.simulateLogs(res, exp)
go w.simulateLogs(res, exp, c.GetTelemetryAttributes())
}
if c.TotalDuration > 0 {
time.Sleep(c.TotalDuration)
Expand Down
8 changes: 7 additions & 1 deletion cmd/telemetrygen/internal/logs/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/zap"
"golang.org/x/time/rate"
Expand All @@ -27,7 +28,7 @@ type worker struct {
index int // worker index
}

func (w worker) simulateLogs(res *resource.Resource, exporter exporter) {
func (w worker) simulateLogs(res *resource.Resource, exporter exporter, telemetryAttributes []attribute.KeyValue) {
limiter := rate.NewLimiter(w.limitPerSecond, 1)
var i int64

Expand All @@ -44,9 +45,14 @@ func (w worker) simulateLogs(res *resource.Resource, exporter exporter) {
log.SetDroppedAttributesCount(1)
log.SetSeverityNumber(plog.SeverityNumberInfo)
log.SetSeverityText("Info")
log.Attributes()
lattrs := log.Attributes()
lattrs.PutStr("app", "server")

for i, key := range telemetryAttributes {
lattrs.PutStr(key.Value.AsString(), telemetryAttributes[i].Value.AsString())
}

if err := exporter.export(logs); err != nil {
w.logger.Fatal("exporter failed", zap.Error(err))
}
Expand Down
109 changes: 109 additions & 0 deletions cmd/telemetrygen/internal/logs/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common"
)

const (
telemetryAttrKeyOne = "k1"
telemetryAttrKeyTwo = "k2"
telemetryAttrValueOne = "v1"
telemetryAttrValueTwo = "v2"
)

type mockExporter struct {
logs []plog.Logs
}
Expand Down Expand Up @@ -96,3 +103,105 @@ func TestCustomBody(t *testing.T) {

assert.Equal(t, "custom body", exp.logs[0].ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Body().AsString())
}

func TestLogsWithNoTelemetryAttributes(t *testing.T) {
cfg := configWithNoAttributes(2, "custom body")

exp := &mockExporter{}

// test
logger, _ := zap.NewDevelopment()
require.NoError(t, Run(cfg, exp, logger))

time.Sleep(1 * time.Second)

// verify
require.Len(t, exp.logs, 2)
for _, log := range exp.logs {
rlogs := log.ResourceLogs()
for i := 0; i < rlogs.Len(); i++ {
attrs := rlogs.At(i).ScopeLogs().At(0).LogRecords().At(0).Attributes()
assert.Equal(t, 1, attrs.Len(), "shouldn't have more than 1 attribute")
}
}
}

func TestLogsWithOneTelemetryAttributes(t *testing.T) {
qty := 1
cfg := configWithOneAttribute(qty, "custom body")

exp := &mockExporter{}

// test
logger, _ := zap.NewDevelopment()
require.NoError(t, Run(cfg, exp, logger))

time.Sleep(1 * time.Second)

// verify
require.Len(t, exp.logs, qty)
for _, log := range exp.logs {
rlogs := log.ResourceLogs()
for i := 0; i < rlogs.Len(); i++ {
attrs := rlogs.At(i).ScopeLogs().At(0).LogRecords().At(0).Attributes()
assert.Equal(t, 2, attrs.Len(), "shouldn't have less than 2 attributes")
}
}
}

func TestLogsWithMultipleTelemetryAttributes(t *testing.T) {
qty := 1
cfg := configWithMultipleAttributes(qty, "custom body")

exp := &mockExporter{}

// test
logger, _ := zap.NewDevelopment()
require.NoError(t, Run(cfg, exp, logger))

time.Sleep(1 * time.Second)

// verify
require.Len(t, exp.logs, qty)
for _, log := range exp.logs {
rlogs := log.ResourceLogs()
for i := 0; i < rlogs.Len(); i++ {
attrs := rlogs.At(i).ScopeLogs().At(0).LogRecords().At(0).Attributes()
assert.Equal(t, 3, attrs.Len(), "shouldn't have less than 3 attributes")
}
}
}

func configWithNoAttributes(qty int, body string) *Config {
return &Config{
Body: body,
NumLogs: qty,
Config: common.Config{
WorkerCount: 1,
TelemetryAttributes: nil,
},
}
}

func configWithOneAttribute(qty int, body string) *Config {
return &Config{
Body: body,
NumLogs: qty,
Config: common.Config{
WorkerCount: 1,
TelemetryAttributes: common.KeyValue{telemetryAttrKeyOne: telemetryAttrValueOne},
},
}
}

func configWithMultipleAttributes(qty int, body string) *Config {
kvs := common.KeyValue{telemetryAttrKeyOne: telemetryAttrValueOne, telemetryAttrKeyTwo: telemetryAttrValueTwo}
return &Config{
Body: body,
NumLogs: qty,
Config: common.Config{
WorkerCount: 1,
TelemetryAttributes: kvs,
},
}
}
2 changes: 1 addition & 1 deletion cmd/telemetrygen/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func Run(c *Config, exp sdkmetric.Exporter, logger *zap.Logger) error {
index: i,
}

go w.simulateMetrics(res, exp)
go w.simulateMetrics(res, exp, c.GetTelemetryAttributes())
}
if c.TotalDuration > 0 {
time.Sleep(c.TotalDuration)
Expand Down
8 changes: 5 additions & 3 deletions cmd/telemetrygen/internal/metrics/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync/atomic"
"time"

"go.opentelemetry.io/otel/attribute"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -26,7 +27,7 @@ type worker struct {
index int // worker index
}

func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter) {
func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter, signalAttrs []attribute.KeyValue) {
limiter := rate.NewLimiter(w.limitPerSecond, 1)
var i int64

Expand All @@ -41,8 +42,9 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Time: time.Now(),
Value: i,
Time: time.Now(),
Value: i,
Attributes: attribute.NewSet(signalAttrs...),
},
},
},
Expand Down
Loading