Skip to content

Commit

Permalink
[console] Support ActivitySource.Tags (#5935)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Oct 30, 2024
1 parent 451a94b commit 858737c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 52 deletions.
11 changes: 8 additions & 3 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
25 changes: 19 additions & 6 deletions src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public override ExportResult Export(in Batch<Activity> 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}");
Expand Down Expand Up @@ -117,6 +111,25 @@ public override ExportResult Export(in Batch<Activity> 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)
{
Expand Down
77 changes: 34 additions & 43 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,29 @@ namespace OpenTelemetry.Exporter;

public class ConsoleMetricExporter : ConsoleExporter<Metric>
{
private Resource? resource;

public ConsoleMetricExporter(ConsoleExporterOptions options)
: base(options)
{
}

public override ExportResult Export(in Batch<Metric> 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)
{
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;
Expand Down Expand Up @@ -220,7 +179,7 @@ public override ExportResult Export(in Batch<Metric> batch)
{
if (!appendedTagString)
{
exemplarString.Append(" Filtered Tags : ");
exemplarString.Append(" Filtered Tags: ");
appendedTagString = true;
}

Expand Down Expand Up @@ -257,6 +216,38 @@ public override ExportResult Export(in Batch<Metric> 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}");
}
}
}
}
}

Expand Down

0 comments on commit 858737c

Please sign in to comment.