Skip to content

Commit

Permalink
set resouce attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur1 committed Jul 22, 2023
1 parent cd7476f commit f445ddf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
24 changes: 19 additions & 5 deletions cmd/metrics_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down
51 changes: 38 additions & 13 deletions metrics/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand All @@ -42,33 +45,55 @@ 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)
}
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
}

0 comments on commit f445ddf

Please sign in to comment.