From 7c4f82c91ce3784e39027c954b6bccce930ff1a7 Mon Sep 17 00:00:00 2001 From: "M.D" Date: Tue, 14 Jan 2025 14:34:15 +0200 Subject: [PATCH] fix(azuremonitorexporter): Fixes flushes on each single Span --- ...fix_azure-monitor-exporter-flush-ddos.yaml | 28 ++++++++++++++++++ .../azuremonitorexporter/traceexporter.go | 8 +++-- .../traceexporter_test.go | 29 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 .chloggen/fix_azure-monitor-exporter-flush-ddos.yaml diff --git a/.chloggen/fix_azure-monitor-exporter-flush-ddos.yaml b/.chloggen/fix_azure-monitor-exporter-flush-ddos.yaml new file mode 100644 index 000000000000..a5ef5f1af3cf --- /dev/null +++ b/.chloggen/fix_azure-monitor-exporter-flush-ddos.yaml @@ -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: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: azuremonitorexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix flushes on each single Span + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [37214] + +# (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: [user] + diff --git a/exporter/azuremonitorexporter/traceexporter.go b/exporter/azuremonitorexporter/traceexporter.go index 66326d2f540b..33a1d1d95d83 100644 --- a/exporter/azuremonitorexporter/traceexporter.go +++ b/exporter/azuremonitorexporter/traceexporter.go @@ -46,8 +46,6 @@ func (v *traceVisitor) visit( v.exporter.transportChannel.Send(envelope) } - // Flush the transport channel to force the telemetry to be sent - v.exporter.transportChannel.Flush() v.processed++ return true @@ -61,6 +59,12 @@ func (exporter *traceExporter) onTraceData(_ context.Context, traceData ptrace.T visitor := &traceVisitor{exporter: exporter} accept(traceData, visitor) + + // Flush the transport channel to force the telemetry to be sent + if visitor.processed > 0 { + exporter.transportChannel.Flush() + } + return visitor.err } diff --git a/exporter/azuremonitorexporter/traceexporter_test.go b/exporter/azuremonitorexporter/traceexporter_test.go index d58a2dc9fd52..5edf3a93c379 100644 --- a/exporter/azuremonitorexporter/traceexporter_test.go +++ b/exporter/azuremonitorexporter/traceexporter_test.go @@ -50,6 +50,33 @@ func TestExporterTraceDataCallbackSingleSpan(t *testing.T) { assert.NoError(t, exporter.onTraceData(context.Background(), traces)) mockTransportChannel.AssertNumberOfCalls(t, "Send", 1) + mockTransportChannel.AssertNumberOfCalls(t, "Flush", 1) +} + +func TestExporterTraceDataCallbackCallFlushOnce(t *testing.T) { + mockTransportChannel := getMockTransportChannel() + exporter := getExporter(defaultConfig, mockTransportChannel) + + resource := getResource() + scope := getScope() + span := getDefaultHTTPServerSpan() + + traces := ptrace.NewTraces() + rs := traces.ResourceSpans().AppendEmpty() + r := rs.Resource() + resource.CopyTo(r) + ilss := rs.ScopeSpans().AppendEmpty() + scope.CopyTo(ilss.Scope()) + + span.CopyTo(ilss.Spans().AppendEmpty()) + span.CopyTo(ilss.Spans().AppendEmpty()) + ilss.CopyTo(rs.ScopeSpans().AppendEmpty()) + rs.CopyTo(traces.ResourceSpans().AppendEmpty()) + + assert.NoError(t, exporter.onTraceData(context.Background(), traces)) + + mockTransportChannel.AssertNumberOfCalls(t, "Send", 8) + mockTransportChannel.AssertNumberOfCalls(t, "Flush", 1) } // Tests the export onTraceData callback with a single Span with SpanEvents @@ -82,6 +109,7 @@ func TestExporterTraceDataCallbackSingleSpanWithSpanEvents(t *testing.T) { assert.NoError(t, exporter.onTraceData(context.Background(), traces)) mockTransportChannel.AssertNumberOfCalls(t, "Send", 3) + mockTransportChannel.AssertNumberOfCalls(t, "Flush", 1) } // Tests the export onTraceData callback with a single Span that fails to produce an envelope @@ -111,6 +139,7 @@ func TestExporterTraceDataCallbackSingleSpanNoEnvelope(t *testing.T) { assert.True(t, consumererror.IsPermanent(err), "error should be permanent") mockTransportChannel.AssertNumberOfCalls(t, "Send", 0) + mockTransportChannel.AssertNumberOfCalls(t, "Flush", 0) } func getMockTransportChannel() *mockTransportChannel {