Skip to content

Commit

Permalink
AspNet.TelemetryHttpModule public api + test fixes + renames (#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Aug 10, 2021
1 parent 8fcc12e commit a8f5491
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 154 deletions.
2 changes: 1 addition & 1 deletion examples/AspNet/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
<modules>
<add name="TelemetryCorrelationHttpModule" type="OpenTelemetry.Instrumentation.AspNet.TelemetryCorrelationHttpModule, OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule" preCondition="integratedMode,managedHandler"/>
<add name="TelemetryHttpModule" type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule, OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule" preCondition="integratedMode,managedHandler"/>
</modules>
</system.webServer>
<runtime>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OpenTelemetry.Instrumentation.AspNet.ActivityExtensions
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Dispose() -> void
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Init(System.Web.HttpApplication context) -> void
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.ParseHeaders.get -> bool
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.ParseHeaders.set -> void
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.TelemetryHttpModule() -> void
static OpenTelemetry.Instrumentation.AspNet.ActivityExtensions.Extract(this System.Diagnostics.Activity activity, System.Collections.Specialized.NameValueCollection requestHeaders) -> bool
static OpenTelemetry.Instrumentation.AspNet.ActivityExtensions.TryParse(this System.Diagnostics.Activity activity, System.Collections.Specialized.NameValueCollection requestHeaders) -> bool
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ public static bool Extract(this Activity activity, NameValueCollection requestHe
{
if (activity == null)
{
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("activity is null");
AspNetTelemetryEventSource.Log.ActvityExtractionError("activity is null");
return false;
}

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

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

Expand Down Expand Up @@ -131,7 +131,7 @@ public static bool Extract(this Activity activity, NameValueCollection requestHe
}
else
{
AspNetTelemetryCorrelationEventSource.Log.HeaderParsingError(CorrelationContextHeaderName, pair);
AspNetTelemetryEventSource.Log.HeaderParsingError(CorrelationContextHeaderName, pair);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static class ActivityHelper
/// <summary>
/// Listener name.
/// </summary>
public const string AspNetListenerName = "Microsoft.AspNet.TelemetryCorrelation";
public const string AspNetListenerName = "OpenTelemetry.Instrumentation.AspNet.Telemetry";

/// <summary>
/// Activity name for http request.
Expand Down Expand Up @@ -72,7 +72,7 @@ public static void StopAspNetActivity(IDictionary contextItems)
contextItems[ActivityKey] = null;
}

AspNetTelemetryCorrelationEventSource.Log.ActivityStopped(currentActivity?.Id, currentActivity?.OperationName);
AspNetTelemetryEventSource.Log.ActivityStopped(currentActivity?.Id, currentActivity?.OperationName);
}

/// <summary>
Expand All @@ -97,7 +97,7 @@ public static Activity CreateRootActivity(HttpContext context, bool parseHeaders
if (StartAspNetActivity(rootActivity))
{
context.Items[ActivityKey] = rootActivity;
AspNetTelemetryCorrelationEventSource.Log.ActivityStarted(rootActivity.Id);
AspNetTelemetryEventSource.Log.ActivityStarted(rootActivity.Id);
return rootActivity;
}
}
Expand All @@ -117,7 +117,7 @@ public static void WriteActivityException(IDictionary contextItems, Exception ex
}

AspNetListener.Write(aspNetActivity.OperationName + ".Exception", exception);
AspNetTelemetryCorrelationEventSource.Log.ActivityException(aspNetActivity.Id, aspNetActivity.OperationName, exception);
AspNetTelemetryEventSource.Log.ActivityException(aspNetActivity.Id, aspNetActivity.OperationName, exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="AspNetTelemetryCorrelationEventSource.cs" company="OpenTelemetry Authors">
// <copyright file="AspNetTelemetryEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,20 +16,19 @@

using System;
using System.Diagnostics.Tracing;
#pragma warning disable SA1600 // Elements must be documented

namespace OpenTelemetry.Instrumentation.AspNet
{
/// <summary>
/// ETW EventSource tracing class.
/// </summary>
[EventSource(Name = "Microsoft-AspNet-Telemetry-Correlation", Guid = "ace2021e-e82c-5502-d81d-657f27612673")]
internal sealed class AspNetTelemetryCorrelationEventSource : EventSource
[EventSource(Name = "OpenTelemetry-Instrumentation-AspNet-Telemetry", Guid = "1de158cc-f7ce-4293-bd19-2358c93c8186")]
internal sealed class AspNetTelemetryEventSource : EventSource
{
/// <summary>
/// Instance of the PlatformEventSource class.
/// </summary>
public static readonly AspNetTelemetryCorrelationEventSource Log = new AspNetTelemetryCorrelationEventSource();
public static readonly AspNetTelemetryEventSource Log = new AspNetTelemetryEventSource();

[NonEvent]
public void ActivityException(string id, string eventName, Exception ex)
Expand Down Expand Up @@ -107,4 +106,3 @@ public void ActivityException(string id, string eventName, string ex)
}
}
}
#pragma warning restore SA1600 // Elements must be documented
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected BaseHeaderParser(bool supportsMultipleValues)

public sealed override bool TryParseValue(string value, ref int index, out T parsedValue)
{
parsedValue = default(T);
parsedValue = default;

// If multiple values are supported (i.e. list of values), then accept an empty string: The header may
// be added multiple times to the request/response message. E.g.
Expand All @@ -38,8 +38,7 @@ public sealed override bool TryParseValue(string value, ref int index, out T par
return this.SupportsMultipleValues;
}

var separatorFound = false;
var current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, index, this.SupportsMultipleValues, out separatorFound);
var current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, index, this.SupportsMultipleValues, out var separatorFound);

if (separatorFound && !this.SupportsMultipleValues)
{
Expand All @@ -56,15 +55,14 @@ public sealed override bool TryParseValue(string value, ref int index, out T par
return this.SupportsMultipleValues;
}

T result;
var length = this.GetParsedValueLength(value, current, out result);
var length = this.GetParsedValueLength(value, current, out var result);

if (length == 0)
{
return false;
}

current = current + length;
current += length;
current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, current, this.SupportsMultipleValues, out separatorFound);

// If we support multiple values and we've not reached the end of the string, then we must have a separator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ namespace OpenTelemetry.Instrumentation.AspNet
// Adoptation of code from https://github.com/aspnet/HttpAbstractions/blob/07d115400e4f8c7a66ba239f230805f03a14ee3d/src/Microsoft.Net.Http.Headers/GenericHeaderParser.cs
internal sealed class GenericHeaderParser<T> : BaseHeaderParser<T>
{
private GetParsedValueLengthDelegate getParsedValueLength;
private readonly GetParsedValueLengthDelegate getParsedValueLength;

internal GenericHeaderParser(bool supportsMultipleValues, GetParsedValueLengthDelegate getParsedValueLength)
: base(supportsMultipleValues)
{
if (getParsedValueLength == null)
{
throw new ArgumentNullException(nameof(getParsedValueLength));
}

this.getParsedValueLength = getParsedValueLength;
this.getParsedValueLength = getParsedValueLength ?? throw new ArgumentNullException(nameof(getParsedValueLength));
}

internal delegate int GetParsedValueLengthDelegate(string value, int startIndex, out T parsedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ internal static int GetNextNonEmptyOrWhitespaceIndex(
// empty values, continue until the current character is neither a separator nor a whitespace.
separatorFound = true;
current++; // skip delimiter.
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
current += HttpRuleParser.GetWhitespaceLength(input, current);

if (skipEmptyValues)
{
while ((current < input.Length) && (input[current] == ','))
{
current++; // skip delimiter.
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
current += HttpRuleParser.GetWhitespaceLength(input, current);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ internal static HttpParseResult GetQuotedPairLength(string input, int startIndex
{
Contract.Requires(input != null);
Contract.Requires((startIndex >= 0) && (startIndex < input.Length));
Contract.Ensures((Contract.ValueAtReturn(out length) >= 0) &&
(Contract.ValueAtReturn(out length) <= (input.Length - startIndex)));
Contract.Ensures((Contract.ValueAtReturn<int>(out _) >= 0) &&
(Contract.ValueAtReturn<int>(out _) <= (input.Length - startIndex)));

length = 0;

Expand Down Expand Up @@ -202,7 +202,7 @@ private static HttpParseResult GetExpressionLength(
Contract.Requires(input != null);
Contract.Requires((startIndex >= 0) && (startIndex < input.Length));
Contract.Ensures((Contract.Result<HttpParseResult>() != HttpParseResult.Parsed) ||
(Contract.ValueAtReturn<int>(out length) > 0));
(Contract.ValueAtReturn<int>(out _) > 0));

length = 0;

Expand All @@ -216,14 +216,13 @@ private static HttpParseResult GetExpressionLength(
{
// Only check whether we have a quoted char, if we have at least 3 characters left to read (i.e.
// quoted char + closing char). Otherwise the closing char may be considered part of the quoted char.
var quotedPairLength = 0;
if ((current + 2 < input.Length) &&
(GetQuotedPairLength(input, current, out quotedPairLength) == HttpParseResult.Parsed))
(GetQuotedPairLength(input, current, out var quotedPairLength) == HttpParseResult.Parsed))
{
// We ignore invalid quoted-pairs. Invalid quoted-pairs may mean that it looked like a quoted pair,
// but we actually have a quoted-string: e.g. '\' followed by a char >127 - quoted-pair only
// allows ASCII chars after '\'; qdtext allows both '\' and >127 chars.
current = current + quotedPairLength;
current += quotedPairLength;
continue;
}

Expand All @@ -239,8 +238,7 @@ private static HttpParseResult GetExpressionLength(
return HttpParseResult.InvalidFormat;
}

var nestedLength = 0;
HttpParseResult nestedResult = GetExpressionLength(input, current, openChar, closeChar, supportsNesting, ref nestedCount, out nestedLength);
HttpParseResult nestedResult = GetExpressionLength(input, current, openChar, closeChar, supportsNesting, ref nestedCount, out var nestedLength);

switch (nestedResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,35 @@ private static int GetNameValueLength(string input, int startIndex, out NameValu

var name = input.Substring(startIndex, nameLength);
var current = startIndex + nameLength;
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
current += HttpRuleParser.GetWhitespaceLength(input, current);

// Parse the separator between name and value
if ((current == input.Length) || (input[current] != '='))
{
// We only have a name and that's OK. Return.
parsedValue = new NameValueHeaderValue();
parsedValue.name = name;
current = current + HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespaces
parsedValue = new NameValueHeaderValue
{
name = name,
};
current += HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespaces
return current - startIndex;
}

current++; // skip delimiter.
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
current += HttpRuleParser.GetWhitespaceLength(input, current);

// Parse the value, i.e. <value> in name/value string "<name>=<value>"
int valueLength = GetValueLength(input, current);

// Value after the '=' may be empty
// Use parameterless ctor to avoid double-parsing of name and value, i.e. skip public ctor validation.
parsedValue = new NameValueHeaderValue();
parsedValue.name = name;
parsedValue.value = input.Substring(current, valueLength);
current = current + valueLength;
current = current + HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespaces
parsedValue = new NameValueHeaderValue
{
name = name,
value = input.Substring(current, valueLength),
};
current += valueLength;
current += HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespaces
return current - startIndex;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
<TargetFrameworks>net461</TargetFrameworks>
<Description>A module that instruments incoming request with System.Diagnostics.Activity and notifies listeners with DiagnosticsSource.</Description>
<PackageTags>$(PackageTags);distributed-tracing;AspNet;MVC;WebAPI</PackageTags>
<!--
TODO: Disable this exception, and actually do document all public API.
-->
<NoWarn>$(NoWarn),1591,CS0618</NoWarn>
<MinVerTagPrefix>core-</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<Reference Include="System.Web" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticSourcePkgVer)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Telemetry correlation http module

[![NuGet](https://img.shields.io/nuget/v/Microsoft.AspNet.TelemetryCorrelation.svg)](https://www.nuget.org/packages/Microsoft.AspNet.TelemetryCorrelation/)
[![NuGet](https://img.shields.io/nuget/v/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/)

Telemetry correlation http module enables cross tier telemetry tracking.

Expand Down Expand Up @@ -30,8 +30,7 @@ Telemetry correlation http module enables cross tier telemetry tracking.

public void OnNext(DiagnosticListener listener)
{
if (listener.Name == "Microsoft.AspNet.TelemetryCorrelation" ||
listener.Name == "System.Net.Http" )
if (listener.Name == "OpenTelemetry.Instrumentation.AspNet.Telemetry")
{
listener.Subscribe(new NoopDiagnosticsListener());
}
Expand Down
Loading

0 comments on commit a8f5491

Please sign in to comment.