Skip to content

Commit

Permalink
initial cleanup (#2224)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored and cijothomas committed Aug 11, 2021
1 parent 717ad74 commit 405e7dc
Show file tree
Hide file tree
Showing 37 changed files with 3,170 additions and 3,496 deletions.
52 changes: 0 additions & 52 deletions src/Microsoft.AspNet.TelemetryCorrelation/.gitattributes

This file was deleted.

15 changes: 0 additions & 15 deletions src/Microsoft.AspNet.TelemetryCorrelation/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions src/Microsoft.AspNet.TelemetryCorrelation/.nuget/NuGet.Config

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,146 +1,146 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;

namespace Microsoft.AspNet.TelemetryCorrelation
{
/// <summary>
/// Extensions of Activity class
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ActivityExtensions
{
/// <summary>
/// Http header name to carry the Request Id: https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/HttpCorrelationProtocol.md.
/// </summary>
internal const string RequestIdHeaderName = "Request-Id";

/// <summary>
/// Http header name to carry the traceparent: https://www.w3.org/TR/trace-context/.
/// </summary>
internal const string TraceparentHeaderName = "traceparent";

/// <summary>
/// Http header name to carry the tracestate: https://www.w3.org/TR/trace-context/.
/// </summary>
internal const string TracestateHeaderName = "tracestate";

/// <summary>
/// Http header name to carry the correlation context.
/// </summary>
internal const string CorrelationContextHeaderName = "Correlation-Context";

/// <summary>
/// Maximum length of Correlation-Context header value.
/// </summary>
internal const int MaxCorrelationContextLength = 1024;

/// <summary>
/// Reads Request-Id and Correlation-Context headers and sets ParentId and Baggage on Activity.
/// </summary>
/// <param name="activity">Instance of activity that has not been started yet.</param>
/// <param name="requestHeaders">Request headers collection.</param>
/// <returns>true if request was parsed successfully, false - otherwise.</returns>
public static bool Extract(this Activity activity, NameValueCollection requestHeaders)
{
if (activity == null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("activity is null");
return false;
}

if (activity.ParentId != null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("ParentId is already set on activity");
return false;
}

if (activity.Id != null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("Activity is already started");
return false;
}

var parents = requestHeaders.GetValues(TraceparentHeaderName);
if (parents == null || parents.Length == 0)
{
parents = requestHeaders.GetValues(RequestIdHeaderName);
}

if (parents != null && parents.Length > 0 && !string.IsNullOrEmpty(parents[0]))
{
// there may be several Request-Id or traceparent headers, but we only read the first one
activity.SetParentId(parents[0]);

var tracestates = requestHeaders.GetValues(TracestateHeaderName);
if (tracestates != null && tracestates.Length > 0)
{
if (tracestates.Length == 1 && !string.IsNullOrEmpty(tracestates[0]))
{
activity.TraceStateString = tracestates[0];
}
else
{
activity.TraceStateString = string.Join(",", tracestates);
}
}

// Header format - Correlation-Context: key1=value1, key2=value2
var baggages = requestHeaders.GetValues(CorrelationContextHeaderName);
if (baggages != null)
{
int correlationContextLength = -1;

// there may be several Correlation-Context header
foreach (var item in baggages)
{
if (correlationContextLength >= MaxCorrelationContextLength)
{
break;
}

foreach (var pair in item.Split(','))
{
correlationContextLength += pair.Length + 1; // pair and comma

if (correlationContextLength >= MaxCorrelationContextLength)
{
break;
}

if (NameValueHeaderValue.TryParse(pair, out NameValueHeaderValue baggageItem))
{
activity.AddBaggage(baggageItem.Name, baggageItem.Value);
}
else
{
AspNetTelemetryCorrelationEventSource.Log.HeaderParsingError(CorrelationContextHeaderName, pair);
}
}
}
}

return true;
}

return false;
}

/// <summary>
/// Reads Request-Id and Correlation-Context headers and sets ParentId and Baggage on Activity.
/// </summary>
/// <param name="activity">Instance of activity that has not been started yet.</param>
/// <param name="requestHeaders">Request headers collection.</param>
/// <returns>true if request was parsed successfully, false - otherwise.</returns>
[Obsolete("Method is obsolete, use Extract method instead", true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool TryParse(this Activity activity, NameValueCollection requestHeaders)
{
return Extract(activity, requestHeaders);
}
}
}
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;

namespace Microsoft.AspNet.TelemetryCorrelation
{
/// <summary>
/// Extensions of Activity class
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ActivityExtensions
{
/// <summary>
/// Http header name to carry the Request Id: https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/HttpCorrelationProtocol.md.
/// </summary>
internal const string RequestIdHeaderName = "Request-Id";

/// <summary>
/// Http header name to carry the traceparent: https://www.w3.org/TR/trace-context/.
/// </summary>
internal const string TraceparentHeaderName = "traceparent";

/// <summary>
/// Http header name to carry the tracestate: https://www.w3.org/TR/trace-context/.
/// </summary>
internal const string TracestateHeaderName = "tracestate";

/// <summary>
/// Http header name to carry the correlation context.
/// </summary>
internal const string CorrelationContextHeaderName = "Correlation-Context";

/// <summary>
/// Maximum length of Correlation-Context header value.
/// </summary>
internal const int MaxCorrelationContextLength = 1024;

/// <summary>
/// Reads Request-Id and Correlation-Context headers and sets ParentId and Baggage on Activity.
/// </summary>
/// <param name="activity">Instance of activity that has not been started yet.</param>
/// <param name="requestHeaders">Request headers collection.</param>
/// <returns>true if request was parsed successfully, false - otherwise.</returns>
public static bool Extract(this Activity activity, NameValueCollection requestHeaders)
{
if (activity == null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("activity is null");
return false;
}

if (activity.ParentId != null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("ParentId is already set on activity");
return false;
}

if (activity.Id != null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("Activity is already started");
return false;
}

var parents = requestHeaders.GetValues(TraceparentHeaderName);
if (parents == null || parents.Length == 0)
{
parents = requestHeaders.GetValues(RequestIdHeaderName);
}

if (parents != null && parents.Length > 0 && !string.IsNullOrEmpty(parents[0]))
{
// there may be several Request-Id or traceparent headers, but we only read the first one
activity.SetParentId(parents[0]);

var tracestates = requestHeaders.GetValues(TracestateHeaderName);
if (tracestates != null && tracestates.Length > 0)
{
if (tracestates.Length == 1 && !string.IsNullOrEmpty(tracestates[0]))
{
activity.TraceStateString = tracestates[0];
}
else
{
activity.TraceStateString = string.Join(",", tracestates);
}
}

// Header format - Correlation-Context: key1=value1, key2=value2
var baggages = requestHeaders.GetValues(CorrelationContextHeaderName);
if (baggages != null)
{
int correlationContextLength = -1;

// there may be several Correlation-Context header
foreach (var item in baggages)
{
if (correlationContextLength >= MaxCorrelationContextLength)
{
break;
}

foreach (var pair in item.Split(','))
{
correlationContextLength += pair.Length + 1; // pair and comma

if (correlationContextLength >= MaxCorrelationContextLength)
{
break;
}

if (NameValueHeaderValue.TryParse(pair, out NameValueHeaderValue baggageItem))
{
activity.AddBaggage(baggageItem.Name, baggageItem.Value);
}
else
{
AspNetTelemetryCorrelationEventSource.Log.HeaderParsingError(CorrelationContextHeaderName, pair);
}
}
}
}

return true;
}

return false;
}

/// <summary>
/// Reads Request-Id and Correlation-Context headers and sets ParentId and Baggage on Activity.
/// </summary>
/// <param name="activity">Instance of activity that has not been started yet.</param>
/// <param name="requestHeaders">Request headers collection.</param>
/// <returns>true if request was parsed successfully, false - otherwise.</returns>
[Obsolete("Method is obsolete, use Extract method instead", true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool TryParse(this Activity activity, NameValueCollection requestHeaders)
{
return Extract(activity, requestHeaders);
}
}
}
Loading

0 comments on commit 405e7dc

Please sign in to comment.