-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(loadbalancingexporter): optimize metrics and traces export #30141
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
Merged
jpkrohling
merged 11 commits into
open-telemetry:main
from
mat-rumian:mat-rumian-lbe-imp
Jan 25, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a488fa7
feat: add metrics&traces merge fns
mat-rumian cbe2479
feat: optimize traces sent
mat-rumian 6a4d717
feat: optimize metrics sent
mat-rumian bf8222a
chore: add changelog
mat-rumian 4e08eba
Merge branch 'main' into mat-rumian-lbe-imp
mat-rumian bfb2837
fix: checks
mat-rumian d9d9ef7
fix: exporter type check
mat-rumian b445cb6
fix: tests and segregation assignments
mat-rumian fd6a12c
fix: mergering functions
mat-rumian b94780b
fix tests
mat-rumian cc1ae01
Merge branch 'main' into mat-rumian-lbe-imp
mat-rumian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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: [] |
This file contains hidden or 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,123 @@ | ||
// 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 two ptrace.Traces into a single ptrace.Traces. | ||
func mergeTraces(t1 ptrace.Traces, t2 ptrace.Traces) ptrace.Traces { | ||
mergedTraces := ptrace.NewTraces() | ||
|
||
if t1.SpanCount() == 0 && t2.SpanCount() == 0 { | ||
return mergedTraces | ||
} | ||
|
||
// Iterate over the first trace and append spans to the merged traces | ||
for i := 0; i < t1.ResourceSpans().Len(); i++ { | ||
rs := t1.ResourceSpans().At(i) | ||
newRS := mergedTraces.ResourceSpans().AppendEmpty() | ||
|
||
rs.Resource().MoveTo(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().MoveTo(newILS.Scope()) | ||
newILS.SetSchemaUrl(ils.SchemaUrl()) | ||
|
||
for k := 0; k < ils.Spans().Len(); k++ { | ||
span := ils.Spans().At(k) | ||
newSpan := newILS.Spans().AppendEmpty() | ||
span.MoveTo(newSpan) | ||
} | ||
} | ||
} | ||
|
||
// Iterate over the second trace and append spans to the merged traces | ||
for i := 0; i < t2.ResourceSpans().Len(); i++ { | ||
rs := t2.ResourceSpans().At(i) | ||
newRS := mergedTraces.ResourceSpans().AppendEmpty() | ||
|
||
rs.Resource().MoveTo(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().MoveTo(newILS.Scope()) | ||
newILS.SetSchemaUrl(ils.SchemaUrl()) | ||
|
||
for k := 0; k < ils.Spans().Len(); k++ { | ||
span := ils.Spans().At(k) | ||
newSpan := newILS.Spans().AppendEmpty() | ||
span.MoveTo(newSpan) | ||
} | ||
} | ||
} | ||
|
||
return mergedTraces | ||
} | ||
|
||
// mergeMetrics concatenates two pmetric.Metrics into a single pmetric.Metrics. | ||
func mergeMetrics(m1 pmetric.Metrics, m2 pmetric.Metrics) pmetric.Metrics { | ||
mergedMetrics := pmetric.NewMetrics() | ||
|
||
if m1.MetricCount() == 0 && m2.MetricCount() == 0 { | ||
return mergedMetrics | ||
} | ||
|
||
// Iterate over the first metric and append metrics to the merged metrics | ||
for i := 0; i < m1.ResourceMetrics().Len(); i++ { | ||
rs := m1.ResourceMetrics().At(i) | ||
newRS := mergedMetrics.ResourceMetrics().AppendEmpty() | ||
|
||
rs.Resource().MoveTo(newRS.Resource()) | ||
newRS.SetSchemaUrl(rs.SchemaUrl()) | ||
|
||
for j := 0; j < rs.ScopeMetrics().Len(); j++ { | ||
ils := rs.ScopeMetrics().At(j) | ||
|
||
newILS := newRS.ScopeMetrics().AppendEmpty() | ||
ils.Scope().MoveTo(newILS.Scope()) | ||
newILS.SetSchemaUrl(ils.SchemaUrl()) | ||
|
||
for k := 0; k < ils.Metrics().Len(); k++ { | ||
metric := ils.Metrics().At(k) | ||
newMetric := newILS.Metrics().AppendEmpty() | ||
metric.MoveTo(newMetric) | ||
} | ||
} | ||
} | ||
|
||
// Iterate over the second metric and append metrics to the merged metrics | ||
for i := 0; i < m2.ResourceMetrics().Len(); i++ { | ||
rs := m2.ResourceMetrics().At(i) | ||
newRS := mergedMetrics.ResourceMetrics().AppendEmpty() | ||
|
||
rs.Resource().MoveTo(newRS.Resource()) | ||
newRS.SetSchemaUrl(rs.SchemaUrl()) | ||
|
||
for j := 0; j < rs.ScopeMetrics().Len(); j++ { | ||
ils := rs.ScopeMetrics().At(j) | ||
|
||
newILS := newRS.ScopeMetrics().AppendEmpty() | ||
ils.Scope().MoveTo(newILS.Scope()) | ||
newILS.SetSchemaUrl(ils.SchemaUrl()) | ||
|
||
for k := 0; k < ils.Metrics().Len(); k++ { | ||
metric := ils.Metrics().At(k) | ||
newMetric := newILS.Metrics().AppendEmpty() | ||
metric.MoveTo(newMetric) | ||
} | ||
} | ||
} | ||
|
||
return mergedMetrics | ||
} |
This file contains hidden or 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,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.NewTraces() | ||
trace2 := ptrace.NewTraces() | ||
|
||
mergedTraces := mergeTraces(trace1, trace2) | ||
|
||
require.Equal(t, expectedEmpty, mergedTraces) | ||
} | ||
|
||
func TestMergeTracesSingleEmpty(t *testing.T) { | ||
expectedTraces := simpleTraces() | ||
|
||
trace1 := ptrace.NewTraces() | ||
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.NewMetrics() | ||
metric2 := pmetric.NewMetrics() | ||
|
||
mergedMetrics := mergeMetrics(metric1, metric2) | ||
|
||
require.Equal(t, expectedEmpty, mergedMetrics) | ||
} | ||
|
||
func TestMergeMetricsSingleEmpty(t *testing.T) { | ||
expectedMetrics := simpleMetricsWithResource() | ||
|
||
metric1 := pmetric.NewMetrics() | ||
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) | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.