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
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
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
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions cmd/telemetrygen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/collector/semconv v0.71.0
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/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
Expand All @@ -29,6 +33,8 @@ 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/otel/metric 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
Expand Down
12 changes: 12 additions & 0 deletions cmd/telemetrygen/go.sum

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

102 changes: 102 additions & 0 deletions cmd/telemetrygen/internal/common/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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://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 common

import (
"fmt"
"strings"
"time"

"github.com/spf13/pflag"
"go.opentelemetry.io/otel/attribute"
)

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

type KeyValue map[string]string

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"
}

type Config struct {
WorkerCount int
Rate int64
TotalDuration time.Duration
ReportingInterval time.Duration

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

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

if len(c.ResourceAttributes) > 0 {
for k, v := range c.ResourceAttributes {
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")
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\")")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package traces
package common

import (
"testing"
Expand Down
34 changes: 34 additions & 0 deletions cmd/telemetrygen/internal/common/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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://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 common

import (
"fmt"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.uber.org/zap"
)

// CreateLogger creates a logger for use by telemetrygen
func CreateLogger() (*zap.Logger, error) {
logger, err := zap.NewDevelopment()
if err != nil {
return nil, fmt.Errorf("failed to obtain logger: %w", err)
}
grpcZap.ReplaceGrpcLoggerV2(logger.WithOptions(
zap.AddCallerSkip(3),
))
return logger, err
}
33 changes: 33 additions & 0 deletions cmd/telemetrygen/internal/metrics/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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://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 (
"github.com/spf13/pflag"

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

// Config describes the test scenario.
type Config struct {
atoulme marked this conversation as resolved.
Show resolved Hide resolved
common.Config
NumMetrics int
}

// Flags registers config flags.
func (c *Config) Flags(fs *pflag.FlagSet) {
c.CommonFlags(fs)
fs.IntVar(&c.NumMetrics, "metrics", 1, "Number of metrics to generate in each worker (ignored if duration is provided)")
}
Loading