Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

Move W3C classes to base SDK so everyone can use them #1138

Merged
merged 9 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 2.9.0
- [Fix: remove unused reference to Microsoft.AspNet.TelemetryCorrelation package from DependencyCollector](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/1136)
- [Move W3C support from DependencyCollector package to base SDK, deprecate W3C support in DependencyCollector](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/1138)

## Version 2.9.0-beta3
- Update Base SDK to version 2.9.0-beta3
Expand Down
69 changes: 10 additions & 59 deletions Src/Common/StringUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace Microsoft.ApplicationInsights.Common.Internal
using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.ApplicationInsights.Extensibility.W3C;
#if DEPENDENCY_COLLECTOR
using Microsoft.ApplicationInsights.W3C;
using Microsoft.ApplicationInsights.W3C;
#else
using Microsoft.ApplicationInsights.W3C.Internal;
#endif
Expand All @@ -17,14 +18,12 @@ namespace Microsoft.ApplicationInsights.Common.Internal
/// Generic functions to perform common operations on a string.
/// </summary>
#if DEPENDENCY_COLLECTOR
public
public
#else
internal
#endif
static class StringUtilities
{
private static readonly uint[] Lookup32 = CreateLookup32();

/// <summary>
/// Check a strings length and trim to a max length if needed.
/// </summary>
Expand All @@ -47,19 +46,21 @@ public static string EnforceMaxLength(string input, int maxLength)
/// https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md#trace-id
/// </summary>
/// <returns>Random 16 bytes array encoded as hex string</returns>
[Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CUtilities.GenerateTraceId in Microsoft.ApplicationInsights package instead.")]
public static string GenerateTraceId()
{
return GenerateId(Guid.NewGuid().ToByteArray(), 0, 16);
return W3CUtilities.GenerateTraceId();
}

/// <summary>
/// Generates random span Id as per W3C Distributed tracing specification.
/// https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md#span-id
/// </summary>
/// <returns>Random 8 bytes array encoded as hex string</returns>
[Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CUtilities.GenerateTraceId in Microsoft.ApplicationInsights package instead.")]
public static string GenerateSpanId()
{
return GenerateId(Guid.NewGuid().ToByteArray(), 0, 8);
return W3CUtilities.GenerateTraceId().Substring(0, 16);
}

/// <summary>
Expand All @@ -68,65 +69,15 @@ public static string GenerateSpanId()
/// <param name="traceId">Trace Id.</param>
/// <param name="spanId">Span id.</param>
/// <returns>valid Request-Id.</returns>
[Obsolete("Obsolete, implement yourself with 'string.Concat(\"|\", traceId, \".\", spanId, \".\").'")]
public static string FormatRequestId(string traceId, string spanId)
{
return String.Concat("|", traceId, ".", spanId, ".");
}

/// <summary>
/// Gets root id (string between '|' and the first dot) from the hierarchical Id.
/// </summary>
/// <param name="hierarchicalId">Id to extract root from.</param>
/// <returns>Root operation id.</returns>
internal static string GetRootId(string hierarchicalId)
{
// Returns the root Id from the '|' to the first '.' if any.
int rootEnd = hierarchicalId.IndexOf('.');
if (rootEnd < 0)
{
rootEnd = hierarchicalId.Length;
}

int rootStart = hierarchicalId[0] == '|' ? 1 : 0;
return hierarchicalId.Substring(rootStart, rootEnd - rootStart);
return string.Concat("|", traceId, ".", spanId, ".");
}

#pragma warning disable 612, 618
internal static string FormatAzureTracestate(string appId)
{
return String.Concat(W3CConstants.AzureTracestateNamespace, "=", appId);
}
#pragma warning restore 612, 618

/// <summary>
/// Converts byte array to hex lower case string.
/// </summary>
/// <returns>Array encoded as hex string</returns>
private static string GenerateId(byte[] bytes, int start, int length)
{
// See https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/24343727#24343727
var result = new char[length * 2];
for (int i = start; i < start + length; i++)
{
var val = Lookup32[bytes[i]];
result[2 * i] = (char)val;
result[(2 * i) + 1] = (char)(val >> 16);
}

return new string(result);
}

private static uint[] CreateLookup32()
{
// See https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/24343727#24343727
var result = new uint[256];
for (int i = 0; i < 256; i++)
{
string s = i.ToString("x2", CultureInfo.InvariantCulture);
result[i] = ((uint)s[0]) + ((uint)s[1] << 16);
}

return result;
return string.Concat(W3CConstants.AzureTracestateNamespace, "=", appId);
}
}
}
Loading