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] create a simple counter metric generator #17898

Merged
merged 9 commits into from
Feb 15, 2023
Merged
Changes from 1 commit
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
Next Next commit
[cmd/telemetrygen] create a simple counter metric generator
atoulme committed Feb 14, 2023
commit 4b5468ada5f12fca7783565293aba061db1d3e2e
16 changes: 16 additions & 0 deletions .chloggen/telemetrygen-metrics.yaml
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: telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Implement a simple metric generation command for telemetrygen

# One or more tracking issues related to the change
issues: [17986]

# (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:
13 changes: 9 additions & 4 deletions cmd/telemetrygen/config.go
Original file line number Diff line number Diff line change
@@ -16,15 +16,18 @@
package main // import "github.com/open-telemetry/opentelemetry-collector-contrib/telemetrygen/internal/telemetrygen"

import (
"errors"
"os"

"github.com/spf13/cobra"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/metrics"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/traces"
)

var tracesCfg *traces.Config
var (
tracesCfg *traces.Config
metricsCfg *metrics.Config
)

// rootCmd is the root command on which will be run children commands
var rootCmd = &cobra.Command{
@@ -49,8 +52,7 @@ var metricsCmd = &cobra.Command{
Short: "Simulates a client generating metrics",
Example: "telemetrygen metrics",
RunE: func(cmd *cobra.Command, args []string) error {
err := errors.New("[WIP] The 'metrics' command is a work in progress, come back later")
return err
return metrics.Start(metricsCfg)
},
}

@@ -60,6 +62,9 @@ func init() {
tracesCfg = new(traces.Config)
tracesCfg.Flags(tracesCmd.Flags())

metricsCfg = new(metrics.Config)
metricsCfg.Flags(metricsCmd.Flags())

// Disabling completion command for end user
// https://github.com/spf13/cobra/blob/master/shell_completions.md
rootCmd.CompletionOptions.DisableDefaultCmd = true
5 changes: 5 additions & 0 deletions cmd/telemetrygen/go.mod
Original file line number Diff line number Diff line change
@@ -8,10 +8,14 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.36.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.36.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.13.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.13.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.13.0
go.opentelemetry.io/otel/metric v0.36.0
go.opentelemetry.io/otel/sdk v1.13.0
go.opentelemetry.io/otel/sdk/metric v0.36.0
go.opentelemetry.io/otel/trace v1.13.0
go.uber.org/atomic v1.10.0
go.uber.org/zap v1.24.0
@@ -29,6 +33,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.13.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.36.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.4.0 // indirect
10 changes: 10 additions & 0 deletions cmd/telemetrygen/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 138 additions & 0 deletions cmd/telemetrygen/internal/metrics/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2018 The Jaeger Authors.
atoulme marked this conversation as resolved.
Show resolved Hide resolved
//
// 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 metrics

import (
"fmt"
"strings"
"sync"
"time"

"github.com/spf13/pflag"
"go.uber.org/atomic"
"go.uber.org/zap"
"golang.org/x/time/rate"
)

var (
errFormatOTLPAttributes = fmt.Errorf("value should be of the format key=\"value\"")
errDoubleQuotesOTLPAttributes = fmt.Errorf("value should be a string wrapped in double quotes")
)

// Config describes the test scenario.
type Config struct {
atoulme marked this conversation as resolved.
Show resolved Hide resolved
WorkerCount int
NumMetrics int
Rate int64
TotalDuration time.Duration
ReportingInterval time.Duration

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

type KeyValue map[string]string
atoulme marked this conversation as resolved.
Show resolved Hide resolved

var _ pflag.Value = (*KeyValue)(nil)

func (v *KeyValue) String() string {
return ""
}

func (v *KeyValue) Set(s string) error {
kv := strings.SplitN(s, "=", 2)
if len(kv) != 2 {
return errFormatOTLPAttributes
}
val := kv[1]
if len(val) < 2 || !strings.HasPrefix(val, "\"") || !strings.HasSuffix(val, "\"") {
return errDoubleQuotesOTLPAttributes
}

(*v)[kv[0]] = val[1 : len(val)-1]
return nil
}

func (v *KeyValue) Type() string {
return "map[string]string"
}

// Flags registers config flags.
func (c *Config) Flags(fs *pflag.FlagSet) {
fs.IntVar(&c.WorkerCount, "workers", 1, "Number of workers (goroutines) to run")
atoulme marked this conversation as resolved.
Show resolved Hide resolved
fs.IntVar(&c.NumMetrics, "metrics", 1, "Number of metrics to generate in each worker (ignored if duration is provided")
fs.Int64Var(&c.Rate, "rate", 0, "Approximately how many metrics per second each worker should generate. Zero means no throttling.")
fs.DurationVar(&c.TotalDuration, "duration", 0, "For how long to run the test")
fs.DurationVar(&c.ReportingInterval, "interval", 1*time.Second, "Reporting interval (default 1 second)")

fs.StringVar(&c.Endpoint, "otlp-endpoint", "localhost:4317", "Target to which the exporter is going to send metrics. This MAY be configured to include a path (e.g. example.com/v1/metrics)")
fs.BoolVar(&c.Insecure, "otlp-insecure", false, "Whether to enable client transport security for the exporter's grpc or http connection")
fs.BoolVar(&c.UseHTTP, "otlp-http", false, "Whether to use HTTP exporter rather than a gRPC one")

// custom headers
c.Headers = make(map[string]string)
fs.Var(&c.Headers, "otlp-header", "Custom header to be passed along with each OTLP request. The value is expected in the format key=value."+
"Flag may be repeated to set multiple headers (e.g -otlp-header key1=value1 -otlp-header key2=value2)")

// custom resource attributes
c.ResourceAttributes = make(map[string]string)
fs.Var(&c.ResourceAttributes, "otlp-attributes", "Custom resource attributes to use. The value is expected in the format key=\"value\"."+
"Flag may be repeated to set multiple attributes (e.g -otlp-attributes key1=\"value1\" -otlp-attributes key2=\"value2\")")
}

// Run executes the test scenario.
func Run(c *Config, logger *zap.Logger) error {
atoulme marked this conversation as resolved.
Show resolved Hide resolved
if c.TotalDuration > 0 {
c.NumMetrics = 0
} else if c.NumMetrics <= 0 {
return fmt.Errorf("either `metrics` or `duration` must be greater than 0")
}

limit := rate.Limit(c.Rate)
if c.Rate == 0 {
limit = rate.Inf
logger.Info("generation of metrics isn't being throttled")
} else {
logger.Info("generation of metrics is limited", zap.Float64("per-second", float64(limit)))
}

wg := sync.WaitGroup{}
running := atomic.NewBool(true)

for i := 0; i < c.WorkerCount; i++ {
wg.Add(1)
w := worker{
numMetrics: c.NumMetrics,
limitPerSecond: limit,
totalDuration: c.TotalDuration,
running: running,
wg: &wg,
logger: logger.With(zap.Int("worker", i)),
}

go w.simulateMetrics()
}
if c.TotalDuration > 0 {
time.Sleep(c.TotalDuration)
running.Store(false)
}
wg.Wait()
return nil
}
63 changes: 63 additions & 0 deletions cmd/telemetrygen/internal/metrics/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2018 The Jaeger 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 metrics

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestKeyValueSet(t *testing.T) {
tests := []struct {
flag string
expected KeyValue
err error
}{
{
flag: "key=\"value\"",
expected: KeyValue(map[string]string{"key": "value"}),
},
{
flag: "key=\"\"",
expected: KeyValue(map[string]string{"key": ""}),
},
{
flag: "key=\"",
err: errDoubleQuotesOTLPAttributes,
},
{
flag: "key=value",
err: errDoubleQuotesOTLPAttributes,
},
{
flag: "key",
err: errFormatOTLPAttributes,
},
}

for _, tt := range tests {
t.Run(tt.flag, func(t *testing.T) {
kv := KeyValue(make(map[string]string))
err := kv.Set(tt.flag)
if err != nil || tt.err != nil {
assert.Equal(t, err, tt.err)
} else {
assert.Equal(t, tt.expected, kv)
}
})
}
}
Loading