diff --git a/README.md b/README.md index 711473c..438b7b3 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,6 @@ WIP ### post metrics ```sh -otlc metrics post --conf ./otlc.yaml --name awesome_gauge --attributes hoge=poyo,fuga=1 --value 123.45 +otlc metrics post --conf ./otlc.yaml --name awesome_gauge --value 123.45 \ +--resource-attrs service.name=otlc --datapoint-attrs hoge=poyo,fuga=1 ``` diff --git a/cmd/metrics_post.go b/cmd/metrics_post.go index f90c40c..f07fa97 100644 --- a/cmd/metrics_post.go +++ b/cmd/metrics_post.go @@ -25,16 +25,28 @@ func NewMetricsPostCmd() *cobra.Command { if err != nil { log.Fatalln(err) } - attributes, err := cmd.Flags().GetStringToString("attributes") + rattrs, err := cmd.Flags().GetStringToString("resource-attrs") + if err != nil { + log.Fatalln(err) + } + /* + sattrs, err := cmd.Flags().GetStringToString("scope-attrs") + if err != nil { + log.Fatalln(err) + } + */ + dattrs, err := cmd.Flags().GetStringToString("datapoint-attrs") if err != nil { log.Fatalln(err) } p := metrics.NewPoster(config.Endpoint, config.Headers) if err := p.Post(&metrics.PostParams{ - Name: name, - Description: description, - DataPointAttrs: attributes, + Name: name, + Description: description, + ResourceAttrs: rattrs, + // ScopeAttrs: sattrs, + DataPointAttrs: dattrs, DataPointValue: value, }); err != nil { log.Fatalln(err) @@ -46,7 +58,9 @@ func NewMetricsPostCmd() *cobra.Command { cmd.Flags().StringP("type", "t", "gauge", "metric value type") cmd.Flags().StringP("name", "n", "", "metric name") cmd.Flags().StringP("description", "d", "", "metric description") - cmd.Flags().StringToStringP("attributes", "a", nil, "metric datapoint attributes. format: key1=value1,key2=value2") + cmd.Flags().StringToString("resource-attrs", nil, "metric resource attributes. format: key1=value1,key2=value2") + // cmd.Flags().StringToString("scope-attrs", nil, "metric scope attributes. format: key1=value1,key2=value2") + cmd.Flags().StringToString("datapoint-attrs", nil, "metric datapoint attributes. format: key1=value1,key2=value2") if err := cmd.MarkFlagRequired("value"); err != nil { panic(err) } diff --git a/go.mod b/go.mod index ae151ca..280a485 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( go.opentelemetry.io/otel v1.16.0 go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.39.0 go.opentelemetry.io/otel/metric v1.16.0 + go.opentelemetry.io/otel/sdk v1.16.0 go.opentelemetry.io/otel/sdk/metric v0.39.0 google.golang.org/grpc v1.55.0 ) @@ -31,7 +32,6 @@ require ( github.com/subosito/gotenv v1.4.2 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect - go.opentelemetry.io/otel/sdk v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect golang.org/x/net v0.10.0 // indirect diff --git a/metrics/post.go b/metrics/post.go index dcb69a5..ff9b4a1 100644 --- a/metrics/post.go +++ b/metrics/post.go @@ -8,6 +8,7 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/metric" sdkMetric "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/resource" _ "google.golang.org/grpc/encoding/gzip" ) @@ -27,6 +28,8 @@ func NewPoster(endpoint string, headers map[string]string) *Poster { type PostParams struct { Name string Description string + ResourceAttrs map[string]string + ScopeAttrs map[string]string DataPointAttrs map[string]string DataPointValue float64 } @@ -42,8 +45,16 @@ func (p *Poster) Post(params *PostParams) error { return err } + res := resource.NewWithAttributes( + "", + convertMapToAttrs(params.ResourceAttrs)..., + ) + reader := sdkMetric.NewPeriodicReader(exporter) - provider := sdkMetric.NewMeterProvider(sdkMetric.WithReader(reader)) + provider := sdkMetric.NewMeterProvider( + sdkMetric.WithReader(reader), + sdkMetric.WithResource(res), + ) defer func() { if err := provider.Shutdown(ctx); err != nil { log.Fatalln(err) @@ -51,24 +62,38 @@ func (p *Poster) Post(params *PostParams) error { log.Println("🚀 posted.") }() - attrs := make([]attribute.KeyValue, 0, len(params.DataPointAttrs)) - for k, v := range params.DataPointAttrs { - attr := attribute.KeyValue{ - Key: attribute.Key(k), - Value: attribute.StringValue(v), - } - attrs = append(attrs, attr) - } - - meter := provider.Meter("github.com/Arthur1/otlc") + meter := provider.Meter( + "github.com/Arthur1/otlc", + /* + metric.WithInstrumentationAttributes( + convertMapToAttrs(params.ScopeAttrs)..., + ), + */ + ) _, err = meter.Float64ObservableGauge( params.Name, metric.WithDescription(params.Description), metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error { - attrs := metric.WithAttributes(attrs...) - o.Observe(params.DataPointValue, attrs) + o.Observe( + params.DataPointValue, + metric.WithAttributes( + convertMapToAttrs(params.DataPointAttrs)..., + ), + ) return nil }), ) return err } + +func convertMapToAttrs(attrsMap map[string]string) []attribute.KeyValue { + attrs := make([]attribute.KeyValue, 0, len(attrsMap)) + for k, v := range attrsMap { + attr := attribute.KeyValue{ + Key: attribute.Key(k), + Value: attribute.StringValue(v), + } + attrs = append(attrs, attr) + } + return attrs +}