Skip to content

Commit

Permalink
[pkg/batchperresourceattr] deprecate in favor of internal/batchperres…
Browse files Browse the repository at this point in the history
…ourceattr
  • Loading branch information
atoulme committed Jan 18, 2025
1 parent 20ecaff commit 85bfc23
Show file tree
Hide file tree
Showing 30 changed files with 922 additions and 19 deletions.
27 changes: 27 additions & 0 deletions .chloggen/deprecate_pkg_batchperresourceattr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/batchperresourceattr

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate in favor of internal/batchperresourceattr

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [15372]

# (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:

# 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: [api]
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extension/storage/redisstorageextension/ @open-telemetry
extension/sumologicextension/ @open-telemetry/collector-contrib-approvers @rnishtala-sumo @chan-tim-sumo

internal/aws/ @open-telemetry/collector-contrib-approvers @Aneurysm9 @mxiamxia
internal/batchperresourceattr/ @open-telemetry/collector-contrib-approvers @atoulme @dmitryax
internal/collectd/ @open-telemetry/collector-contrib-approvers @atoulme
internal/coreinternal/ @open-telemetry/collector-contrib-approvers @open-telemetry/collector-approvers
internal/docker/ @open-telemetry/collector-contrib-approvers @jamesmoessis @MovieStoreGuy
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ body:
- extension/storage/redisstorage
- extension/sumologic
- internal/aws
- internal/batchperresourceattr
- internal/collectd
- internal/core
- internal/docker
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ body:
- extension/storage/redisstorage
- extension/sumologic
- internal/aws
- internal/batchperresourceattr
- internal/collectd
- internal/core
- internal/docker
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ body:
- extension/storage/redisstorage
- extension/sumologic
- internal/aws
- internal/batchperresourceattr
- internal/collectd
- internal/core
- internal/docker
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/unmaintained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ body:
- extension/storage/redisstorage
- extension/sumologic
- internal/aws
- internal/batchperresourceattr
- internal/collectd
- internal/core
- internal/docker
Expand Down
2 changes: 1 addition & 1 deletion cmd/otelcontribcol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ replaces:
- github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/k8s => ../../internal/aws/k8s
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver => ../../receiver/mysqlreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/extension/asapauthextension => ../../extension/asapauthextension
- github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../pkg/batchperresourceattr
- github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr => ../../internal/batchperresourceattr
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver => ../../receiver/vcenterreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver => ../../receiver/statsdreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter => ../../exporter/prometheusremotewriteexporter
Expand Down
2 changes: 1 addition & 1 deletion cmd/oteltestbedcol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ replaces:
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver => ../../receiver/sapmreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver => ../../receiver/zipkinreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver => ../../receiver/jaegerreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../pkg/batchperresourceattr
- github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr => ../../internal/batchperresourceattr
- github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter => ../../exporter/prometheusremotewriteexporter
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcplogreceiver => ../../receiver/tcplogreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/extension/pprofextension => ../../extension/pprofextension
Expand Down
2 changes: 0 additions & 2 deletions exporter/elasticsearchexporter/integrationtest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/splun

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter => ../../signalfxexporter

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../../pkg/batchperresourceattr

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver => ../../../receiver/datadogreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sapmexporter => ../../sapmexporter
Expand Down
2 changes: 1 addition & 1 deletion exporter/sapmexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
)

