From 0b9247765708da557ba85fa2f375427e4a875880 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 8 Feb 2023 22:19:53 -0800 Subject: [PATCH] use counter --- cmd/telemetrygen/internal/common/config.go | 22 ++++++++++++++++++++ cmd/telemetrygen/internal/metrics/config.go | 23 ++------------------- cmd/telemetrygen/internal/metrics/worker.go | 7 ++++--- cmd/telemetrygen/internal/traces/config.go | 20 ++---------------- 4 files changed, 30 insertions(+), 42 deletions(-) diff --git a/cmd/telemetrygen/internal/common/config.go b/cmd/telemetrygen/internal/common/config.go index ccb13dc9fff7..ff5b188192bb 100644 --- a/cmd/telemetrygen/internal/common/config.go +++ b/cmd/telemetrygen/internal/common/config.go @@ -78,3 +78,25 @@ func (c *Config) GetAttributes() []attribute.KeyValue { } 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\")") +} diff --git a/cmd/telemetrygen/internal/metrics/config.go b/cmd/telemetrygen/internal/metrics/config.go index 54c433e1f061..7cc3d69742f7 100644 --- a/cmd/telemetrygen/internal/metrics/config.go +++ b/cmd/telemetrygen/internal/metrics/config.go @@ -15,8 +15,6 @@ package metrics import ( - "time" - "github.com/spf13/pflag" "github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common" @@ -30,23 +28,6 @@ type Config struct { // Flags registers config flags. func (c *Config) Flags(fs *pflag.FlagSet) { - fs.IntVar(&c.WorkerCount, "workers", 1, "Number of workers (goroutines) to run") - 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\")") + c.CommonFlags(fs) + fs.IntVar(&c.NumMetrics, "metrics", 1, "Number of metrics to generate in each worker (ignored if duration is provided)") } diff --git a/cmd/telemetrygen/internal/metrics/worker.go b/cmd/telemetrygen/internal/metrics/worker.go index 6f95ee0f1a75..4ab3566e8dae 100644 --- a/cmd/telemetrygen/internal/metrics/worker.go +++ b/cmd/telemetrygen/internal/metrics/worker.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/metrics" +package metrics import ( "context" @@ -45,9 +45,9 @@ func (w worker) simulateMetrics() { max = int64(w.limitPerSecond) } - gauge, _ := meter.Int64ObservableGauge("gen") + counter, _ := meter.Int64Counter("gen") for w.running.Load() { - gauge(int64(i)) + counter.Add(context.Background(), 1) if err := limiter.Wait(context.Background()); err != nil { w.logger.Fatal("limiter waited failed, retry", zap.Error(err)) } @@ -63,6 +63,7 @@ func (w worker) simulateMetrics() { } } } + w.logger.Info("metrics generated", zap.Int("metrics", i)) w.wg.Done() } diff --git a/cmd/telemetrygen/internal/traces/config.go b/cmd/telemetrygen/internal/traces/config.go index a032bf5ddca4..a91765cba2f5 100644 --- a/cmd/telemetrygen/internal/traces/config.go +++ b/cmd/telemetrygen/internal/traces/config.go @@ -30,24 +30,8 @@ type Config struct { // Flags registers config flags. func (c *Config) Flags(fs *pflag.FlagSet) { - fs.IntVar(&c.WorkerCount, "workers", 1, "Number of workers (goroutines) to run") - fs.IntVar(&c.NumTraces, "traces", 1, "Number of traces to generate in each worker (ignored if duration is provided") + c.CommonFlags(fs) + fs.IntVar(&c.NumTraces, "traces", 1, "Number of traces to generate in each worker (ignored if duration is provided)") fs.BoolVar(&c.PropagateContext, "marshal", false, "Whether to marshal trace context via HTTP headers") - fs.Int64Var(&c.Rate, "rate", 0, "Approximately how many traces per second each worker should generate. Zero means no throttling.") - fs.DurationVar(&c.TotalDuration, "duration", 0, "For how long to run the test") fs.StringVar(&c.ServiceName, "service", "telemetrygen", "Service name to use") - - fs.StringVar(&c.Endpoint, "otlp-endpoint", "localhost:4317", "Target to which the exporter is going to send spans or metrics. This MAY be configured to include a path (e.g. example.com/v1/traces)") - 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\")") }