Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
27 changes: 27 additions & 0 deletions .chloggen/mat-rumian-lbe-imp.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: enhancement

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Optimize metrics and traces export"

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

# (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: []
85 changes: 85 additions & 0 deletions exporter/loadbalancingexporter/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package loadbalancingexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter"

import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)

// mergeTraces concatenates multiple ptrace.Traces into a single ptrace.Traces.
func mergeTraces(traces ...ptrace.Traces) ptrace.Traces {
merged := ptrace.NewTraces()
empty := ptrace.Traces{}

for _, trace := range traces {

if trace == empty {
continue
}

for i := 0; i < trace.ResourceSpans().Len(); i++ {
rs := trace.ResourceSpans().At(i)

newRS := merged.ResourceSpans().AppendEmpty()
rs.Resource().CopyTo(newRS.Resource())
newRS.SetSchemaUrl(rs.SchemaUrl())

for j := 0; j < rs.ScopeSpans().Len(); j++ {
ils := rs.ScopeSpans().At(j)

newILS := newRS.ScopeSpans().AppendEmpty()
ils.Scope().CopyTo(newILS.Scope())
newILS.SetSchemaUrl(ils.SchemaUrl())

for k := 0; k < ils.Spans().Len(); k++ {
span := ils.Spans().At(k)
newSpan := newILS.Spans().AppendEmpty()
span.CopyTo(newSpan)
}
}
}

}

return merged
}

// mergeTraces concatenates multiple pmetric.Metrics into a single pmetric.Metrics.
func mergeMetrics(metrics ...pmetric.Metrics) pmetric.Metrics {
merged := pmetric.NewMetrics()
empty := pmetric.Metrics{}

for _, metric := range metrics {

if metric == empty {
continue
}

for i := 0; i < metric.ResourceMetrics().Len(); i++ {
rs := metric.ResourceMetrics().At(i)

newRM := merged.ResourceMetrics().AppendEmpty()
rs.Resource().CopyTo(newRM.Resource())
newRM.SetSchemaUrl(rs.SchemaUrl())

for j := 0; j < rs.ScopeMetrics().Len(); j++ {
ilm := rs.ScopeMetrics().At(j)

newILM := newRM.ScopeMetrics().AppendEmpty()
ilm.Scope().CopyTo(newILM.Scope())
newILM.SetSchemaUrl(ilm.SchemaUrl())

for k := 0; k < ilm.Metrics().Len(); k++ {
m := ilm.Metrics().At(k)
newMetric := newILM.Metrics().AppendEmpty()
m.CopyTo(newMetric)
}
}
}

}

return merged
}
121 changes: 121 additions & 0 deletions exporter/loadbalancingexporter/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package loadbalancingexporter

import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)

func TestMergeTracesTwoEmpty(t *testing.T) {
expectedEmpty := ptrace.NewTraces()
trace1 := ptrace.Traces{}
trace2 := ptrace.Traces{}

mergedTraces := mergeTraces(trace1, trace2)

require.Equal(t, expectedEmpty, mergedTraces)
}

func TestMergeTracesSingleEmpty(t *testing.T) {
expectedTraces := simpleTraces()

trace1 := ptrace.Traces{}
trace2 := simpleTraces()

mergedTraces := mergeTraces(trace1, trace2)

require.Equal(t, expectedTraces, mergedTraces)
}

func TestMergeTraces(t *testing.T) {
expectedTraces := ptrace.NewTraces()
expectedTraces.ResourceSpans().EnsureCapacity(3)
aspans := expectedTraces.ResourceSpans().AppendEmpty()
aspans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-1")
aspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetTraceID([16]byte{1, 2, 3, 4})
bspans := expectedTraces.ResourceSpans().AppendEmpty()
bspans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-2")
bspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetTraceID([16]byte{1, 2, 3, 2})
cspans := expectedTraces.ResourceSpans().AppendEmpty()
cspans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-3")
cspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetTraceID([16]byte{1, 2, 3, 3})

trace1 := ptrace.NewTraces()
trace1.ResourceSpans().EnsureCapacity(2)
t1aspans := trace1.ResourceSpans().AppendEmpty()
t1aspans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-1")
t1aspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetTraceID([16]byte{1, 2, 3, 4})
t1bspans := trace1.ResourceSpans().AppendEmpty()
t1bspans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-2")
t1bspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetTraceID([16]byte{1, 2, 3, 2})

trace2 := ptrace.NewTraces()
trace2.ResourceSpans().EnsureCapacity(1)
t2cspans := trace2.ResourceSpans().AppendEmpty()
t2cspans.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-3")
t2cspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetTraceID([16]byte{1, 2, 3, 3})

mergedTraces := mergeTraces(trace1, trace2)

require.Equal(t, expectedTraces, mergedTraces)
}

func TestMergeMetricsTwoEmpty(t *testing.T) {
expectedEmpty := pmetric.NewMetrics()
metric1 := pmetric.Metrics{}
metric2 := pmetric.Metrics{}

mergedMetrics := mergeMetrics(metric1, metric2)

require.Equal(t, expectedEmpty, mergedMetrics)
}

func TestMergeMetricsSingleEmpty(t *testing.T) {
expectedMetrics := simpleMetricsWithResource()

metric1 := pmetric.Metrics{}
metric2 := simpleMetricsWithResource()

mergedMetrics := mergeMetrics(metric1, metric2)

require.Equal(t, expectedMetrics, mergedMetrics)
}