Expand Down
4 changes: 2 additions & 2 deletions exporter/sapmexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/jaegertracing/jaeger v1.65.0
github.com/klauspost/compress v1.17.11
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger v0.117.0
github.com/signalfx/sapm-proto v0.17.0
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -79,7 +79,7 @@ require (

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk => ../../internal/splunk

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../pkg/batchperresourceattr
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr => ../../internal/batchperresourceattr

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger => ../../pkg/translator/jaeger

Expand Down
2 changes: 1 addition & 1 deletion exporter/signalfxexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/correlation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions exporter/signalfxexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.117.1-0.20250117002813-e970f8bb1258
Expand Down Expand Up @@ -106,7 +106,7 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/corei

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk => ../../internal/splunk

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../pkg/batchperresourceattr
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr => ../../internal/batchperresourceattr

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ../../pkg/experimentalmetricmetadata

Expand Down
2 changes: 1 addition & 1 deletion exporter/splunkhecexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions exporter/splunkhecexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/goccy/go-json v0.10.4
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.117.1-0.20250117002813-e970f8bb1258
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -123,7 +123,7 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/corei

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk => ../../internal/splunk

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../pkg/batchperresourceattr
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr => ../../internal/batchperresourceattr

retract (
v0.76.2
Expand Down
1 change: 1 addition & 0 deletions exporter/splunkhecexporter/go.sum

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

1 change: 1 addition & 0 deletions internal/batchperresourceattr/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
202 changes: 202 additions & 0 deletions internal/batchperresourceattr/batchperresourceattr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package batchperresourceattr // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/batchperresourceattr"

import (
"context"
"fmt"

"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
)

var separator = string([]byte{0x0, 0x1})

type batchTraces struct {
attrKeys []string
next consumer.Traces
}

func NewBatchPerResourceTraces(attrKey string, next consumer.Traces) consumer.Traces {
return &batchTraces{
attrKeys: []string{attrKey},
next: next,
}
}

func NewMultiBatchPerResourceTraces(attrKeys []string, next consumer.Traces) consumer.Traces {
return &batchTraces{
attrKeys: attrKeys,
next: next,
}
}

// Capabilities returns the capabilities of the next consumer because batchTraces doesn't mutate data itself.
func (bt *batchTraces) Capabilities() consumer.Capabilities {
return bt.next.Capabilities()
}

func (bt *batchTraces) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
rss := td.ResourceSpans()
lenRss := rss.Len()
// If zero or one resource spans just call next.
if lenRss <= 1 {
return bt.next.ConsumeTraces(ctx, td)
}

indicesByAttr := make(map[string][]int)
for i := 0; i < lenRss; i++ {
rs := rss.At(i)
var attrVal string

for _, k := range bt.attrKeys {
if attributeValue, ok := rs.Resource().Attributes().Get(k); ok {
attrVal = fmt.Sprintf("%s%s%s", attrVal, separator, attributeValue.Str())
}
}

indicesByAttr[attrVal] = append(indicesByAttr[attrVal], i)
}
// If there is a single attribute value, then call next.
if len(indicesByAttr) <= 1 {
return bt.next.ConsumeTraces(ctx, td)
}

// Build the resource spans for each attribute value using CopyTo and call next for each one.
var errs error
for _, indices := range indicesByAttr {
tracesForAttr := ptrace.NewTraces()
for _, i := range indices {
rs := rss.At(i)
rs.CopyTo(tracesForAttr.ResourceSpans().AppendEmpty())
}
errs = multierr.Append(errs, bt.next.ConsumeTraces(ctx, tracesForAttr))
}
return errs
}

type batchMetrics struct {
attrKeys []string
next consumer.Metrics
}

func NewBatchPerResourceMetrics(attrKey string, next consumer.Metrics) consumer.Metrics {
return &batchMetrics{
attrKeys: []string{attrKey},
next: next,
}
}

func NewMultiBatchPerResourceMetrics(attrKeys []string, next consumer.Metrics) consumer.Metrics {
return &batchMetrics{
attrKeys: attrKeys,
next: next,
}
}

// Capabilities returns the capabilities of the next consumer because batchMetrics doesn't mutate data itself.
func (bt *batchMetrics) Capabilities() consumer.Capabilities {
return bt.next.Capabilities()
}

func (bt *batchMetrics) ConsumeMetrics(ctx context.Context, td pmetric.Metrics) error {
rms := td.ResourceMetrics()
lenRms := rms.Len()
// If zero or one resource metrics just call next.
if lenRms <= 1 {
return bt.next.ConsumeMetrics(ctx, td)
}

indicesByAttr := make(map[string][]int)
for i := 0; i < lenRms; i++ {
rm := rms.At(i)
var attrVal string
for _, k := range bt.attrKeys {
if attributeValue, ok := rm.Resource().Attributes().Get(k); ok {
attrVal = fmt.Sprintf("%s%s%s", attrVal, separator, attributeValue.Str())
}
}
indicesByAttr[attrVal] = append(indicesByAttr[attrVal], i)
}
// If there is a single attribute value, then call next.
if len(indicesByAttr) <= 1 {
return bt.next.ConsumeMetrics(ctx, td)
}

// Build the resource metrics for each attribute value using CopyTo and call next for each one.
var errs error
for _, indices := range indicesByAttr {
metricsForAttr := pmetric.NewMetrics()
for _, i := range indices {
rm := rms.At(i)
rm.CopyTo(metricsForAttr.ResourceMetrics().AppendEmpty())
}
errs = multierr.Append(errs, bt.next.ConsumeMetrics(ctx, metricsForAttr))
}
return errs
}

type batchLogs struct {
attrKeys []string
next consumer.Logs
}

func NewBatchPerResourceLogs(attrKey string, next consumer.Logs) consumer.Logs {
return &batchLogs{
attrKeys: []string{attrKey},
next: next,
}
}

func NewMultiBatchPerResourceLogs(attrKeys []string, next consumer.Logs) consumer.Logs {
return &batchLogs{
attrKeys: attrKeys,
next: next,
}
}

// Capabilities returns the capabilities of the next consumer because batchLogs doesn't mutate data itself.
func (bt *batchLogs) Capabilities() consumer.Capabilities {
return bt.next.Capabilities()
}

func (bt *batchLogs) ConsumeLogs(ctx context.Context, td plog.Logs) error {
rls := td.ResourceLogs()
lenRls := rls.Len()
// If zero or one resource logs just call next.
if lenRls <= 1 {
return bt.next.ConsumeLogs(ctx, td)
}

indicesByAttr := make(map[string][]int)
for i := 0; i < lenRls; i++ {
rl := rls.At(i)
var attrVal string
for _, k := range bt.attrKeys {
if attributeValue, ok := rl.Resource().Attributes().Get(k); ok {
attrVal = fmt.Sprintf("%s%s%s", attrVal, separator, attributeValue.Str())
}
}
indicesByAttr[attrVal] = append(indicesByAttr[attrVal], i)
}
// If there is a single attribute value, then call next.
if len(indicesByAttr) <= 1 {
return bt.next.ConsumeLogs(ctx, td)
}

// Build the resource logs for each attribute value using CopyTo and call next for each one.
var errs error
for _, indices := range indicesByAttr {
logsForAttr := plog.NewLogs()
for _, i := range indices {
rl := rls.At(i)
rl.CopyTo(logsForAttr.ResourceLogs().AppendEmpty())
}
errs = multierr.Append(errs, bt.next.ConsumeLogs(ctx, logsForAttr))
}
return errs
}
Loading

0 comments on commit 85bfc23

Please sign in to comment.