Skip to content

Commit

Permalink
Fix #1958 OTel bridge span's destination service may contain null res…
Browse files Browse the repository at this point in the history
…ource (#1964)

Co-authored-by: Wolfgang Ziegler <[email protected]>
Fix #1958
  • Loading branch information
kuanpak authored Jan 10, 2023
1 parent 643c6e1 commit 02ea126
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,11 @@ static string ToResourceName(string type, string name)
}

span.Context.Service = new SpanService(new Target(serviceTargetType, serviceTargetName));
span.Context.Destination ??= new Destination();
span.Context.Destination.Service = new Destination.DestinationService { Resource = resource };
if (resource != null)
{
span.Context.Destination ??= new Destination();
span.Context.Destination.Service = new Destination.DestinationService { Resource = resource };
}
}

public void Dispose() => Listener?.Dispose();
Expand Down
15 changes: 15 additions & 0 deletions test/Elastic.Apm.Tests/OpenTelemetryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ public void ActivityAndTransactionTraceIdSynced()
using (var activity = src.StartActivity("foo", ActivityKind.Server)) traceId = activity.TraceId.ToString();
payloadSender.FirstTransaction.TraceId.Should().Be(traceId);
}

[Fact]
public void ResourceIsRequiredWhenSpanDestinationServiceIsNotNull()
{
var payloadSender = new MockPayloadSender();
using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender, apmServerInfo: MockApmServerInfo.Version716,
configuration: new MockConfiguration(enableOpenTelemetryBridge: "true"))))
OTSamples.TwoSpansWithAttributes();

payloadSender.Spans.Should().NotContain(span =>
span.Context.Destination != null
&& span.Context.Destination.Service != null
&& span.Context.Destination.Service.Resource == null,
because: "Resource is required in Destination Serivce");
}
}
}

Expand Down

0 comments on commit 02ea126

Please sign in to comment.