Skip to content

Commit

Permalink
fix(azuremonitorexporter): Fixes flushes on each single Span
Browse files Browse the repository at this point in the history
  • Loading branch information
an-mmx committed Jan 22, 2025
1 parent 4ebb7af commit 7c4f82c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
28 changes: 28 additions & 0 deletions .chloggen/fix_azure-monitor-exporter-flush-ddos.yaml
Original file line number Diff line number Diff line change
@@ -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]

8 changes: 6 additions & 2 deletions exporter/azuremonitorexporter/traceexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
29 changes: 29 additions & 0 deletions exporter/azuremonitorexporter/traceexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7c4f82c

Please sign in to comment.