Skip to content

Commit 86526de

Browse files
authored
Fix exemplars that have no tracing information (#10745)
1 parent ee7ef1d commit 86526de

20 files changed

+883
-400
lines changed

playground/Stress/Stress.ApiService/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Channels;
99
using System.Xml.Linq;
1010
using Microsoft.AspNetCore.Mvc;
11+
using OpenTelemetry.Metrics;
1112
using OpenTelemetry.Resources;
1213
using Stress.ApiService;
1314

@@ -23,7 +24,12 @@
2324
.WithTracing(tracing => tracing
2425
.AddSource(TraceCreator.ActivitySourceName, ProducerConsumer.ActivitySourceName)
2526
.AddSource("Services.Api"))
26-
.WithMetrics(metrics => metrics.AddMeter(TestMetrics.MeterName));
27+
.WithMetrics(metrics =>
28+
{
29+
metrics.AddMeter(TestMetrics.MeterName);
30+
metrics.SetExemplarFilter(ExemplarFilterType.AlwaysOn);
31+
32+
});
2733
builder.Services.AddSingleton<TestMetrics>();
2834

2935
var app = builder.Build();
@@ -63,6 +69,18 @@
6369
return "Counter incremented";
6470
});
6571

72+
app.MapGet("/exemplars-no-span", (TestMetrics metrics) =>
73+
{
74+
var activity = Activity.Current;
75+
Activity.Current = null;
76+
77+
metrics.RecordHistogram(Random.Shared.NextDouble(), new TagList());
78+
79+
Activity.Current = activity;
80+
81+
return "Exemplar recorded";
82+
});
83+
6684
app.MapGet("/overflow-counter", (TestMetrics metrics) =>
6785
{
6886
// Emit measurements to ensure at least 2000 unique tag values are emitted,

playground/Stress/Stress.ApiService/TestMetrics.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class TestMetrics : IDisposable
1212

1313
private readonly Meter _meter;
1414
private readonly Counter<int> _counter;
15+
private readonly Histogram<double> _histogram;
1516

1617
public TestMetrics()
1718
{
@@ -25,6 +26,11 @@ public TestMetrics()
2526
new KeyValuePair<string, object?>("instrument-tag", Guid.NewGuid().ToString())
2627
]);
2728

29+
_histogram = _meter.CreateHistogram<double>("test-histogram", unit: null, description: "This is a description", tags:
30+
[
31+
new KeyValuePair<string, object?>("instrument-tag", Guid.NewGuid().ToString())
32+
]);
33+
2834
var uploadSpeed = new List<double>();
2935

3036
Task.Run(async () =>
@@ -58,6 +64,11 @@ public void IncrementCounter(int value, in TagList tags)
5864
_counter.Add(value, in tags);
5965
}
6066

67+
public void RecordHistogram(double value, in TagList tags)
68+
{
69+
_histogram.Record(value, in tags);
70+
}
71+
6172
public void Dispose()
6273
{
6374
_meter.Dispose();

playground/Stress/Stress.AppHost/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
serviceBuilder.WithHttpCommand("/multiple-traces-linked", "Multiple traces linked", commandOptions: new() { Method = HttpMethod.Get, IconName = "ContentViewGalleryLightning" });
8484
serviceBuilder.WithHttpCommand("/overflow-counter", "Overflow counter", commandOptions: new() { Method = HttpMethod.Get, IconName = "ContentViewGalleryLightning" });
8585
serviceBuilder.WithHttpCommand("/nested-trace-spans", "Out of order nested spans", commandOptions: new() { Method = HttpMethod.Get, IconName = "ContentViewGalleryLightning" });
86+
serviceBuilder.WithHttpCommand("/exemplars-no-span", "Examplars with no span", commandOptions: new() { Method = HttpMethod.Get, IconName = "ContentViewGalleryLightning" });
8687

8788
builder.AddProject<Projects.Stress_TelemetryService>("stress-telemetryservice")
8889
.WithUrls(c => c.Urls.Add(new() { Url = "https://someplace.com", DisplayText = "Some place" }))

src/Aspire.Dashboard/Components/Controls/Chart/PlotlyChart.razor.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,22 @@ private PlotlyTrace CalculateExemplarsTrace(List<DateTimeOffset> xValues, List<C
153153

154154
foreach (var exemplar in exemplarGroups.SelectMany(g => g.Value))
155155
{
156-
var title = exemplar.Span != null
157-
? SpanWaterfallViewModel.GetTitle(exemplar.Span, Applications)
158-
: $"{Loc[nameof(ControlsStrings.PlotlyChartTrace)]}: {OtlpHelpers.ToShortenedId(exemplar.TraceId)}";
156+
string title;
157+
if (exemplar.Span != null)
158+
{
159+
title = SpanWaterfallViewModel.GetTitle(exemplar.Span, Applications);
160+
}
161+
else if (!string.IsNullOrEmpty(exemplar.TraceId))
162+
{
163+
// Exemplar has trace information but isn't matched to a span in the system.
164+
title = $"{Loc[nameof(ControlsStrings.PlotlyChartTrace)]}: {OtlpHelpers.ToShortenedId(exemplar.TraceId)}";
165+
}
166+
else
167+
{
168+
// Exemplar has no span information. Use generic title.
169+
title = Loc[nameof(ControlsStrings.PlotlyChartExemplar)];
170+
}
171+
159172
var tooltip = FormatTooltip(title, exemplar.Value, exemplar.Start);
160173

161174
exemplarTraceDto.X.Add(exemplar.Start);

0 commit comments

Comments
 (0)