This repository has been archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Move W3C classes to base SDK so everyone can use them #1138
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d962102
Move W3C classes to base and deprecate them in DependencyCollector
217513d
up
43c56c2
changelog
d22e1ad
up
427a627
minor fixes
7b4c1f8
review
94e2340
more fixes
99ee669
more fixes
393022d
:up:
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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> | ||
|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?