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

[OASIS-37] Move collector internal/datadog to Agent as modules #25841

Merged
merged 5 commits into from
May 24, 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
7 changes: 7 additions & 0 deletions comp/otelcol/otlp/components/metricsclient/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

// Package metricsclient provides a statsd.ClientInterface implementation to datadog exporter and datadog connector
package metricsclient
29 changes: 29 additions & 0 deletions comp/otelcol/otlp/components/metricsclient/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/metricsclient

go 1.21.0

replace github.com/DataDog/datadog-agent/pkg/trace => ../../../../../pkg/trace

require (
github.com/DataDog/datadog-agent/pkg/trace v0.52.1
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.26.0
go.opentelemetry.io/otel/metric v1.26.0
go.opentelemetry.io/otel/sdk/metric v1.26.0
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/tools v0.16.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
83 changes: 83 additions & 0 deletions comp/otelcol/otlp/components/metricsclient/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 181 additions & 0 deletions comp/otelcol/otlp/components/metricsclient/metrics_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package metricsclient

import (
"context"
"strings"
"sync"
"time"

"github.com/DataDog/datadog-go/v5/statsd"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

const (
// ExporterSourceTag is the source tag for Datadog exporter
ExporterSourceTag = "datadogexporter"
// ConnectorSourceTag is the source tag for Datadog connector
ConnectorSourceTag = "datadogconnector"
)

type metricsClient struct {
meter metric.Meter
gauges map[string]float64
mutex sync.Mutex
source string
}

// InitializeMetricClient using a meter provider.
func InitializeMetricClient(mp metric.MeterProvider, source string) statsd.ClientInterface {
return &metricsClient{
meter: mp.Meter("datadog"),
gauges: make(map[string]float64),
source: source,
}
}

// Gauge implements the Statsd Gauge interface
func (m *metricsClient) Gauge(name string, value float64, tags []string, _ float64) error {
// The last parameter is rate, but we're omitting it because rate does not have effect for gauge points: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/dedd44436ae064f5a0b43769d24adf897533957b/receiver/statsdreceiver/internal/protocol/metric_translator.go#L153-L156
m.mutex.Lock()
defer m.mutex.Unlock()
if _, ok := m.gauges[name]; ok {
m.gauges[name] = value
return nil
}
m.gauges[name] = value
_, err := m.meter.Float64ObservableGauge(name, metric.WithFloat64Callback(func(_ context.Context, f metric.Float64Observer) error {
attr := m.attributeFromTags(tags)
m.mutex.Lock()
defer m.mutex.Unlock()
if v, ok := m.gauges[name]; ok {
f.Observe(v, metric.WithAttributeSet(attr))
}
return nil
}))
if err != nil {
return err
}

Check warning on line 61 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L60-L61

Added lines #L60 - L61 were not covered by tests
return nil
}

// Count implements the Statsd Count interface
func (m *metricsClient) Count(name string, value int64, tags []string, _ float64) error {
counter, err := m.meter.Int64Counter(name)
if err != nil {
return err
}

Check warning on line 70 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L69-L70

Added lines #L69 - L70 were not covered by tests
attr := m.attributeFromTags(tags)
counter.Add(context.Background(), value, metric.WithAttributeSet(attr))
return nil
}

func (m *metricsClient) attributeFromTags(tags []string) attribute.Set {
attr := make([]attribute.KeyValue, 0, len(tags)+1)
attr = append(attr, attribute.KeyValue{
Key: "source",
Value: attribute.StringValue(m.source),
})
for _, t := range tags {
kv := strings.Split(t, ":")
attr = append(attr, attribute.KeyValue{
Key: attribute.Key(kv[0]),
Value: attribute.StringValue(kv[1]),
})
}
return attribute.NewSet(attr...)
}

// Histogram implements the Statsd Histogram interface
func (m *metricsClient) Histogram(name string, value float64, tags []string, _ float64) error {
hist, err := m.meter.Float64Histogram(name)
if err != nil {
return err
}

Check warning on line 97 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L96-L97

Added lines #L96 - L97 were not covered by tests
attr := m.attributeFromTags(tags)
hist.Record(context.Background(), value, metric.WithAttributeSet(attr))
return nil
}

// Distribution implements the Statsd Distribution interface
func (m *metricsClient) Distribution(name string, value float64, tags []string, rate float64) error {
return m.Histogram(name, value, tags, rate)

Check warning on line 105 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}

// Timing implements the Statsd Timing interface
func (m *metricsClient) Timing(name string, value time.Duration, tags []string, rate float64) error {
return m.TimeInMilliseconds(name, value.Seconds()*1000, tags, rate)
}

// TimeInMilliseconds implements the Statsd TimeInMilliseconds interface
func (m *metricsClient) TimeInMilliseconds(name string, value float64, tags []string, rate float64) error {
return m.Histogram(name, value, tags, rate)
}

// Decr implements the Statsd Decr interface
func (m *metricsClient) Decr(name string, tags []string, rate float64) error {
return m.Count(name, -1, tags, rate)

Check warning on line 120 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L119-L120

Added lines #L119 - L120 were not covered by tests
}

// Incr implements the Statsd Incr interface
func (m *metricsClient) Incr(name string, tags []string, rate float64) error {
return m.Count(name, 1, tags, rate)

Check warning on line 125 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L124-L125

Added lines #L124 - L125 were not covered by tests
}

// Flush implements the Statsd Flush interface
func (m *metricsClient) Flush() error {
return nil

Check warning on line 130 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L129-L130

Added lines #L129 - L130 were not covered by tests
}

// Set implements the Statsd Set interface
func (m *metricsClient) Set(string, string, []string, float64) error {
return nil

Check warning on line 135 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}

// Event implements the Statsd Event interface
func (m *metricsClient) Event(*statsd.Event) error {
return nil

Check warning on line 140 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L139-L140

Added lines #L139 - L140 were not covered by tests
}

// SimpleEvent implements the Statsd SimpleEvent interface
func (m *metricsClient) SimpleEvent(string, string) error {
return nil

Check warning on line 145 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L144-L145

Added lines #L144 - L145 were not covered by tests
}

// ServiceCheck implements the Statsd ServiceCheck interface
func (m *metricsClient) ServiceCheck(*statsd.ServiceCheck) error {
return nil

Check warning on line 150 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L149-L150

Added lines #L149 - L150 were not covered by tests
}

// SimpleServiceCheck implements the Statsd SimpleServiceCheck interface
func (m *metricsClient) SimpleServiceCheck(string, statsd.ServiceCheckStatus) error {
return nil

Check warning on line 155 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}

// Close implements the Statsd Close interface
func (m *metricsClient) Close() error {
return nil

Check warning on line 160 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L159-L160

Added lines #L159 - L160 were not covered by tests
}

// IsClosed implements the Statsd IsClosed interface
func (m *metricsClient) IsClosed() bool {
return false

Check warning on line 165 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L164-L165

Added lines #L164 - L165 were not covered by tests
}

// GetTelemetry implements the Statsd GetTelemetry interface
func (m *metricsClient) GetTelemetry() statsd.Telemetry {
return statsd.Telemetry{}

Check warning on line 170 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L169-L170

Added lines #L169 - L170 were not covered by tests
}

// GaugeWithTimestamp implements the Statsd GaugeWithTimestamp interface
func (m *metricsClient) GaugeWithTimestamp(string, float64, []string, float64, time.Time) error {
return nil

Check warning on line 175 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L174-L175

Added lines #L174 - L175 were not covered by tests
}

// CountWithTimestamp implements the Statsd CountWithTimestamp interface
func (m *metricsClient) CountWithTimestamp(string, int64, []string, float64, time.Time) error {
return nil

Check warning on line 180 in comp/otelcol/otlp/components/metricsclient/metrics_client.go

View check run for this annotation

Codecov / codecov/patch

comp/otelcol/otlp/components/metricsclient/metrics_client.go#L179-L180

Added lines #L179 - L180 were not covered by tests
}
Loading
Loading