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

Passing metrics unit when available #15

Merged
merged 1 commit into from
Jun 19, 2024
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
19 changes: 16 additions & 3 deletions pkg/opentelemetry/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,25 @@ func (o *Output) dispatch(entry metrics.Sample) error {
attributeSet := newAttributeSet(entry.Tags)
attributeSetOpt := otelMetric.WithAttributeSet(attributeSet)

unit := normalizeUnit(entry.Metric.Contains)

switch entry.Metric.Type {
case metrics.Counter:
counter, err := o.metricsRegistry.getOrCreateCounter(name)
counter, err := o.metricsRegistry.getOrCreateCounter(name, unit)
if err != nil {
return err
}

counter.Add(ctx, entry.Value, attributeSetOpt)
case metrics.Gauge:
gauge, err := o.metricsRegistry.getOrCreateGauge(name)
gauge, err := o.metricsRegistry.getOrCreateGauge(name, unit)
if err != nil {
return err
}

gauge.Set(entry.Value, attributeSet)
case metrics.Trend:
trend, err := o.metricsRegistry.getOrCreateHistogram(name)
trend, err := o.metricsRegistry.getOrCreateHistogram(name, unit)
if err != nil {
return err
}
Expand All @@ -190,3 +192,14 @@ func (o *Output) dispatch(entry metrics.Sample) error {
func normalizeMetricName(cfg Config, name string) string {
return cfg.MetricPrefix.String + name
}

func normalizeUnit(vt metrics.ValueType) string {
switch vt {
case metrics.Time:
return "ms"
case metrics.Data:
return "By"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "By"
return "byte"

Is it on purpose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's by purpose. OpenTelemetry follows the UCCM standard where bytes is By see open-telemetry/opentelemetry-specification#2973

default:
return ""
}
}
31 changes: 25 additions & 6 deletions pkg/opentelemetry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newRegistry(meter otelMetric.Meter, logger logrus.FieldLogger) *registry {
}
}

func (r *registry) getOrCreateCounter(name string) (otelMetric.Float64Counter, error) {
func (r *registry) getOrCreateCounter(name, unit string) (otelMetric.Float64Counter, error) {
if counter, ok := r.counters.Load(name); ok {
if v, ok := counter.(otelMetric.Float64Counter); ok {
return v, nil
Expand All @@ -36,7 +36,12 @@ func (r *registry) getOrCreateCounter(name string) (otelMetric.Float64Counter, e
return nil, fmt.Errorf("metric %q is not a counter", name)
}

c, err := r.meter.Float64Counter(name)
opts := []otelMetric.Float64CounterOption{}
if unit != "" {
opts = append(opts, otelMetric.WithUnit(unit))
}

c, err := r.meter.Float64Counter(name, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create counter for %q: %w", name, err)
}
Expand All @@ -47,7 +52,7 @@ func (r *registry) getOrCreateCounter(name string) (otelMetric.Float64Counter, e
return c, nil
}

func (r *registry) getOrCreateHistogram(name string) (otelMetric.Float64Histogram, error) {
func (r *registry) getOrCreateHistogram(name, unit string) (otelMetric.Float64Histogram, error) {
if histogram, ok := r.histograms.Load(name); ok {
if v, ok := histogram.(otelMetric.Float64Histogram); ok {
return v, nil
Expand All @@ -56,7 +61,12 @@ func (r *registry) getOrCreateHistogram(name string) (otelMetric.Float64Histogra
return nil, fmt.Errorf("metric %q is not a histogram", name)
}

h, err := r.meter.Float64Histogram(name)
opts := []otelMetric.Float64HistogramOption{}
if unit != "" {
opts = append(opts, otelMetric.WithUnit(unit))
}

h, err := r.meter.Float64Histogram(name, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create histogram for %q: %w", name, err)
}
Expand Down Expand Up @@ -115,7 +125,7 @@ func (r *registry) getOrCreateCountersForRate(name string) (otelMetric.Int64Coun
return nonZeroCounter, totalCounter, nil
}

func (r *registry) getOrCreateGauge(name string) (*Float64Gauge, error) {
func (r *registry) getOrCreateGauge(name, unit string) (*Float64Gauge, error) {
if gauge, ok := r.gauges.Load(name); ok {
if v, ok := gauge.(*Float64Gauge); ok {
return v, nil
Expand All @@ -125,7 +135,16 @@ func (r *registry) getOrCreateGauge(name string) (*Float64Gauge, error) {
}

g := NewFloat64Gauge()
_, err := r.meter.Float64ObservableGauge(name, otelMetric.WithFloat64Callback(g.Callback))

opts := []otelMetric.Float64ObservableGaugeOption{
otelMetric.WithFloat64Callback(g.Callback),
}

if unit != "" {
opts = append(opts, otelMetric.WithUnit(unit))
}

_, err := r.meter.Float64ObservableGauge(name, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create gauge for %q: %w", name, err)
}
Expand Down
Loading