func TestMergeMetrics(t *testing.T) {
expectedMetrics := pmetric.NewMetrics()
expectedMetrics.ResourceMetrics().EnsureCapacity(3)
ametrics := expectedMetrics.ResourceMetrics().AppendEmpty()
ametrics.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-1")
ametrics.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("m1")
bmetrics := expectedMetrics.ResourceMetrics().AppendEmpty()
bmetrics.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-2")
bmetrics.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("m1")
cmetrics := expectedMetrics.ResourceMetrics().AppendEmpty()
cmetrics.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-3")
cmetrics.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("m2")

metric1 := pmetric.NewMetrics()
metric1.ResourceMetrics().EnsureCapacity(2)
m1ametrics := metric1.ResourceMetrics().AppendEmpty()
m1ametrics.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-1")
m1ametrics.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("m1")
m1bmetrics := metric1.ResourceMetrics().AppendEmpty()
m1bmetrics.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-2")
m1bmetrics.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("m1")

metric2 := pmetric.NewMetrics()
metric2.ResourceMetrics().EnsureCapacity(1)
m2cmetrics := metric2.ResourceMetrics().AppendEmpty()
m2cmetrics.Resource().Attributes().PutStr(conventions.AttributeServiceName, "service-name-3")
m2cmetrics.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("m2")

mergedMetrics := mergeMetrics(metric1, metric2)

require.Equal(t, expectedMetrics, mergedMetrics)
}
90 changes: 59 additions & 31 deletions exporter/loadbalancingexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (

var _ exporter.Metrics = (*metricExporterImp)(nil)

type exporterMetrics map[component.Component]map[string]pmetric.Metrics
type endpointMetrics map[string]pmetric.Metrics

type metricExporterImp struct {
loadBalancer loadBalancer
routingKey routingKey
Expand Down Expand Up @@ -80,46 +83,71 @@ func (e *metricExporterImp) Shutdown(context.Context) error {

func (e *metricExporterImp) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
var errs error
var exp component.Component

batches := batchpersignal.SplitMetrics(md)
for _, batch := range batches {
errs = multierr.Append(errs, e.consumeMetric(ctx, batch))
}

return errs
}
exporterSegregatedMetrics := make(exporterMetrics)
endpointSegregatedMetrics := make(endpointMetrics)

func (e *metricExporterImp) consumeMetric(ctx context.Context, md pmetric.Metrics) error {
var exp component.Component
routingIds, err := routingIdentifiersFromMetrics(md, e.routingKey)
if err != nil {
return err
}
for rid := range routingIds {
endpoint := e.loadBalancer.Endpoint([]byte(rid))
exp, err = e.loadBalancer.Exporter(endpoint)
for _, batch := range batches {
routingIds, err := routingIdentifiersFromMetrics(batch, e.routingKey)
if err != nil {
return err
}

te, ok := exp.(exporter.Metrics)
if !ok {
return fmt.Errorf("unable to export metrics, unexpected exporter type: expected exporter.Metrics but got %T", exp)
for rid := range routingIds {
endpoint := e.loadBalancer.Endpoint([]byte(rid))
exp, err = e.loadBalancer.Exporter(endpoint)
if err != nil {
return err
}
_, ok := exp.(exporter.Metrics)
if !ok {
return fmt.Errorf("unable to export metrics, unexpected exporter type: expected exporter.Metrics but got %T", exp)
}

_, ok = endpointSegregatedMetrics[endpoint]
if !ok {
endpointSegregatedMetrics[endpoint] = pmetric.Metrics{}
}
endpointSegregatedMetrics[endpoint] = mergeMetrics(endpointSegregatedMetrics[endpoint], batch)

_, ok = exporterSegregatedMetrics[exp]
if !ok {
exporterSegregatedMetrics[exp] = endpointMetrics{}
}
exporterSegregatedMetrics[exp][endpoint] = endpointSegregatedMetrics[endpoint]
}
}

errs = multierr.Append(errs, e.consumeMetric(ctx, exporterSegregatedMetrics))

return errs
}

start := time.Now()
err = te.ConsumeMetrics(ctx, md)
duration := time.Since(start)

if err == nil {
_ = stats.RecordWithTags(
ctx,
[]tag.Mutator{tag.Upsert(endpointTagKey, endpoint), successTrueMutator},
mBackendLatency.M(duration.Milliseconds()))
} else {
_ = stats.RecordWithTags(
ctx,
[]tag.Mutator{tag.Upsert(endpointTagKey, endpoint), successFalseMutator},
mBackendLatency.M(duration.Milliseconds()))
func (e *metricExporterImp) consumeMetric(ctx context.Context, exporterSegregatedMetrics exporterMetrics) error {
var err error

for exp, endpointMetrics := range exporterSegregatedMetrics {
for endpoint, md := range endpointMetrics {
te, _ := exp.(exporter.Metrics)

start := time.Now()
err = te.ConsumeMetrics(ctx, md)
duration := time.Since(start)

if err == nil {
_ = stats.RecordWithTags(
ctx,
[]tag.Mutator{tag.Upsert(endpointTagKey, endpoint), successTrueMutator},
mBackendLatency.M(duration.Milliseconds()))
} else {
_ = stats.RecordWithTags(
ctx,
[]tag.Mutator{tag.Upsert(endpointTagKey, endpoint), successFalseMutator},
mBackendLatency.M(duration.Milliseconds()))
}
}
}

Expand Down
Loading