forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metrics client for trace agent (open-telemetry#31179)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> Implement `datadog-agent/pkg/trace/metrics.StatsClient` so that we can record telemetry metrics on the trace agent running in the Datadog Exporter and Datadog Connector. We implemented the StatsClient in OTel metric format so that the metrics can be recorded in the OTel Collector and picked up by the prometheus receiver under the `otelcol_` prefix. **Link to tracking Issue:** <Issue number if applicable> **Testing:** <Describe what testing was performed and which tests were added.> Added unit tests and tested change by running modified Collector with opentelemetry-demo and observing generated metrics. **Documentation:** <Describe the documentation added.> --------- Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
- Loading branch information
1 parent
643af70
commit 48d33fa
Showing
8 changed files
with
371 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: connector/datadogconnector | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Internal telemetry metrics for the Datadog traces exporter are now reported through the Collector's self-telemetry | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [31179] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | | ||
- These internal metrics may be dropped or change name without prior notice | ||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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,28 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: exporter/datadogexporter | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Internal telemetry metrics for the Datadog traces exporter are now reported through the Collector's self-telemetry | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [31179] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | | ||
- These internal metrics may be dropped or change name without prior notice | ||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,98 @@ | ||
// 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 | ||
gauges map[string]float64 | ||
mutex sync.Mutex | ||
} | ||
|
||
var initializeOnce sync.Once | ||
|
||
// InitializeMetricClient using a meter provider. If the client has already been initialized, | ||
// this function just returns the previous one. | ||
func InitializeMetricClient(mp metric.MeterProvider) metrics.StatsClient { | ||
initializeOnce.Do(func() { | ||
m := &metricsClient{ | ||
meter: mp.Meter("datadog"), | ||
gauges: make(map[string]float64), | ||
} | ||
metrics.Client = m | ||
}) | ||
return metrics.Client | ||
} | ||
|
||
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 := attributeFromTags(tags) | ||
if v, ok := m.gauges[name]; ok { | ||
f.Observe(v, metric.WithAttributeSet(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.WithAttributeSet(attr)) | ||
return nil | ||
} | ||
|
||
func attributeFromTags(tags []string) attribute.Set { | ||
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 attribute.NewSet(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.WithAttributeSet(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 | ||
} |
Oops, something went wrong.