Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting tags consistently #3281

Merged
merged 36 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3d44019
WIP generalize transforming tags
alanwest May 13, 2022
5c17ba1
Merge branch 'main' into alanwest/tag-transformer
alanwest May 16, 2022
6653468
Make abstract methods protected
alanwest May 17, 2022
bd59744
Add ZipkinTagTransformer
alanwest May 17, 2022
84a1ee7
Simplify ZipkinTagTransformer
alanwest May 17, 2022
9cbb855
Refactor OTLP array attribute transform
alanwest May 18, 2022
fc5bf81
Merge branch 'main' into alanwest/tag-transformer
alanwest May 19, 2022
0c92519
Merge branch 'main' into alanwest/tag-transformer
alanwest May 20, 2022
493ed24
Unused using
alanwest May 20, 2022
0ad99fe
Change Zipkin test. Array values are now represented as JSON arrays.
alanwest May 20, 2022
e8a52a4
net462 and netcoreapp3.1 runtimes are confused about pointer types
alanwest May 20, 2022
643742e
Handle array values better
alanwest May 24, 2022
347ab44
Fix namespace
alanwest May 24, 2022
181e2fa
Handle exceptions when transforming array values
alanwest May 24, 2022
a2c2d69
Log when transform fails
alanwest May 24, 2022
9d9c5d0
Merge branch 'main' into alanwest/tag-transformer
alanwest May 24, 2022
38d17a7
TransformTag -> TryTransformTag
alanwest May 25, 2022
1f09f2c
CodeAnalysis recommendations
alanwest May 25, 2022
bdd17b2
Change OTLP exporter to use TryTransformTag
alanwest May 25, 2022
f3ee3e7
CodeAnalysis
alanwest May 25, 2022
c3dd4ec
CodeAnalysis
alanwest May 25, 2022
76a2f3c
Change Jaeger exporter to use TryTransformTag
alanwest May 25, 2022
38fb5f0
Make TagTransformers singletons
alanwest May 25, 2022
4b7727c
Merge branch 'main' into alanwest/tag-transformer
alanwest May 25, 2022
efc21a4
Unused using
alanwest May 25, 2022
a536bf0
else if
alanwest May 25, 2022
b6cab43
Make singleton Instance a property
alanwest May 25, 2022
5b23656
:seal: classes
alanwest May 25, 2022
040762c
Debug.Assert
alanwest May 25, 2022
fd9f373
Do later
alanwest May 25, 2022
0d31699
Make TransformValue protected
alanwest May 25, 2022
673ade0
Exclude TagTransformer.cs from compilation of SDK project
alanwest May 25, 2022
74df0df
Move JSON serialization to projects that need it
alanwest May 25, 2022
d882145
File-scoped namespaces
alanwest May 25, 2022
4f89b27
Merge branch 'main' into alanwest/tag-transformer
cijothomas May 26, 2022
5fa4281
Update changelogs
alanwest May 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,6 @@ public static JaegerSpanRef ToJaegerSpanRef(this in ActivityLink link)
return new JaegerSpanRef(refType, traceId.Low, traceId.High, spanId.Low);
}

public static JaegerTag ToJaegerTag(this KeyValuePair<string, object> attribute)
{
return attribute.Value switch
{
string s => new JaegerTag(attribute.Key, JaegerTagType.STRING, vStr: s),
int i => new JaegerTag(attribute.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(i)),
long l => new JaegerTag(attribute.Key, JaegerTagType.LONG, vLong: l),
float f => new JaegerTag(attribute.Key, JaegerTagType.DOUBLE, vDouble: Convert.ToDouble(f)),
double d => new JaegerTag(attribute.Key, JaegerTagType.DOUBLE, vDouble: d),
bool b => new JaegerTag(attribute.Key, JaegerTagType.BOOL, vBool: b),
_ => new JaegerTag(attribute.Key, JaegerTagType.STRING, vStr: attribute.Value.ToString()),
};
}

public static long ToEpochMicroseconds(this DateTime utcDateTime)
{
// Truncate sub-microsecond precision before offsetting by the Unix Epoch to avoid
Expand All @@ -260,42 +246,6 @@ public static long ToEpochMicroseconds(this DateTimeOffset timestamp)
return microseconds - UnixEpochMicroseconds;
}

