diff --git a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md index abacb304cfb..7f688ee9942 100644 --- a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md @@ -2,6 +2,21 @@ ## Unreleased +* Improve the conversion and formatting of attribute values. + The list of data types that must be supported per the + [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/common#attribute) + is more narrow than what the .NET OpenTelemetry SDK supports. Numeric + [built-in value types](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/built-in-types) + are supported by converting to a `long` or `double` as appropriate except for + numeric types that could cause overflow (`ulong`) or rounding (`decimal`) + which are converted to strings. Non-numeric built-in types - `string`, + `char`, `bool` are supported. All other types are converted to a `string`. + Array values are also supported. + ([#3311](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3311)) +* Fix conversion of array-valued attributes. They were previously + converted to a string like "System.String[]". + ([#3311](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3311)) + ## 1.3.0-beta.2 Released 2022-May-16 diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index 5d0ed336e81..9c08ce59f27 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -14,7 +14,6 @@ // limitations under the License. // -using System; using System.Diagnostics; using System.Linq; using OpenTelemetry.Resources; @@ -71,13 +70,10 @@ public override ExportResult Export(in Batch batch) continue; } - if (tag.Value is not Array array) + if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result)) { - this.WriteLine($" {tag.Key}: {tag.Value}"); - continue; + this.WriteLine($" {result}"); } - - this.WriteLine($" {tag.Key}: [{string.Join(", ", array.Cast())}]"); } } @@ -106,7 +102,10 @@ public override ExportResult Export(in Batch batch) this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]"); foreach (var attribute in activityEvent.Tags) { - this.WriteLine($" {attribute.Key}: {attribute.Value}"); + if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result)) + { + this.WriteLine($" {result}"); + } } } } @@ -117,6 +116,13 @@ public override ExportResult Export(in Batch batch) foreach (var activityLink in activity.Links) { this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}"); + foreach (var attribute in activityLink.Tags) + { + if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result)) + { + this.WriteLine($" {result}"); + } + } } } @@ -126,7 +132,10 @@ public override ExportResult Export(in Batch batch) this.WriteLine("Resource associated with Activity:"); foreach (var resourceAttribute in resource.Attributes) { - this.WriteLine($" {resourceAttribute.Key}: {resourceAttribute.Value}"); + if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result)) + { + this.WriteLine($" {result}"); + } } } diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs index b8e2cde014d..2e3731e1d3b 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs @@ -62,13 +62,13 @@ public override ExportResult Export(in Batch batch) // Special casing {OriginalFormat} // See https://github.com/open-telemetry/opentelemetry-dotnet/pull/3182 // for explanation. - if (logRecord.StateValues[i].Key.Equals("{OriginalFormat}")) - { - this.WriteLine($"{string.Empty,-4}{"OriginalFormat (a.k.a. Body)",-RightPaddingLength}{logRecord.StateValues[i].Value}"); - } - else + var valueToTransform = logRecord.StateValues[i].Key.Equals("{OriginalValue}") + ? new KeyValuePair("OriginalFormat (a.k.a Body)", logRecord.StateValues[i].Value) + : logRecord.StateValues[i]; + + if (ConsoleTagTransformer.Instance.TryTransformTag(valueToTransform, out var result)) { - this.WriteLine($"{string.Empty,-4}{logRecord.StateValues[i].Key,-RightPaddingLength}{logRecord.StateValues[i].Value}"); + this.WriteLine($"{string.Empty,-4}{result}"); } } } @@ -100,7 +100,10 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter) foreach (KeyValuePair scopeItem in scope) { - exporter.WriteLine($"[Scope.{scopeDepth}]:{scopeItem.Key,-RightPaddingLength}{scopeItem.Value}"); + if (ConsoleTagTransformer.Instance.TryTransformTag(scopeItem, out var result)) + { + exporter.WriteLine($"[Scope.{scopeDepth}]:{result}"); + } } } @@ -110,7 +113,10 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter) this.WriteLine("\nResource associated with LogRecord:"); foreach (var resourceAttribute in resource.Attributes) { - this.WriteLine($"{resourceAttribute.Key}: {resourceAttribute.Value}"); + if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result)) + { + this.WriteLine(result); + } } } diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index ca4cc0e1199..9948e55055a 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -80,10 +80,11 @@ public override ExportResult Export(in Batch batch) StringBuilder tagsBuilder = new StringBuilder(); foreach (var tag in metricPoint.Tags) { - tagsBuilder.Append(tag.Key); - tagsBuilder.Append(':'); - tagsBuilder.Append(tag.Value); - tagsBuilder.Append(' '); + if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result)) + { + tagsBuilder.Append(result); + tagsBuilder.Append(' '); + } } var tags = tagsBuilder.ToString().TrimEnd(); diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleTagTransformer.cs b/src/OpenTelemetry.Exporter.Console/ConsoleTagTransformer.cs new file mode 100644 index 00000000000..e87862e5b55 --- /dev/null +++ b/src/OpenTelemetry.Exporter.Console/ConsoleTagTransformer.cs @@ -0,0 +1,40 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Exporter; + +internal sealed class ConsoleTagTransformer : TagTransformer +{ + private ConsoleTagTransformer() + { + } + + public static ConsoleTagTransformer Instance { get; } = new(); + + protected override string TransformIntegralTag(string key, long value) => $"{key}: {value}"; + + protected override string TransformFloatingPointTag(string key, double value) => $"{key}: {value}"; + + protected override string TransformBooleanTag(string key, bool value) => $"{key}: {(value ? "true" : "false")}"; + + protected override string TransformStringTag(string key, string value) => $"{key}: {value}"; + + protected override string TransformArrayTag(string key, Array array) + => this.TransformStringTag(key, System.Text.Json.JsonSerializer.Serialize(array)); +} diff --git a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj index f97c1178f4f..a6a7147ee47 100644 --- a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj +++ b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj @@ -18,13 +18,20 @@ false + + + + + + +