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

[chore] [receiver/datadog] Add support for v1 series #33957

Merged
merged 11 commits into from
Jul 17, 2024
119 changes: 119 additions & 0 deletions receiver/datadogreceiver/batcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package datadogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver"

import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity"
)

type Batcher struct {
Copy link
Member

Choose a reason for hiding this comment

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

this should probably be inside an internal package, otherwise it's part of this component's public API. Unless you meant to expose this to external consumers?

pmetric.Metrics

resourceMetrics map[identity.Resource]pmetric.ResourceMetrics
scopeMetrics map[identity.Scope]pmetric.ScopeMetrics
metrics map[identity.Metric]pmetric.Metric
}

func newBatcher() Batcher {
return Batcher{
Metrics: pmetric.NewMetrics(),
resourceMetrics: make(map[identity.Resource]pmetric.ResourceMetrics),
scopeMetrics: make(map[identity.Scope]pmetric.ScopeMetrics),
metrics: make(map[identity.Metric]pmetric.Metric),
}
}

// Dimensions stores the properties of the series that are needed in order
// to unique identify the series. This is needed in order to batch metrics by
// resource, scope, and datapoint attributes
type Dimensions struct {
Copy link
Member

Choose a reason for hiding this comment

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

Same here

name string
metricType pmetric.MetricType
resourceAttrs pcommon.Map
scopeAttrs pcommon.Map
dpAttrs pcommon.Map
buildInfo string
}

var metricTypeMap = map[string]pmetric.MetricType{
"count": pmetric.MetricTypeSum,
"gauge": pmetric.MetricTypeGauge,
"rate": pmetric.MetricTypeSum,
"service_check": pmetric.MetricTypeGauge,
"sketch": pmetric.MetricTypeExponentialHistogram,
}

func parseSeriesProperties(name string, metricType string, tags []string, host string, version string, stringPool *StringPool) Dimensions {
resourceAttrs, scopeAttrs, dpAttrs := tagsToAttributes(tags, host, stringPool)
return Dimensions{
name: name,
metricType: metricTypeMap[metricType],
buildInfo: version,
resourceAttrs: resourceAttrs,
scopeAttrs: scopeAttrs,
dpAttrs: dpAttrs,
}
}

func (b Batcher) Lookup(dim Dimensions) (pmetric.Metric, identity.Metric) {
resource := dim.Resource()
resourceID := identity.OfResource(resource)
resourceMetrics, ok := b.resourceMetrics[resourceID]
if !ok {
resourceMetrics = b.Metrics.ResourceMetrics().AppendEmpty()
resource.MoveTo(resourceMetrics.Resource())
b.resourceMetrics[resourceID] = resourceMetrics
}

scope := dim.Scope()
scopeID := identity.OfScope(resourceID, scope)
scopeMetrics, ok := b.scopeMetrics[scopeID]
if !ok {
scopeMetrics = resourceMetrics.ScopeMetrics().AppendEmpty()
scope.MoveTo(scopeMetrics.Scope())
b.scopeMetrics[scopeID] = scopeMetrics
}

m := dim.Metric()
metricID := identity.OfMetric(scopeID, m)
metric, ok := b.metrics[metricID]
if !ok {
metric = scopeMetrics.Metrics().AppendEmpty()
m.MoveTo(metric)
b.metrics[metricID] = metric
}

return metric, metricID
}

func (d Dimensions) Resource() pcommon.Resource {
resource := pcommon.NewResource()
d.resourceAttrs.CopyTo(resource.Attributes()) // TODO(jesus.vazquez) review this copy
return resource
}

func (d Dimensions) Scope() pcommon.InstrumentationScope {
scope := pcommon.NewInstrumentationScope()
scope.SetName("otelcol/datadogreceiver")
scope.SetVersion(d.buildInfo)
d.scopeAttrs.CopyTo(scope.Attributes())
return scope
}

func (d Dimensions) Metric() pmetric.Metric {
metric := pmetric.NewMetric()
metric.SetName(d.name)
switch d.metricType {
case pmetric.MetricTypeSum:
metric.SetEmptySum()
case pmetric.MetricTypeGauge:
metric.SetEmptyGauge()
case pmetric.MetricTypeExponentialHistogram:
metric.SetEmptyExponentialHistogram()
}
return metric
}
Loading