private static void ProcessJaegerTagArray(ref PooledList<JaegerTag> tags, KeyValuePair<string, object> activityTag)
{
if (activityTag.Value is int[] intArray)
{
foreach (var item in intArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(item));
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is string[] stringArray)
{
foreach (var item in stringArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.STRING, vStr: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is bool[] boolArray)
{
foreach (var item in boolArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.BOOL, vBool: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is double[] doubleArray)
{
foreach (var item in doubleArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.DOUBLE, vDouble: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
}

private struct TagEnumerationState : IActivityEnumerator<KeyValuePair<string, object>>, PeerServiceResolver.IPeerServiceState
{
public PooledList<JaegerTag> Tags;
Expand All @@ -316,14 +266,15 @@ private struct TagEnumerationState : IActivityEnumerator<KeyValuePair<string, ob

public bool ForEach(KeyValuePair<string, object> activityTag)
{
if (activityTag.Value is Array)
{
ProcessJaegerTagArray(ref this.Tags, activityTag);
}
else if (activityTag.Value != null)
if (activityTag.Value != null)
{
var key = activityTag.Key;
var jaegerTag = activityTag.ToJaegerTag();

if (!JaegerTagTransformer.Instance.TryTransformTag(activityTag, out var jaegerTag))
{
return true;
}

if (jaegerTag.VStr != null)
{
PeerServiceResolver.InspectTag(ref this, key, jaegerTag.VStr);
Expand Down Expand Up @@ -400,18 +351,14 @@ private struct EventTagsEnumerationState : IActivityEnumerator<KeyValuePair<stri

public bool ForEach(KeyValuePair<string, object> tag)
{
if (tag.Value is Array)
{
ProcessJaegerTagArray(ref this.Tags, tag);
}
else if (tag.Value != null)
if (JaegerTagTransformer.Instance.TryTransformTag(tag, out var result))
{
PooledList<JaegerTag>.Add(ref this.Tags, tag.ToJaegerTag());
}
PooledList<JaegerTag>.Add(ref this.Tags, result);

if (tag.Key == "event")
{
this.HasEvent = true;
if (tag.Key == "event")
{
this.HasEvent = true;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <copyright file="JaegerTagTransformer.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>

using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.Jaeger.Implementation
{
internal class JaegerTagTransformer : TagTransformer<JaegerTag>
alanwest marked this conversation as resolved.
Show resolved Hide resolved
{
public static JaegerTagTransformer Instance = new();
alanwest marked this conversation as resolved.
Show resolved Hide resolved

private JaegerTagTransformer()
{
}

protected override JaegerTag TransformIntegralTag(string key, long value)
{
return new JaegerTag(key, JaegerTagType.LONG, vLong: value);
}

protected override JaegerTag TransformFloatingPointTag(string key, double value)
{
return new JaegerTag(key, JaegerTagType.DOUBLE, vDouble: value);
}

protected override JaegerTag TransformBooleanTag(string key, bool value)
{
return new JaegerTag(key, JaegerTagType.BOOL, vBool: value);
}

protected override JaegerTag TransformStringTag(string key, string value)
{
return new JaegerTag(key, JaegerTagType.STRING, vStr: value);
}
}
}
14 changes: 0 additions & 14 deletions src/OpenTelemetry.Exporter.Jaeger/Implementation/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ public Process(string serviceName)
this.ServiceName = serviceName;
}

public Process(string serviceName, IEnumerable<KeyValuePair<string, object>> processTags)
: this(serviceName, processTags?.Select(pt => pt.ToJaegerTag()).ToDictionary(pt => pt.Key, pt => pt))
{
}

internal Process(string serviceName, Dictionary<string, JaegerTag> processTags)
: this(serviceName)
{
if (processTags != null)
{
this.Tags = processTags;
}
}

public string ServiceName { get; internal set; }

internal Dictionary<string, JaegerTag> Tags { get; set; }
Expand Down
11 changes: 7 additions & 4 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,15 @@ internal void SetResourceAndInitializeBatch(Resource resource)
}
}

if (process.Tags == null)
if (JaegerTagTransformer.Instance.TryTransformTag(label, out var result))
{
process.Tags = new Dictionary<string, JaegerTag>();
}
if (process.Tags == null)
{
process.Tags = new Dictionary<string, JaegerTag>();
}

process.Tags[key] = label.ToJaegerTag();
process.Tags[key] = result;
}
}

if (serviceName != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\TagTransformer.cs" Link="Includes\TagTransformer.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsPkgVer)" Condition="'$(TargetFramework)' != 'netstandard2.1'" />
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonPkgVer)" Condition="'$(TargetFramework)' != 'net6.0'" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ public bool ForEach(KeyValuePair<string, object> activityTag)
this.Created = true;
}

var attribute = activityTag.ToOtlpAttribute();
if (attribute != null)
if (OtlpKeyValueTransformer.Instance.TryTransformTag(activityTag, out var attribute))
{
PooledList<OtlpCommon.KeyValue>.Add(ref this.Tags, attribute);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
}
else
{
var otlpAttribute = stateValue.ToOtlpAttribute();
otlpLogRecord.Attributes.Add(otlpAttribute);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(stateValue, out var result))
alanwest marked this conversation as resolved.
Show resolved Hide resolved
{
otlpLogRecord.Attributes.Add(result);
}
}
}
}
Expand Down Expand Up @@ -150,8 +152,10 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
foreach (var scopeItem in scope)
{
var scopeItemWithDepthInfo = new KeyValuePair<string, object>($"[Scope.{scopeDepth}]:{scopeItem.Key}", scopeItem.Value);
var otlpAttribute = scopeItemWithDepthInfo.ToOtlpAttribute();
otlpLog.Attributes.Add(otlpAttribute);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItemWithDepthInfo, out var result))
{
otlpLog.Attributes.Add(result);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
{
foreach (var tag in tags)
{
attributes.Add(tag.ToOtlpAttribute());
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
{
attributes.Add(result);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,5 @@ public void CouldNotTranslateLogRecord(string exceptionMessage)
{
this.WriteEvent(9, exceptionMessage);
}

[Event(10, Message = "Unsupported attribute type '{0}' for '{1}'. Attribute will not be exported.", Level = EventLevel.Warning)]
public void UnsupportedAttributeType(string type, string key)
{
this.WriteEvent(10, type.ToString(), key);
}
}
}
Loading