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 5 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: 8 additions & 61 deletions Src/Common/StringUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ namespace Microsoft.ApplicationInsights.Common.Internal
using System;
using System.Diagnostics;
using System.Globalization;
#if DEPENDENCY_COLLECTOR
using Microsoft.ApplicationInsights.W3C;
#else
using Microsoft.ApplicationInsights.Extensibility.W3C;
using Microsoft.ApplicationInsights.W3C.Internal;
#endif

/// <summary>
/// Generic functions to perform common operations on a string.
Expand All @@ -23,8 +20,6 @@ namespace Microsoft.ApplicationInsights.Common.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 +42,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 instead.", true)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it break compilation? It can be fine if we didn't have this feature in any stable (I remember we didn't), so migrating between stables won't suddenly stop compiling, right?

Copy link
Contributor

@cijothomas cijothomas Jan 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also mention the name of the nuget package as well - Microsoft.ApplicationInsights

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 instead.", true)]
public static string GenerateSpanId()
{
return GenerateId(Guid.NewGuid().ToByteArray(), 0, 8);
return W3CUtilities.GenerateTraceId().Substring(0, 16);
}

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

#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