Skip to content

Commit 858737c

Browse files
authored
[console] Support ActivitySource.Tags (open-telemetry#5935)
1 parent 451a94b commit 858737c

File tree

3 files changed

+61
-52
lines changed

3 files changed

+61
-52
lines changed

src/OpenTelemetry.Exporter.Console/CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Notes](../../RELEASENOTES.md).
1313
([#5874](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5874),
1414
[#5891](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5891))
1515

16+
* Added support for Instrumentation Scope Attributes (i.e
17+
[ActivitySource.Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.activitysource.tags))
18+
when writing traces to the console.
19+
([#5935](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5935))
20+
1621
## 1.10.0-beta.1
1722

1823
Released 2024-Sep-30
@@ -61,9 +66,9 @@ Released 2023-Dec-08
6166

6267
Released 2023-Nov-29
6368

64-
* Add support for Instrumentation Scope Attributes (i.e [Meter
65-
Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)),
66-
fixing issue
69+
* Added support for Instrumentation Scope Attributes (i.e
70+
[Meter.Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)),
71+
when writing metrics to the console, fixing issue
6772
[#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563).
6873
([#5089](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5089))
6974

src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ public override ExportResult Export(in Batch<Activity> batch)
3131
this.WriteLine($"Activity.ParentSpanId: {activity.ParentSpanId}");
3232
}
3333

34-
this.WriteLine($"Activity.ActivitySourceName: {activity.Source.Name}");
35-
if (!string.IsNullOrEmpty(activity.Source.Version))
36-
{
37-
this.WriteLine($"Activity.ActivitySourceVersion: {activity.Source.Version}");
38-
}
39-
4034
this.WriteLine($"Activity.DisplayName: {activity.DisplayName}");
4135
this.WriteLine($"Activity.Kind: {activity.Kind}");
4236
this.WriteLine($"Activity.StartTime: {activity.StartTimeUtc:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
@@ -117,6 +111,25 @@ public override ExportResult Export(in Batch<Activity> batch)
117111
}
118112
}
119113

114+
this.WriteLine("Instrumentation scope (ActivitySource):");
115+
this.WriteLine($" Name: {activity.Source.Name}");
116+
if (!string.IsNullOrEmpty(activity.Source.Version))
117+
{
118+
this.WriteLine($" Version: {activity.Source.Version}");
119+
}
120+
121+
if (activity.Source.Tags?.Any() == true)
122+
{
123+
this.WriteLine(" Tags:");
124+
foreach (var activitySourceTag in activity.Source.Tags)
125+
{
126+
if (this.TagWriter.TryTransformTag(activitySourceTag, out var result))
127+
{
128+
this.WriteLine($" {result.Key}: {result.Value}");
129+
}
130+
}
131+
}
132+
120133
var resource = this.ParentProvider.GetResource();
121134
if (resource != Resource.Empty)
122135
{

src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,70 +10,29 @@ namespace OpenTelemetry.Exporter;
1010

1111
public class ConsoleMetricExporter : ConsoleExporter<Metric>
1212
{
13-
private Resource? resource;
14-
1513
public ConsoleMetricExporter(ConsoleExporterOptions options)
1614
: base(options)
1715
{
1816
}
1917

2018
public override ExportResult Export(in Batch<Metric> batch)
2119
{
22-
if (this.resource == null)
23-
{
24-
this.resource = this.ParentProvider.GetResource();
25-
if (this.resource != Resource.Empty)
26-
{
27-
this.WriteLine("Resource associated with Metric:");
28-
foreach (var resourceAttribute in this.resource.Attributes)
29-
{
30-
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
31-
{
32-
this.WriteLine($" {result.Key}: {result.Value}");
33-
}
34-
}
35-
}
36-
}
37-
3820
foreach (var metric in batch)
3921
{
4022
var msg = new StringBuilder($"\n");
4123
msg.Append($"Metric Name: {metric.Name}");
4224
if (metric.Description != string.Empty)
4325
{
44-
msg.Append(", ");
45-
msg.Append(metric.Description);
26+
msg.Append($", Description: {metric.Description}");
4627
}
4728

4829
if (metric.Unit != string.Empty)
4930
{
5031
msg.Append($", Unit: {metric.Unit}");
5132
}
5233

53-
if (!string.IsNullOrEmpty(metric.MeterName))
54-
{
55-
msg.Append($", Meter: {metric.MeterName}");
56-
57-
if (!string.IsNullOrEmpty(metric.MeterVersion))
58-
{
59-
msg.Append($"/{metric.MeterVersion}");
60-
}
61-
}
62-
6334
this.WriteLine(msg.ToString());
6435

65-
if (metric.MeterTags != null)
66-
{
67-
foreach (var meterTag in metric.MeterTags)
68-
{
69-
this.WriteLine("\tMeter Tags:");
70-
if (this.TagWriter.TryTransformTag(meterTag, out var result))
71-
{
72-
this.WriteLine($"\t\t{result.Key}: {result.Value}");
73-
}
74-
}
75-
}
76-
7736
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
7837
{
7938
string valueDisplay = string.Empty;
@@ -220,7 +179,7 @@ public override ExportResult Export(in Batch<Metric> batch)
220179
{
221180
if (!appendedTagString)
222181
{
223-
exemplarString.Append(" Filtered Tags : ");
182+
exemplarString.Append(" Filtered Tags: ");
224183
appendedTagString = true;
225184
}
226185

@@ -257,6 +216,38 @@ public override ExportResult Export(in Batch<Metric> batch)
257216
}
258217

259218
this.WriteLine(msg.ToString());
219+
220+
this.WriteLine("Instrumentation scope (Meter):");
221+
this.WriteLine($"\tName: {metric.MeterName}");
222+
if (!string.IsNullOrEmpty(metric.MeterVersion))
223+
{
224+
this.WriteLine($"\tVersion: {metric.MeterVersion}");
225+
}
226+
227+
if (metric.MeterTags?.Any() == true)
228+
{
229+
this.WriteLine("\tTags:");
230+
foreach (var meterTag in metric.MeterTags)
231+
{
232+
if (this.TagWriter.TryTransformTag(meterTag, out var result))
233+
{
234+
this.WriteLine($"\t\t{result.Key}: {result.Value}");
235+
}
236+
}
237+
}
238+
239+
var resource = this.ParentProvider.GetResource();
240+
if (resource != Resource.Empty)
241+
{
242+
this.WriteLine("Resource associated with Metric:");
243+
foreach (var resourceAttribute in resource.Attributes)
244+
{
245+
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
246+
{
247+
this.WriteLine($"\t{result.Key}: {result.Value}");
248+
}
249+
}
250+
}
260251
}
261252
}
262253

0 commit comments

Comments
 (0)