From 858737cdb0f50cd3a8ccd70d3a26f5334f53b82d Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Wed, 30 Oct 2024 09:48:42 -0700 Subject: [PATCH] [console] Support ActivitySource.Tags (#5935) --- .../CHANGELOG.md | 11 ++- .../ConsoleActivityExporter.cs | 25 ++++-- .../ConsoleMetricExporter.cs | 77 ++++++++----------- 3 files changed, 61 insertions(+), 52 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md index d50c0cd8be5..c44b85af92f 100644 --- a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md @@ -13,6 +13,11 @@ Notes](../../RELEASENOTES.md). ([#5874](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5874), [#5891](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5891)) +* Added support for Instrumentation Scope Attributes (i.e + [ActivitySource.Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.activitysource.tags)) + when writing traces to the console. + ([#5935](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5935)) + ## 1.10.0-beta.1 Released 2024-Sep-30 @@ -61,9 +66,9 @@ Released 2023-Dec-08 Released 2023-Nov-29 -* Add support for Instrumentation Scope Attributes (i.e [Meter - Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)), - fixing issue +* Added support for Instrumentation Scope Attributes (i.e + [Meter.Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)), + when writing metrics to the console, fixing issue [#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563). ([#5089](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5089)) diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index 08d280c6101..c1b707b3e4d 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -31,12 +31,6 @@ public override ExportResult Export(in Batch batch) this.WriteLine($"Activity.ParentSpanId: {activity.ParentSpanId}"); } - this.WriteLine($"Activity.ActivitySourceName: {activity.Source.Name}"); - if (!string.IsNullOrEmpty(activity.Source.Version)) - { - this.WriteLine($"Activity.ActivitySourceVersion: {activity.Source.Version}"); - } - this.WriteLine($"Activity.DisplayName: {activity.DisplayName}"); this.WriteLine($"Activity.Kind: {activity.Kind}"); this.WriteLine($"Activity.StartTime: {activity.StartTimeUtc:yyyy-MM-ddTHH:mm:ss.fffffffZ}"); @@ -117,6 +111,25 @@ public override ExportResult Export(in Batch batch) } } + this.WriteLine("Instrumentation scope (ActivitySource):"); + this.WriteLine($" Name: {activity.Source.Name}"); + if (!string.IsNullOrEmpty(activity.Source.Version)) + { + this.WriteLine($" Version: {activity.Source.Version}"); + } + + if (activity.Source.Tags?.Any() == true) + { + this.WriteLine(" Tags:"); + foreach (var activitySourceTag in activity.Source.Tags) + { + if (this.TagWriter.TryTransformTag(activitySourceTag, out var result)) + { + this.WriteLine($" {result.Key}: {result.Value}"); + } + } + } + var resource = this.ParentProvider.GetResource(); if (resource != Resource.Empty) { diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index 264da361e6e..97ba3bbe25c 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -10,8 +10,6 @@ namespace OpenTelemetry.Exporter; public class ConsoleMetricExporter : ConsoleExporter { - private Resource? resource; - public ConsoleMetricExporter(ConsoleExporterOptions options) : base(options) { @@ -19,30 +17,13 @@ public ConsoleMetricExporter(ConsoleExporterOptions options) public override ExportResult Export(in Batch batch) { - if (this.resource == null) - { - this.resource = this.ParentProvider.GetResource(); - if (this.resource != Resource.Empty) - { - this.WriteLine("Resource associated with Metric:"); - foreach (var resourceAttribute in this.resource.Attributes) - { - if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result)) - { - this.WriteLine($" {result.Key}: {result.Value}"); - } - } - } - } - foreach (var metric in batch) { var msg = new StringBuilder($"\n"); msg.Append($"Metric Name: {metric.Name}"); if (metric.Description != string.Empty) { - msg.Append(", "); - msg.Append(metric.Description); + msg.Append($", Description: {metric.Description}"); } if (metric.Unit != string.Empty) @@ -50,30 +31,8 @@ public override ExportResult Export(in Batch batch) msg.Append($", Unit: {metric.Unit}"); } - if (!string.IsNullOrEmpty(metric.MeterName)) - { - msg.Append($", Meter: {metric.MeterName}"); - - if (!string.IsNullOrEmpty(metric.MeterVersion)) - { - msg.Append($"/{metric.MeterVersion}"); - } - } - this.WriteLine(msg.ToString()); - if (metric.MeterTags != null) - { - foreach (var meterTag in metric.MeterTags) - { - this.WriteLine("\tMeter Tags:"); - if (this.TagWriter.TryTransformTag(meterTag, out var result)) - { - this.WriteLine($"\t\t{result.Key}: {result.Value}"); - } - } - } - foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { string valueDisplay = string.Empty; @@ -220,7 +179,7 @@ public override ExportResult Export(in Batch batch) { if (!appendedTagString) { - exemplarString.Append(" Filtered Tags : "); + exemplarString.Append(" Filtered Tags: "); appendedTagString = true; } @@ -257,6 +216,38 @@ public override ExportResult Export(in Batch batch) } this.WriteLine(msg.ToString()); + + this.WriteLine("Instrumentation scope (Meter):"); + this.WriteLine($"\tName: {metric.MeterName}"); + if (!string.IsNullOrEmpty(metric.MeterVersion)) + { + this.WriteLine($"\tVersion: {metric.MeterVersion}"); + } + + if (metric.MeterTags?.Any() == true) + { + this.WriteLine("\tTags:"); + foreach (var meterTag in metric.MeterTags) + { + if (this.TagWriter.TryTransformTag(meterTag, out var result)) + { + this.WriteLine($"\t\t{result.Key}: {result.Value}"); + } + } + } + + var resource = this.ParentProvider.GetResource(); + if (resource != Resource.Empty) + { + this.WriteLine("Resource associated with Metric:"); + foreach (var resourceAttribute in resource.Attributes) + { + if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result)) + { + this.WriteLine($"\t{result.Key}: {result.Value}"); + } + } + } } }