Skip to content

Commit

Permalink
fix(azuremonitorexporter): Fix flushes on each single Span (#37217)
Browse files Browse the repository at this point in the history
#### Description
This commit fixes AzureMonitorExporter issue when it calls Flush on each
Span

<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes #37214 

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Unit tests updated or introduced new coverage to track number of calls
to the Flush method

<!--Describe the documentation added.-->
#### Documentation
No documentation added

<!--Please delete paragraphs that you did not use before submitting.-->
  • Loading branch information
an-mmx authored Mar 7, 2025
1 parent d472019 commit 3863a10
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix_azure-monitor-exporter-flush-ddos.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: 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]
8 changes: 6 additions & 2 deletions exporter/azuremonitorexporter/azuremonitor_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,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
Expand All @@ -137,5 +135,11 @@ func (exporter *azureMonitorExporter) consumeTraces(_ context.Context, traceData

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
}
31 changes: 31 additions & 0 deletions exporter/azuremonitorexporter/traceexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestExporterTraceDataCallbackNoSpans(t *testing.T) {
assert.NoError(t, exporter.consumeTraces(context.Background(), traces))

mockTransportChannel.AssertNumberOfCalls(t, "Send", 0)
mockTransportChannel.AssertNumberOfCalls(t, "Flush", 0)
}

// Tests the export onTraceData callback with a single Span
Expand All @@ -51,6 +52,34 @@ func TestExporterTraceDataCallbackSingleSpan(t *testing.T) {
assert.NoError(t, exporter.consumeTraces(context.Background(), traces))

mockTransportChannel.AssertNumberOfCalls(t, "Send", 1)
mockTransportChannel.AssertNumberOfCalls(t, "Flush", 1)
}

// Tests the export onTraceData callback calls exporter flush only once for 8 spans
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.consumeTraces(context.Background(), traces))

mockTransportChannel.AssertNumberOfCalls(t, "Send", 8)
mockTransportChannel.AssertNumberOfCalls(t, "Flush", 1)
}

// Tests the export onTraceData callback with a single Span with SpanEvents
Expand Down Expand Up @@ -83,6 +112,7 @@ func TestExporterTraceDataCallbackSingleSpanWithSpanEvents(t *testing.T) {
assert.NoError(t, exporter.consumeTraces(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
Expand Down Expand Up @@ -112,6 +142,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 {
Expand Down

0 comments on commit 3863a10

Please sign in to comment.