-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package datadog // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog" | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/trace/metrics" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
type metricsClient struct { | ||
meter metric.Meter | ||
guages map[string]float64 | ||
mutex sync.Mutex | ||
} | ||
|
||
// NewMetricClient returns a new metrics client | ||
func NewMetricClient(meter metric.Meter) metrics.StatsClient { | ||
// We need to lock the mutex here to avoid | ||
// duplicate metrics being created by both datadogconnector and exporter | ||
var mutex sync.Mutex | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
m := &metricsClient{ | ||
meter: meter, | ||
guages: make(map[string]float64), | ||
} | ||
metrics.Client = m | ||
return m | ||
} | ||
|
||
func (m *metricsClient) Gauge(name string, value float64, tags []string, _ float64) error { | ||
m.mutex.Lock() | ||
if _, ok := m.guages[name]; ok { | ||
m.guages[name] = value | ||
return nil | ||
} | ||
m.guages[name] = value | ||
m.mutex.Unlock() | ||
_, err := m.meter.Float64ObservableGauge(name, metric.WithFloat64Callback(func(_ context.Context, f metric.Float64Observer) error { | ||
attr := attributeFromTags(tags) | ||
if v, ok := m.guages[name]; ok { | ||
f.Observe(v, metric.WithAttributes(attr...)) | ||
} | ||
return nil | ||
})) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (m *metricsClient) Count(name string, value int64, tags []string, _ float64) error { | ||
counter, err := m.meter.Int64Counter(name) | ||
if err != nil { | ||
return err | ||
} | ||
attr := attributeFromTags(tags) | ||
counter.Add(context.Background(), value, metric.WithAttributes(attr...)) | ||
return nil | ||
} | ||
|
||
func attributeFromTags(tags []string) []attribute.KeyValue { | ||
attr := make([]attribute.KeyValue, 0, len(tags)) | ||
for _, t := range tags { | ||
kv := strings.Split(t, ":") | ||
attr = append(attr, attribute.KeyValue{ | ||
Key: attribute.Key(kv[0]), | ||
Value: attribute.StringValue(kv[1]), | ||
}) | ||
} | ||
return attr | ||
} | ||
|
||
func (m *metricsClient) Histogram(name string, value float64, tags []string, _ float64) error { | ||
hist, err := m.meter.Float64Histogram(name) | ||
if err != nil { | ||
return err | ||
} | ||
attr := attributeFromTags(tags) | ||
hist.Record(context.Background(), value, metric.WithAttributes(attr...)) | ||
return nil | ||
} | ||
|
||
func (m *metricsClient) Timing(name string, value time.Duration, tags []string, rate float64) error { | ||
return m.Histogram(name, float64(value.Milliseconds()), tags, rate) | ||
} | ||
|
||
func (m *metricsClient) Flush() error { | ||
return nil | ||
} |