Checking for null value and type to add tag (#1097)
* Checking type before adding to tags * Checking if value is null, adding more tests * changing order to prevent useless conversion Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
parent
0d00b8df1c
commit
25d9a4befe
|
|
@ -146,6 +146,43 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation
|
|||
return microseconds - UnixEpochMicroseconds;
|
||||
}
|
||||
|
||||
internal static bool ProcessTags(ref AttributeEnumerationState state, KeyValuePair<string, object> attribute)
|
||||
{
|
||||
if (attribute.Value == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (attribute.Value is string strVal)
|
||||
{
|
||||
string key = attribute.Key;
|
||||
if (RemoteEndpointServiceNameKeyResolutionDictionary.TryGetValue(key, out int priority)
|
||||
&& (state.RemoteEndpointServiceName == null || priority < state.RemoteEndpointServiceNamePriority))
|
||||
{
|
||||
state.RemoteEndpointServiceName = strVal;
|
||||
state.RemoteEndpointServiceNamePriority = priority;
|
||||
}
|
||||
else if (key == Resource.ServiceNameKey)
|
||||
{
|
||||
state.ServiceName = strVal;
|
||||
}
|
||||
else if (key == Resource.ServiceNamespaceKey)
|
||||
{
|
||||
state.ServiceNamespace = strVal;
|
||||
}
|
||||
else
|
||||
{
|
||||
PooledList<KeyValuePair<string, object>>.Add(ref state.Tags, new KeyValuePair<string, object>(key, strVal));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PooledList<KeyValuePair<string, object>>.Add(ref state.Tags, attribute);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string EncodeTraceId(ActivityTraceId traceId, bool useShortTraceIds)
|
||||
{
|
||||
var id = traceId.ToHexString();
|
||||
|
|
@ -176,41 +213,7 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation
|
|||
return true;
|
||||
}
|
||||
|
||||
private static bool ProcessTags(ref AttributeEnumerationState state, KeyValuePair<string, object> attribute)
|
||||
{
|
||||
string key = attribute.Key;
|
||||
string strVal = attribute.Value as string;
|
||||
|
||||
if (strVal != null)
|
||||
{
|
||||
if (RemoteEndpointServiceNameKeyResolutionDictionary.TryGetValue(key, out int priority)
|
||||
&& (state.RemoteEndpointServiceName == null || priority < state.RemoteEndpointServiceNamePriority))
|
||||
{
|
||||
state.RemoteEndpointServiceName = strVal;
|
||||
state.RemoteEndpointServiceNamePriority = priority;
|
||||
}
|
||||
else if (key == Resource.ServiceNameKey)
|
||||
{
|
||||
state.ServiceName = strVal;
|
||||
}
|
||||
else if (key == Resource.ServiceNamespaceKey)
|
||||
{
|
||||
state.ServiceNamespace = strVal;
|
||||
}
|
||||
else
|
||||
{
|
||||
PooledList<KeyValuePair<string, object>>.Add(ref state.Tags, new KeyValuePair<string, object>(key, strVal));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PooledList<KeyValuePair<string, object>>.Add(ref state.Tags, new KeyValuePair<string, object>(key, strVal));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private struct AttributeEnumerationState
|
||||
internal struct AttributeEnumerationState
|
||||
{
|
||||
public PooledList<KeyValuePair<string, object>> Tags;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
// <copyright file="ZipkinActivityConversionExtensionsTest.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 System.Collections.Generic;
|
||||
using OpenTelemetry.Internal;
|
||||
using Xunit;
|
||||
using static OpenTelemetry.Exporter.Zipkin.Implementation.ZipkinActivityConversionExtensions;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Zipkin.Tests.Implementation
|
||||
{
|
||||
public class ZipkinActivityConversionExtensionsTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("int", 1)]
|
||||
[InlineData("string", "s")]
|
||||
[InlineData("bool", true)]
|
||||
[InlineData("double", 1.0)]
|
||||
public void CheckProcessTag(string key, object value)
|
||||
{
|
||||
var attributeEnumerationState = new AttributeEnumerationState
|
||||
{
|
||||
Tags = PooledList<KeyValuePair<string, object>>.Create(),
|
||||
};
|
||||
ProcessTags(ref attributeEnumerationState, new KeyValuePair<string, object>(key, value));
|
||||
|
||||
Assert.Equal(key, attributeEnumerationState.Tags[0].Key);
|
||||
Assert.Equal(value, attributeEnumerationState.Tags[0].Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("int", null)]
|
||||
[InlineData("string", null)]
|
||||
[InlineData("bool", null)]
|
||||
[InlineData("double", null)]
|
||||
public void CheckNullValueProcessTag(string key, object value)
|
||||
{
|
||||
var attributeEnumerationState = new AttributeEnumerationState
|
||||
{
|
||||
Tags = PooledList<KeyValuePair<string, object>>.Create(),
|
||||
};
|
||||
ProcessTags(ref attributeEnumerationState, new KeyValuePair<string, object>(key, value));
|
||||
|
||||
Assert.Empty(attributeEnumerationState.Tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue