Enable unique EventSource for each project (#879)

* Enable unique EventSource for each project

* updating using place from Hosting project

* InstrumentationEventSource is now internal, adding EventSource to Hosting project

* removing unused methods, updating messages
This commit is contained in:
Eddy Nakamura 2020-07-22 11:34:28 -03:00 committed by GitHub
parent 1ed4a28927
commit a2b1b45c94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 2217 additions and 1743 deletions

View File

@ -0,0 +1,81 @@
// <copyright file="PrometheusExporterEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Threading;
namespace OpenTelemetry.Exporter.Prometheus.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Exporter-Prometheus")]
internal class PrometheusExporterEventSource : EventSource
{
public static PrometheusExporterEventSource Log = new PrometheusExporterEventSource();
[NonEvent]
public void FailedExport(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.FailedExport(ToInvariantString(ex));
}
}
[NonEvent]
public void CanceledExport(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.CanceledExport(ToInvariantString(ex));
}
}
/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
/// </summary>
private static string ToInvariantString(Exception exception)
{
var originalUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
return exception.ToString();
}
finally
{
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}
}
[Event(1, Message = "Failed to export activities: '{0}'", Level = EventLevel.Error)]
private void FailedExport(string exception)
{
this.WriteEvent(1, exception);
}
[Event(2, Message = "Canceled to export activities: '{0}'", Level = EventLevel.Error)]
private void CanceledExport(string exception)
{
this.WriteEvent(2, exception);
}
}
}

View File

@ -117,13 +117,13 @@ namespace OpenTelemetry.Exporter.Prometheus
this.exporter.WriteMetricsCollection(writer);
}
}
catch (OperationCanceledException)
catch (OperationCanceledException ex)
{
// this will happen when cancellation will be requested
PrometheusExporterEventSource.Log.CanceledExport(ex);
}
catch (Exception)
catch (Exception ex)
{
// TODO: report error
PrometheusExporterEventSource.Log.FailedExport(ex);
}
finally
{

View File

@ -0,0 +1,96 @@
// <copyright file="ZPagesExporterEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Threading;
namespace OpenTelemetry.Exporter.Prometheus.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Exporter-ZPages")]
internal class ZPagesExporterEventSource : EventSource
{
public static ZPagesExporterEventSource Log = new ZPagesExporterEventSource();
[NonEvent]
public void FailedExport(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.FailedExport(ToInvariantString(ex));
}
}
[NonEvent]
public void CanceledExport(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.CanceledExport(ToInvariantString(ex));
}
}
[NonEvent]
public void FailedProcess(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.FailedProcess(ToInvariantString(ex));
}
}
/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
/// </summary>
private static string ToInvariantString(Exception exception)
{
var originalUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
return exception.ToString();
}
finally
{
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}
}
[Event(1, Message = "Failed to export activities: '{0}'", Level = EventLevel.Error)]
private void FailedExport(string exception)
{
this.WriteEvent(1, exception);
}
[Event(2, Message = "Canceled to export activities: '{0}'", Level = EventLevel.Error)]
private void CanceledExport(string exception)
{
this.WriteEvent(2, exception);
}
[Event(3, Message = "Failed to process activity: '{0}'", Level = EventLevel.Error)]
private void FailedProcess(string exception)
{
this.WriteEvent(3, exception);
}
}
}

View File

@ -20,6 +20,7 @@ using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Exporter.Prometheus.Implementation;
using OpenTelemetry.Exporter.ZPages.Implementation;
#if NET452
using OpenTelemetry.Internal;
@ -178,13 +179,13 @@ namespace OpenTelemetry.Exporter.ZPages
writer.WriteLine("</div></div></body></html>");
}
}
catch (OperationCanceledException)
catch (OperationCanceledException ex)
{
// this will happen when cancellation will be requested
ZPagesExporterEventSource.Log.CanceledExport(ex);
}
catch (Exception)
catch (Exception ex)
{
// TODO: report error
ZPagesExporterEventSource.Log.FailedExport(ex);
}
finally
{

View File

@ -18,6 +18,7 @@ using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Exporter.Prometheus.Implementation;
using OpenTelemetry.Exporter.ZPages.Implementation;
#if NET452
using OpenTelemetry.Internal;
@ -115,10 +116,10 @@ namespace OpenTelemetry.Exporter.ZPages
minuteSpanInformation.LastUpdated = new DateTimeOffset(DateTime.Now).ToUnixTimeMilliseconds();
hourSpanInformation.LastUpdated = new DateTimeOffset(DateTime.Now).ToUnixTimeMilliseconds();
}
catch (Exception exception)
catch (Exception ex)
{
// Log.SpanProcessorException("OnEnd", ex);
Console.Write("OnEnd", exception);
ZPagesExporterEventSource.Log.FailedProcess(ex);
Console.Write("OnEnd", ex);
}
}

View File

@ -0,0 +1,81 @@
// <copyright file="HostingExtensionsEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Threading;
namespace OpenTelemetry.Extensions.Hosting.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Extensions-Hosting")]
internal class HostingExtensionsEventSource : EventSource
{
public static HostingExtensionsEventSource Log = new HostingExtensionsEventSource();
[NonEvent]
public void FailedInitialize(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.FailedInitialize(ToInvariantString(ex));
}
}
[NonEvent]
public void FailedOpenTelemetrySDK(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.FailedOpenTelemetrySDK(ToInvariantString(ex));
}
}
/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
/// </summary>
private static string ToInvariantString(Exception exception)
{
var originalUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
return exception.ToString();
}
finally
{
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}
}
[Event(1, Message = "Failed to initialize: '{0}'. OpenTelemetry will not work.", Level = EventLevel.Error)]
private void FailedInitialize(string exception)
{
this.WriteEvent(1, exception);
}
[Event(2, Message = "Failed to get OpenTelemetrySDK: '{0}'. OpenTelemetry will not work.", Level = EventLevel.Error)]
private void FailedOpenTelemetrySDK(string exception)
{
this.WriteEvent(2, exception);
}
}
}

View File

@ -14,15 +14,15 @@
// limitations under the License.
// </copyright>
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace.Configuration;
namespace OpenTelemetry.Extensions.Hosting.Implementation
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace.Configuration;
internal class TelemetryHostedService : IHostedService
{
private readonly IServiceProvider serviceProvider;
@ -34,12 +34,19 @@ namespace OpenTelemetry.Extensions.Hosting.Implementation
public Task StartAsync(CancellationToken cancellationToken)
{
// The sole purpose of this HostedService is to ensure
// all instrumentations are created and started.
// This method is invoked when host starts, and
// by requesting the OpenTelemetrySdk from DI
// it ensures all instrumentations gets started.
this.serviceProvider.GetRequiredService<OpenTelemetrySdk>();
try
{
// The sole purpose of this HostedService is to ensure
// all instrumentations are created and started.
// This method is invoked when host starts, and
// by requesting the OpenTelemetrySdk from DI
// it ensures all instrumentations gets started.
this.serviceProvider.GetRequiredService<OpenTelemetrySdk>();
}
catch (Exception ex)
{
HostingExtensionsEventSource.Log.FailedOpenTelemetrySDK(ex);
}
return Task.CompletedTask;
}

View File

@ -14,14 +14,14 @@
// limitations under the License.
// </copyright>
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Extensions.Hosting.Implementation;
using OpenTelemetry.Trace.Configuration;
namespace Microsoft.Extensions.DependencyInjection
{
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Extensions.Hosting.Implementation;
using OpenTelemetry.Trace.Configuration;
/// <summary>
/// Extension methods for setting up OpenTelemetry services in an <see cref="IServiceCollection" />.
/// </summary>
@ -80,8 +80,15 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(createSdk));
}
services.AddSingleton(s => createSdk());
AddOpenTelemetryInternal(services);
try
{
services.AddSingleton(s => createSdk());
AddOpenTelemetryInternal(services);
}
catch (Exception ex)
{
HostingExtensionsEventSource.Log.FailedInitialize(ex);
}
return services;
}
@ -104,8 +111,15 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(createSdk));
}
services.AddSingleton(s => createSdk(s));
AddOpenTelemetryInternal(services);
try
{
services.AddSingleton(s => createSdk(s));
AddOpenTelemetryInternal(services);
}
catch (Exception ex)
{
HostingExtensionsEventSource.Log.FailedInitialize(ex);
}
return services;
}

View File

@ -0,0 +1,63 @@
// <copyright file="AspNetInstrumentationEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Threading;
namespace OpenTelemetry.Instrumentation.AspNet.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation-AspNet")]
internal class AspNetInstrumentationEventSource : EventSource
{
public static AspNetInstrumentationEventSource Log = new AspNetInstrumentationEventSource();
[Event(1, Message = "Payload is NULL in event '{1}' from handler '{0}', span will not be recorded.", Level = EventLevel.Warning)]
public void NullPayload(string handlerName, string eventName)
{
this.WriteEvent(1, handlerName, eventName);
}
[Event(2, Message = "Request is filtered out.", Level = EventLevel.Verbose)]
public void RequestIsFilteredOut(string eventName)
{
this.WriteEvent(2, eventName);
}
/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
/// </summary>
private static string ToInvariantString(Exception exception)
{
var originalUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
return exception.ToString();
}
finally
{
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}
}
}
}

View File

@ -1,207 +1,207 @@
// <copyright file="HttpInListener.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
// <copyright file="HttpInListener.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Web.Routing;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.AspNet.Implementation
{
internal class HttpInListener : ListenerHandler
{
private static readonly string ActivityNameByHttpInListener = "ActivityCreatedByHttpInListener";
private static readonly Func<HttpRequest, string, IEnumerable<string>> HttpRequestHeaderValuesGetter = (request, name) => request.Headers.GetValues(name);
private readonly PropertyFetcher routeFetcher = new PropertyFetcher("Route");
private readonly PropertyFetcher routeTemplateFetcher = new PropertyFetcher("RouteTemplate");
private readonly AspNetInstrumentationOptions options;
private readonly ActivitySourceAdapter activitySource;
public HttpInListener(string name, AspNetInstrumentationOptions options, ActivitySourceAdapter activitySource)
: base(name)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.activitySource = activitySource;
}
public override void OnStartActivity(Activity activity, object payload)
{
var context = HttpContext.Current;
if (context == null)
{
InstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity));
return;
}
if (this.options.RequestFilter != null && !this.options.RequestFilter(context))
{
InstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}
var request = context.Request;
var requestValues = request.Unvalidated;
if (!(this.options.TextFormat is TraceContextFormatActivity))
{
// This requires to ignore the current activity and create a new one
// using the context extracted using the format TextFormat supports.
var ctx = this.options.TextFormat.Extract(request, HttpRequestHeaderValuesGetter);
// Create a new activity with its parent set from the extracted context.
// This makes the new activity as a "sibling" of the activity created by
// Asp.Net.
Activity newOne = new Activity(ActivityNameByHttpInListener);
newOne.SetParentId(ctx.TraceId, ctx.SpanId, ctx.TraceFlags);
newOne.TraceStateString = ctx.TraceState;
// Starting the new activity make it the Activity.Current one.
newOne.Start();
// Both new activity and old one store the other activity
// inside them. This is required in the Stop step to
// correctly stop and restore Activity.Current.
newOne.SetCustomProperty("ActivityByAspNet", activity);
activity.SetCustomProperty("ActivityByHttpInListener", newOne);
activity = newOne;
}
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
var path = requestValues.Path;
activity.DisplayName = path;
activity.SetKind(ActivityKind.Server);
this.activitySource.Start(activity);
if (activity.IsAllDataRequested)
{
if (request.Url.Port == 80 || request.Url.Port == 443)
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Url.Host);
}
else
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Url.Host + ":" + request.Url.Port);
}
activity.AddTag(SemanticConventions.AttributeHTTPMethod, request.HttpMethod);
activity.AddTag(SpanAttributeConstants.HttpPathKey, path);
activity.AddTag(SemanticConventions.AttributeHTTPUserAgent, request.UserAgent);
activity.AddTag(SemanticConventions.AttributeHTTPURL, request.Url.ToString());
}
}
public override void OnStopActivity(Activity activity, object payload)
{
Activity activityToEnrich = activity;
if (!(this.options.TextFormat is TraceContextFormatActivity))
{
// If using custom context propagator, then the activity here
// could be either the one from Asp.Net, or the one
// this instrumentation created in Start.
// This is because Asp.Net, under certain circumstances, restores Activity.Current
// to its own activity.
if (activity.OperationName.Equals("Microsoft.AspNet.HttpReqIn.Start"))
{
// This block is hit if Asp.Net did restore Current to its own activity,
// then we need to retrieve the one created by HttpInListener
// and populate tags to it.
activityToEnrich = (Activity)activity.GetCustomProperty("ActivityByHttpInListener");
}
}
if (activityToEnrich.IsAllDataRequested)
{
var context = HttpContext.Current;
if (context == null)
{
InstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStopActivity));
return;
}
var response = context.Response;
activityToEnrich.AddTag(SemanticConventions.AttributeHTTPStatusCode, response.StatusCode.ToString());
activityToEnrich.SetStatus(
SpanHelper
.ResolveSpanStatusForHttpStatusCode(response.StatusCode)
.WithDescription(response.StatusDescription));
var routeData = context.Request.RequestContext.RouteData;
string template = null;
if (routeData.Values.TryGetValue("MS_SubRoutes", out object msSubRoutes))
{
// WebAPI attribute routing flows here. Use reflection to not take a dependency on microsoft.aspnet.webapi.core\[version]\lib\[framework]\System.Web.Http.
if (msSubRoutes is Array attributeRouting && attributeRouting.Length == 1)
{
var subRouteData = attributeRouting.GetValue(0);
var route = this.routeFetcher.Fetch(subRouteData);
template = this.routeTemplateFetcher.Fetch(route) as string;
}
}
else if (routeData.Route is Route route)
{
// MVC + WebAPI traditional routing & MVC attribute routing flow here.
template = route.Url;
}
if (!string.IsNullOrEmpty(template))
{
// Override the name that was previously set to the path part of URL.
activityToEnrich.DisplayName = template;
activityToEnrich.AddTag(SemanticConventions.AttributeHTTPRoute, template);
}
}
if (!(this.options.TextFormat is TraceContextFormatActivity))
{
if (activity.OperationName.Equals(ActivityNameByHttpInListener))
{
// If instrumentation started a new Activity, it must
// be stopped here.
activity.Stop();
// Restore the original activity as Current.
var activityByAspNet = (Activity)activity.GetCustomProperty("ActivityByAspNet");
Activity.Current = activityByAspNet;
}
else if (activity.OperationName.Equals("Microsoft.AspNet.HttpReqIn.Start"))
{
// This block is hit if Asp.Net did restore Current to its own activity,
// then we need to retrieve the one created by HttpInListener
// and stop it.
var activityByHttpInListener = (Activity)activity.GetCustomProperty("ActivityByHttpInListener");
activityByHttpInListener.Stop();
// Restore current back to the one created by Asp.Net
Activity.Current = activity;
}
}
this.activitySource.Stop(activityToEnrich);
}
}
}
using System.Diagnostics;
using System.Web;
using System.Web.Routing;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.AspNet.Implementation
{
internal class HttpInListener : ListenerHandler
{
private static readonly string ActivityNameByHttpInListener = "ActivityCreatedByHttpInListener";
private static readonly Func<HttpRequest, string, IEnumerable<string>> HttpRequestHeaderValuesGetter = (request, name) => request.Headers.GetValues(name);
private readonly PropertyFetcher routeFetcher = new PropertyFetcher("Route");
private readonly PropertyFetcher routeTemplateFetcher = new PropertyFetcher("RouteTemplate");
private readonly AspNetInstrumentationOptions options;
private readonly ActivitySourceAdapter activitySource;
public HttpInListener(string name, AspNetInstrumentationOptions options, ActivitySourceAdapter activitySource)
: base(name)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.activitySource = activitySource;
}
public override void OnStartActivity(Activity activity, object payload)
{
var context = HttpContext.Current;
if (context == null)
{
AspNetInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity));
return;
}
if (this.options.RequestFilter != null && !this.options.RequestFilter(context))
{
AspNetInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}
var request = context.Request;
var requestValues = request.Unvalidated;
if (!(this.options.TextFormat is TraceContextFormatActivity))
{
// This requires to ignore the current activity and create a new one
// using the context extracted using the format TextFormat supports.
var ctx = this.options.TextFormat.Extract(request, HttpRequestHeaderValuesGetter);
// Create a new activity with its parent set from the extracted context.
// This makes the new activity as a "sibling" of the activity created by
// Asp.Net.
Activity newOne = new Activity(ActivityNameByHttpInListener);
newOne.SetParentId(ctx.TraceId, ctx.SpanId, ctx.TraceFlags);
newOne.TraceStateString = ctx.TraceState;
// Starting the new activity make it the Activity.Current one.
newOne.Start();
// Both new activity and old one store the other activity
// inside them. This is required in the Stop step to
// correctly stop and restore Activity.Current.
newOne.SetCustomProperty("ActivityByAspNet", activity);
activity.SetCustomProperty("ActivityByHttpInListener", newOne);
activity = newOne;
}
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
var path = requestValues.Path;
activity.DisplayName = path;
activity.SetKind(ActivityKind.Server);
this.activitySource.Start(activity);
if (activity.IsAllDataRequested)
{
if (request.Url.Port == 80 || request.Url.Port == 443)
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Url.Host);
}
else
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Url.Host + ":" + request.Url.Port);
}
activity.AddTag(SemanticConventions.AttributeHTTPMethod, request.HttpMethod);
activity.AddTag(SpanAttributeConstants.HttpPathKey, path);
activity.AddTag(SemanticConventions.AttributeHTTPUserAgent, request.UserAgent);
activity.AddTag(SemanticConventions.AttributeHTTPURL, request.Url.ToString());
}
}
public override void OnStopActivity(Activity activity, object payload)
{
Activity activityToEnrich = activity;
if (!(this.options.TextFormat is TraceContextFormatActivity))
{
// If using custom context propagator, then the activity here
// could be either the one from Asp.Net, or the one
// this instrumentation created in Start.
// This is because Asp.Net, under certain circumstances, restores Activity.Current
// to its own activity.
if (activity.OperationName.Equals("Microsoft.AspNet.HttpReqIn.Start"))
{
// This block is hit if Asp.Net did restore Current to its own activity,
// then we need to retrieve the one created by HttpInListener
// and populate tags to it.
activityToEnrich = (Activity)activity.GetCustomProperty("ActivityByHttpInListener");
}
}
if (activityToEnrich.IsAllDataRequested)
{
var context = HttpContext.Current;
if (context == null)
{
AspNetInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStopActivity));
return;
}
var response = context.Response;
activityToEnrich.AddTag(SemanticConventions.AttributeHTTPStatusCode, response.StatusCode.ToString());
activityToEnrich.SetStatus(
SpanHelper
.ResolveSpanStatusForHttpStatusCode(response.StatusCode)
.WithDescription(response.StatusDescription));
var routeData = context.Request.RequestContext.RouteData;
string template = null;
if (routeData.Values.TryGetValue("MS_SubRoutes", out object msSubRoutes))
{
// WebAPI attribute routing flows here. Use reflection to not take a dependency on microsoft.aspnet.webapi.core\[version]\lib\[framework]\System.Web.Http.
if (msSubRoutes is Array attributeRouting && attributeRouting.Length == 1)
{
var subRouteData = attributeRouting.GetValue(0);
var route = this.routeFetcher.Fetch(subRouteData);
template = this.routeTemplateFetcher.Fetch(route) as string;
}
}
else if (routeData.Route is Route route)
{
// MVC + WebAPI traditional routing & MVC attribute routing flow here.
template = route.Url;
}
if (!string.IsNullOrEmpty(template))
{
// Override the name that was previously set to the path part of URL.
activityToEnrich.DisplayName = template;
activityToEnrich.AddTag(SemanticConventions.AttributeHTTPRoute, template);
}
}
if (!(this.options.TextFormat is TraceContextFormatActivity))
{
if (activity.OperationName.Equals(ActivityNameByHttpInListener))
{
// If instrumentation started a new Activity, it must
// be stopped here.
activity.Stop();
// Restore the original activity as Current.
var activityByAspNet = (Activity)activity.GetCustomProperty("ActivityByAspNet");
Activity.Current = activityByAspNet;
}
else if (activity.OperationName.Equals("Microsoft.AspNet.HttpReqIn.Start"))
{
// This block is hit if Asp.Net did restore Current to its own activity,
// then we need to retrieve the one created by HttpInListener
// and stop it.
var activityByHttpInListener = (Activity)activity.GetCustomProperty("ActivityByHttpInListener");
activityByHttpInListener.Stop();
// Restore current back to the one created by Asp.Net
Activity.Current = activity;
}
}
this.activitySource.Stop(activityToEnrich);
}
}
}

View File

@ -0,0 +1,63 @@
// <copyright file="AspNetCoreInstrumentationEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Threading;
namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation-AspNetCore")]
internal class AspNetCoreInstrumentationEventSource : EventSource
{
public static AspNetCoreInstrumentationEventSource Log = new AspNetCoreInstrumentationEventSource();
[Event(1, Message = "Payload is NULL in event '{1}' from handler '{0}', span will not be recorded.", Level = EventLevel.Warning)]
public void NullPayload(string handlerName, string eventName)
{
this.WriteEvent(1, handlerName, eventName);
}
[Event(2, Message = "Request is filtered out.", Level = EventLevel.Verbose)]
public void RequestIsFilteredOut(string eventName)
{
this.WriteEvent(2, eventName);
}
/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
/// </summary>
private static string ToInvariantString(Exception exception)
{
var originalUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
return exception.ToString();
}
finally
{
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}
}
}
}

View File

@ -1,224 +1,224 @@
// <copyright file="HttpInListener.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
// <copyright file="HttpInListener.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
{
internal class HttpInListener : ListenerHandler
{
private static readonly string UnknownHostName = "UNKNOWN-HOST";
private static readonly string ActivityNameByHttpInListener = "ActivityCreatedByHttpInListener";
private static readonly Func<HttpRequest, string, IEnumerable<string>> HttpRequestHeaderValuesGetter = (request, name) => request.Headers[name];
private readonly PropertyFetcher startContextFetcher = new PropertyFetcher("HttpContext");
private readonly PropertyFetcher stopContextFetcher = new PropertyFetcher("HttpContext");
private readonly PropertyFetcher beforeActionActionDescriptorFetcher = new PropertyFetcher("actionDescriptor");
private readonly PropertyFetcher beforeActionAttributeRouteInfoFetcher = new PropertyFetcher("AttributeRouteInfo");
private readonly PropertyFetcher beforeActionTemplateFetcher = new PropertyFetcher("Template");
private readonly bool hostingSupportsW3C = false;
private readonly AspNetCoreInstrumentationOptions options;
private readonly ActivitySourceAdapter activitySource;
public HttpInListener(string name, AspNetCoreInstrumentationOptions options, ActivitySourceAdapter activitySource)
: base(name)
{
this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3;
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.activitySource = activitySource;
}
public override void OnStartActivity(Activity activity, object payload)
{
var context = this.startContextFetcher.Fetch(payload) as HttpContext;
if (context == null)
{
InstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity));
return;
}
if (this.options.RequestFilter != null && !this.options.RequestFilter(context))
{
InstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}
var request = context.Request;
if (!this.hostingSupportsW3C || !(this.options.TextFormat is TraceContextFormatActivity))
{
// This requires to ignore the current activity and create a new one
// using the context extracted from w3ctraceparent header or
// using the format TextFormat supports.
var ctx = this.options.TextFormat.Extract(request, HttpRequestHeaderValuesGetter);
// Create a new activity with its parent set from the extracted context.
// This makes the new activity as a "sibling" of the activity created by
// Asp.Net Core.
Activity newOne = new Activity(ActivityNameByHttpInListener);
newOne.SetParentId(ctx.TraceId, ctx.SpanId, ctx.TraceFlags);
newOne.TraceStateString = ctx.TraceState;
// Starting the new activity make it the Activity.Current one.
newOne.Start();
activity = newOne;
}
// TODO: move setting displayname to inside IsAllDataRequested?
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
activity.DisplayName = path;
activity.SetKind(ActivityKind.Server);
this.activitySource.Start(activity);
if (activity.IsAllDataRequested)
{
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
if (request.Host.Port == 80 || request.Host.Port == 443)
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Host.Host);
}
else
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Host.Host + ":" + request.Host.Port);
}
activity.AddTag(SemanticConventions.AttributeHTTPMethod, request.Method);
activity.AddTag(SpanAttributeConstants.HttpPathKey, path);
activity.AddTag(SemanticConventions.AttributeHTTPURL, GetUri(request));
var userAgent = request.Headers["User-Agent"].FirstOrDefault();
if (!string.IsNullOrEmpty(userAgent))
{
activity.AddTag(SemanticConventions.AttributeHTTPUserAgent, userAgent);
}
}
}
public override void OnStopActivity(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
{
if (!(this.stopContextFetcher.Fetch(payload) is HttpContext context))
{
InstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStopActivity));
return;
}
var response = context.Response;
activity.AddTag(SemanticConventions.AttributeHTTPStatusCode, response.StatusCode.ToString());
Status status = SpanHelper.ResolveSpanStatusForHttpStatusCode(response.StatusCode);
activity.SetStatus(status.WithDescription(response.HttpContext.Features.Get<IHttpResponseFeature>()?.ReasonPhrase));
}
if (activity.OperationName.Equals(ActivityNameByHttpInListener))
{
// If instrumentation started a new Activity, it must
// be stopped here.
activity.Stop();
// After the activity.Stop() code, Activity.Current becomes null.
// If Asp.Net Core uses Activity.Current?.Stop() - it'll not stop the activity
// it created.
// Currently Asp.Net core does not use Activity.Current, instead it stores a
// reference to its activity, and calls .Stop on it.
// TODO: Should we still restore Activity.Current here?
// If yes, then we need to store the asp.net core activity inside
// the one created by the instrumentation.
// And retrieve it here, and set it to Current.
}
this.activitySource.Stop(activity);
}
public override void OnCustom(string name, Activity activity, object payload)
{
if (name == "Microsoft.AspNetCore.Mvc.BeforeAction")
{
if (activity.IsAllDataRequested)
{
// See https://github.com/aspnet/Mvc/blob/2414db256f32a047770326d14d8b0e2afd49ba49/src/Microsoft.AspNetCore.Mvc.Core/MvcCoreDiagnosticSourceExtensions.cs#L36-L44
// Reflection accessing: ActionDescriptor.AttributeRouteInfo.Template
// The reason to use reflection is to avoid a reference on MVC package.
// This package can be used with non-MVC apps and this logic simply wouldn't run.
// Taking reference on MVC will increase size of deployment for non-MVC apps.
var actionDescriptor = this.beforeActionActionDescriptorFetcher.Fetch(payload);
var attributeRouteInfo = this.beforeActionAttributeRouteInfoFetcher.Fetch(actionDescriptor);
var template = this.beforeActionTemplateFetcher.Fetch(attributeRouteInfo) as string;
if (!string.IsNullOrEmpty(template))
{
// override the span name that was previously set to the path part of URL.
activity.DisplayName = template;
activity.AddTag(SemanticConventions.AttributeHTTPRoute, template);
}
// TODO: Should we get values from RouteData?
// private readonly PropertyFetcher beforActionRouteDataFetcher = new PropertyFetcher("routeData");
// var routeData = this.beforActionRouteDataFetcher.Fetch(payload) as RouteData;
}
}
}
private static string GetUri(HttpRequest request)
{
var builder = new StringBuilder();
builder.Append(request.Scheme).Append("://");
if (request.Host.HasValue)
{
builder.Append(request.Host.Value);
}
else
{
// HTTP 1.0 request with NO host header would result in empty Host.
// Use placeholder to avoid incorrect URL like "http:///"
builder.Append(UnknownHostName);
}
if (request.PathBase.HasValue)
{
builder.Append(request.PathBase.Value);
}
if (request.Path.HasValue)
{
builder.Append(request.Path.Value);
}
if (request.QueryString.HasValue)
{
builder.Append(request.QueryString);
}
return builder.ToString();
}
}
}
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
{
internal class HttpInListener : ListenerHandler
{
private static readonly string UnknownHostName = "UNKNOWN-HOST";
private static readonly string ActivityNameByHttpInListener = "ActivityCreatedByHttpInListener";
private static readonly Func<HttpRequest, string, IEnumerable<string>> HttpRequestHeaderValuesGetter = (request, name) => request.Headers[name];
private readonly PropertyFetcher startContextFetcher = new PropertyFetcher("HttpContext");
private readonly PropertyFetcher stopContextFetcher = new PropertyFetcher("HttpContext");
private readonly PropertyFetcher beforeActionActionDescriptorFetcher = new PropertyFetcher("actionDescriptor");
private readonly PropertyFetcher beforeActionAttributeRouteInfoFetcher = new PropertyFetcher("AttributeRouteInfo");
private readonly PropertyFetcher beforeActionTemplateFetcher = new PropertyFetcher("Template");
private readonly bool hostingSupportsW3C = false;
private readonly AspNetCoreInstrumentationOptions options;
private readonly ActivitySourceAdapter activitySource;
public HttpInListener(string name, AspNetCoreInstrumentationOptions options, ActivitySourceAdapter activitySource)
: base(name)
{
this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3;
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.activitySource = activitySource;
}
public override void OnStartActivity(Activity activity, object payload)
{
var context = this.startContextFetcher.Fetch(payload) as HttpContext;
if (context == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity));
return;
}
if (this.options.RequestFilter != null && !this.options.RequestFilter(context))
{
AspNetCoreInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}
var request = context.Request;
if (!this.hostingSupportsW3C || !(this.options.TextFormat is TraceContextFormatActivity))
{
// This requires to ignore the current activity and create a new one
// using the context extracted from w3ctraceparent header or
// using the format TextFormat supports.
var ctx = this.options.TextFormat.Extract(request, HttpRequestHeaderValuesGetter);
// Create a new activity with its parent set from the extracted context.
// This makes the new activity as a "sibling" of the activity created by
// Asp.Net Core.
Activity newOne = new Activity(ActivityNameByHttpInListener);
newOne.SetParentId(ctx.TraceId, ctx.SpanId, ctx.TraceFlags);
newOne.TraceStateString = ctx.TraceState;
// Starting the new activity make it the Activity.Current one.
newOne.Start();
activity = newOne;
}
// TODO: move setting displayname to inside IsAllDataRequested?
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
activity.DisplayName = path;
activity.SetKind(ActivityKind.Server);
this.activitySource.Start(activity);
if (activity.IsAllDataRequested)
{
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
if (request.Host.Port == 80 || request.Host.Port == 443)
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Host.Host);
}
else
{
activity.AddTag(SemanticConventions.AttributeHTTPHost, request.Host.Host + ":" + request.Host.Port);
}
activity.AddTag(SemanticConventions.AttributeHTTPMethod, request.Method);
activity.AddTag(SpanAttributeConstants.HttpPathKey, path);
activity.AddTag(SemanticConventions.AttributeHTTPURL, GetUri(request));
var userAgent = request.Headers["User-Agent"].FirstOrDefault();
if (!string.IsNullOrEmpty(userAgent))
{
activity.AddTag(SemanticConventions.AttributeHTTPUserAgent, userAgent);
}
}
}
public override void OnStopActivity(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
{
if (!(this.stopContextFetcher.Fetch(payload) is HttpContext context))
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStopActivity));
return;
}
var response = context.Response;
activity.AddTag(SemanticConventions.AttributeHTTPStatusCode, response.StatusCode.ToString());
Status status = SpanHelper.ResolveSpanStatusForHttpStatusCode(response.StatusCode);
activity.SetStatus(status.WithDescription(response.HttpContext.Features.Get<IHttpResponseFeature>()?.ReasonPhrase));
}
if (activity.OperationName.Equals(ActivityNameByHttpInListener))
{
// If instrumentation started a new Activity, it must
// be stopped here.
activity.Stop();
// After the activity.Stop() code, Activity.Current becomes null.
// If Asp.Net Core uses Activity.Current?.Stop() - it'll not stop the activity
// it created.
// Currently Asp.Net core does not use Activity.Current, instead it stores a
// reference to its activity, and calls .Stop on it.
// TODO: Should we still restore Activity.Current here?
// If yes, then we need to store the asp.net core activity inside
// the one created by the instrumentation.
// And retrieve it here, and set it to Current.
}
this.activitySource.Stop(activity);
}
public override void OnCustom(string name, Activity activity, object payload)
{
if (name == "Microsoft.AspNetCore.Mvc.BeforeAction")
{
if (activity.IsAllDataRequested)
{
// See https://github.com/aspnet/Mvc/blob/2414db256f32a047770326d14d8b0e2afd49ba49/src/Microsoft.AspNetCore.Mvc.Core/MvcCoreDiagnosticSourceExtensions.cs#L36-L44
// Reflection accessing: ActionDescriptor.AttributeRouteInfo.Template
// The reason to use reflection is to avoid a reference on MVC package.
// This package can be used with non-MVC apps and this logic simply wouldn't run.
// Taking reference on MVC will increase size of deployment for non-MVC apps.
var actionDescriptor = this.beforeActionActionDescriptorFetcher.Fetch(payload);
var attributeRouteInfo = this.beforeActionAttributeRouteInfoFetcher.Fetch(actionDescriptor);
var template = this.beforeActionTemplateFetcher.Fetch(attributeRouteInfo) as string;
if (!string.IsNullOrEmpty(template))
{
// override the span name that was previously set to the path part of URL.
activity.DisplayName = template;
activity.AddTag(SemanticConventions.AttributeHTTPRoute, template);
}
// TODO: Should we get values from RouteData?
// private readonly PropertyFetcher beforActionRouteDataFetcher = new PropertyFetcher("routeData");
// var routeData = this.beforActionRouteDataFetcher.Fetch(payload) as RouteData;
}
}
}
private static string GetUri(HttpRequest request)
{
var builder = new StringBuilder();
builder.Append(request.Scheme).Append("://");
if (request.Host.HasValue)
{
builder.Append(request.Host.Value);
}
else
{
// HTTP 1.0 request with NO host header would result in empty Host.
// Use placeholder to avoid incorrect URL like "http:///"
builder.Append(UnknownHostName);
}
if (request.PathBase.HasValue)
{
builder.Append(request.PathBase.Value);
}
if (request.Path.HasValue)
{
builder.Append(request.Path.Value);
}
if (request.QueryString.HasValue)
{
builder.Append(request.QueryString);
}
return builder.ToString();
}
}
}

View File

@ -0,0 +1,114 @@
// <copyright file="DependenciesInstrumentationEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Threading;
namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation-Dependencies")]
internal class DependenciesInstrumentationEventSource : EventSource
{
public static DependenciesInstrumentationEventSource Log = new DependenciesInstrumentationEventSource();
[NonEvent]
public void UnknownErrorProcessingEvent(string handlerName, string eventName, Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.UnknownErrorProcessingEvent(handlerName, eventName, ToInvariantString(ex));
}
}
[NonEvent]
public void FailedProcessResult(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.FailedProcessResult(ToInvariantString(ex));
}
}
[NonEvent]
public void ExceptionInitializingInstrumentation(string instrumentationType, Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.ExceptionInitializingInstrumentation(instrumentationType, ToInvariantString(ex));
}
}
[Event(4, Message = "Current Activity is NULL the '{0}' callback. Span will not be recorded.", Level = EventLevel.Warning)]
public void NullActivity(string eventName)
{
this.WriteEvent(4, eventName);
}
[Event(5, Message = "Payload is NULL in event '{1}' from handler '{0}', span will not be recorded.", Level = EventLevel.Warning)]
public void NullPayload(string handlerName, string eventName)
{
this.WriteEvent(5, handlerName, eventName);
}
[Event(6, Message = "Payload is invalid in event '{1}' from handler '{0}', span will not be recorded.", Level = EventLevel.Warning)]
public void InvalidPayload(string handlerName, string eventName)
{
this.WriteEvent(6, handlerName, eventName);
}
/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
/// </summary>
private static string ToInvariantString(Exception exception)
{
var originalUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
return exception.ToString();
}
finally
{
Thread.CurrentThread.CurrentUICulture = originalUICulture;
}
}
[Event(1, Message = "Unknown error processing event '{1}' from handler '{0}', Exception: {2}", Level = EventLevel.Error)]
private void UnknownErrorProcessingEvent(string handlerName, string eventName, string ex)
{
this.WriteEvent(1, handlerName, eventName, ex);
}
[Event(2, Message = "Failed to process result: '{0}'", Level = EventLevel.Error)]
private void FailedProcessResult(string ex)
{
this.WriteEvent(2, ex);
}
[Event(3, Message = "Error initializing instrumentation type {0}. Exception : {1}", Level = EventLevel.Error)]
private void ExceptionInitializingInstrumentation(string instrumentationType, string ex)
{
this.WriteEvent(3, instrumentationType, ex);
}
}
}

View File

@ -41,7 +41,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
{
if (!(this.startRequestFetcher.Fetch(payload) is HttpRequestMessage request))
{
InstrumentationEventSource.Log.NullPayload(nameof(GrpcClientDiagnosticListener), nameof(this.OnStartActivity));
DependenciesInstrumentationEventSource.Log.NullPayload(nameof(GrpcClientDiagnosticListener), nameof(this.OnStartActivity));
return;
}

View File

@ -1,34 +1,34 @@
// <copyright file="HttpHandlerDiagnosticListener.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
// <copyright file="HttpHandlerDiagnosticListener.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
{
internal class HttpHandlerDiagnosticListener : ListenerHandler
{
using System.Diagnostics;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
{
internal class HttpHandlerDiagnosticListener : ListenerHandler
{
private static readonly Func<HttpRequestMessage, string, IEnumerable<string>> HttpRequestMessageHeaderValuesGetter = (request, name) =>
{
if (request.Headers.TryGetValues(name, out var values))
@ -40,142 +40,142 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
};
private static readonly Action<HttpRequestMessage, string, string> HttpRequestMessageHeaderValueSetter = (request, name, value) => request.Headers.Add(name, value);
private static readonly Regex CoreAppMajorVersionCheckRegex = new Regex("^\\.NETCoreApp,Version=v(\\d+)\\.", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private readonly ActivitySourceAdapter activitySource;
private readonly PropertyFetcher startRequestFetcher = new PropertyFetcher("Request");
private readonly PropertyFetcher stopResponseFetcher = new PropertyFetcher("Response");
private readonly PropertyFetcher stopExceptionFetcher = new PropertyFetcher("Exception");
private readonly PropertyFetcher stopRequestStatusFetcher = new PropertyFetcher("RequestTaskStatus");
private readonly bool httpClientSupportsW3C = false;
private readonly HttpClientInstrumentationOptions options;
public HttpHandlerDiagnosticListener(HttpClientInstrumentationOptions options, ActivitySourceAdapter activitySource)
: base("HttpHandlerDiagnosticListener")
{
var framework = Assembly
.GetEntryAssembly()?
.GetCustomAttribute<TargetFrameworkAttribute>()?
.FrameworkName;
// Depending on the .NET version/flavor this will look like
// '.NETCoreApp,Version=v3.0', '.NETCoreApp,Version = v2.2' or '.NETFramework,Version = v4.7.1'
if (framework != null)
{
var match = CoreAppMajorVersionCheckRegex.Match(framework);
this.httpClientSupportsW3C = match.Success && int.Parse(match.Groups[1].Value) >= 3;
}
this.options = options;
this.activitySource = activitySource;
}
public override void OnStartActivity(Activity activity, object payload)
{
if (!(this.startRequestFetcher.Fetch(payload) is HttpRequestMessage request))
{
InstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerDiagnosticListener), nameof(this.OnStartActivity));
return;
}
if (this.options.TextFormat.IsInjected(request, HttpRequestMessageHeaderValuesGetter))
{
// this request is already instrumented, we should back off
activity.IsAllDataRequested = false;
return;
}
activity.SetKind(ActivityKind.Client);
activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);
this.activitySource.Start(activity);
if (activity.IsAllDataRequested)
{
activity.AddTag(SemanticConventions.AttributeHTTPMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
activity.AddTag(SemanticConventions.AttributeHTTPHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
activity.AddTag(SemanticConventions.AttributeHTTPURL, request.RequestUri.OriginalString);
if (this.options.SetHttpFlavor)
{
activity.AddTag(SemanticConventions.AttributeHTTPFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version));
}
}
if (!(this.httpClientSupportsW3C && this.options.TextFormat is TraceContextFormatActivity))
{
this.options.TextFormat.Inject(activity.Context, request, HttpRequestMessageHeaderValueSetter);
}
}
public override void OnStopActivity(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
{
var requestTaskStatus = this.stopRequestStatusFetcher.Fetch(payload) as TaskStatus?;
if (requestTaskStatus.HasValue)
{
if (requestTaskStatus != TaskStatus.RanToCompletion)
{
if (requestTaskStatus == TaskStatus.Canceled)
{
activity.SetStatus(Status.Cancelled);
}
else if (requestTaskStatus != TaskStatus.Faulted)
{
// Faults are handled in OnException and should already have a span.Status of Unknown w/ Description.
activity.SetStatus(Status.Unknown);
}
}
}
if (this.stopResponseFetcher.Fetch(payload) is HttpResponseMessage response)
{
// response could be null for DNS issues, timeouts, etc...
activity.AddTag(SemanticConventions.AttributeHTTPStatusCode, response.StatusCode.ToString());
activity.SetStatus(
SpanHelper
.ResolveSpanStatusForHttpStatusCode((int)response.StatusCode)
.WithDescription(response.ReasonPhrase));
}
}
this.activitySource.Stop(activity);
}
public override void OnException(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
{
if (!(this.stopExceptionFetcher.Fetch(payload) is Exception exc))
{
InstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerDiagnosticListener), nameof(this.OnException));
return;
}
if (exc is HttpRequestException)
{
if (exc.InnerException is SocketException exception)
{
switch (exception.SocketErrorCode)
{
case SocketError.HostNotFound:
activity.SetStatus(Status.InvalidArgument.WithDescription(exc.Message));
return;
}
}
if (exc.InnerException != null)
{
activity.SetStatus(Status.Unknown.WithDescription(exc.Message));
}
}
}
}
}
}
private static readonly Regex CoreAppMajorVersionCheckRegex = new Regex("^\\.NETCoreApp,Version=v(\\d+)\\.", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private readonly ActivitySourceAdapter activitySource;
private readonly PropertyFetcher startRequestFetcher = new PropertyFetcher("Request");
private readonly PropertyFetcher stopResponseFetcher = new PropertyFetcher("Response");
private readonly PropertyFetcher stopExceptionFetcher = new PropertyFetcher("Exception");
private readonly PropertyFetcher stopRequestStatusFetcher = new PropertyFetcher("RequestTaskStatus");
private readonly bool httpClientSupportsW3C = false;
private readonly HttpClientInstrumentationOptions options;
public HttpHandlerDiagnosticListener(HttpClientInstrumentationOptions options, ActivitySourceAdapter activitySource)
: base("HttpHandlerDiagnosticListener")
{
var framework = Assembly
.GetEntryAssembly()?
.GetCustomAttribute<TargetFrameworkAttribute>()?
.FrameworkName;
// Depending on the .NET version/flavor this will look like
// '.NETCoreApp,Version=v3.0', '.NETCoreApp,Version = v2.2' or '.NETFramework,Version = v4.7.1'
if (framework != null)
{
var match = CoreAppMajorVersionCheckRegex.Match(framework);
this.httpClientSupportsW3C = match.Success && int.Parse(match.Groups[1].Value) >= 3;
}
this.options = options;
this.activitySource = activitySource;
}
public override void OnStartActivity(Activity activity, object payload)
{
if (!(this.startRequestFetcher.Fetch(payload) is HttpRequestMessage request))
{
DependenciesInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerDiagnosticListener), nameof(this.OnStartActivity));
return;
}
if (this.options.TextFormat.IsInjected(request, HttpRequestMessageHeaderValuesGetter))
{
// this request is already instrumented, we should back off
activity.IsAllDataRequested = false;
return;
}
activity.SetKind(ActivityKind.Client);
activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);
this.activitySource.Start(activity);
if (activity.IsAllDataRequested)
{
activity.AddTag(SemanticConventions.AttributeHTTPMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
activity.AddTag(SemanticConventions.AttributeHTTPHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
activity.AddTag(SemanticConventions.AttributeHTTPURL, request.RequestUri.OriginalString);
if (this.options.SetHttpFlavor)
{
activity.AddTag(SemanticConventions.AttributeHTTPFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version));
}
}
if (!(this.httpClientSupportsW3C && this.options.TextFormat is TraceContextFormatActivity))
{
this.options.TextFormat.Inject(activity.Context, request, HttpRequestMessageHeaderValueSetter);
}
}
public override void OnStopActivity(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
{
var requestTaskStatus = this.stopRequestStatusFetcher.Fetch(payload) as TaskStatus?;
if (requestTaskStatus.HasValue)
{
if (requestTaskStatus != TaskStatus.RanToCompletion)
{
if (requestTaskStatus == TaskStatus.Canceled)
{
activity.SetStatus(Status.Cancelled);
}
else if (requestTaskStatus != TaskStatus.Faulted)
{
// Faults are handled in OnException and should already have a span.Status of Unknown w/ Description.
activity.SetStatus(Status.Unknown);
}
}
}
if (this.stopResponseFetcher.Fetch(payload) is HttpResponseMessage response)
{
// response could be null for DNS issues, timeouts, etc...
activity.AddTag(SemanticConventions.AttributeHTTPStatusCode, response.StatusCode.ToString());
activity.SetStatus(
SpanHelper
.ResolveSpanStatusForHttpStatusCode((int)response.StatusCode)
.WithDescription(response.ReasonPhrase));
}
}
this.activitySource.Stop(activity);
}
public override void OnException(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
{
if (!(this.stopExceptionFetcher.Fetch(payload) is Exception exc))
{
DependenciesInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerDiagnosticListener), nameof(this.OnException));
return;
}
if (exc is HttpRequestException)
{
if (exc.InnerException is SocketException exception)
{
switch (exception.SocketErrorCode)
{
case SocketError.HostNotFound:
activity.SetStatus(Status.InvalidArgument.WithDescription(exc.Message));
return;
}
}
if (exc.InnerException != null)
{
activity.SetStatus(Status.Unknown.WithDescription(exc.Message));
}
}
}
}
}
}

View File

@ -76,7 +76,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
var command = this.commandFetcher.Fetch(payload);
if (command == null)
{
InstrumentationEventSource.Log.NullPayload(nameof(SqlClientDiagnosticListener), name);
DependenciesInstrumentationEventSource.Log.NullPayload(nameof(SqlClientDiagnosticListener), name);
activity.Stop();
return;
}
@ -132,7 +132,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
{
if (activity == null)
{
InstrumentationEventSource.Log.NullActivity(name);
DependenciesInstrumentationEventSource.Log.NullActivity(name);
return;
}
@ -160,7 +160,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
{
if (activity == null)
{
InstrumentationEventSource.Log.NullActivity(name);
DependenciesInstrumentationEventSource.Log.NullActivity(name);
return;
}
@ -179,7 +179,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
}
else
{
InstrumentationEventSource.Log.NullPayload(nameof(SqlClientDiagnosticListener), name);
DependenciesInstrumentationEventSource.Log.NullPayload(nameof(SqlClientDiagnosticListener), name);
}
}
}

View File

@ -77,7 +77,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
}
catch (Exception exc)
{
InstrumentationEventSource.Log.UnknownErrorProcessingEvent(nameof(SqlEventSourceListener), nameof(this.OnEventWritten), exc);
DependenciesInstrumentationEventSource.Log.UnknownErrorProcessingEvent(nameof(SqlEventSourceListener), nameof(this.OnEventWritten), exc);
}
}
@ -93,7 +93,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
if ((eventData?.Payload?.Count ?? 0) < 4)
{
InstrumentationEventSource.Log.InvalidPayload(nameof(SqlEventSourceListener), nameof(this.OnBeginExecute));
DependenciesInstrumentationEventSource.Log.InvalidPayload(nameof(SqlEventSourceListener), nameof(this.OnBeginExecute));
return;
}
@ -142,7 +142,7 @@ namespace OpenTelemetry.Instrumentation.Dependencies.Implementation
if ((eventData?.Payload?.Count ?? 0) < 3)
{
InstrumentationEventSource.Log.InvalidPayload(nameof(SqlEventSourceListener), nameof(this.OnEndExecute));
DependenciesInstrumentationEventSource.Log.InvalidPayload(nameof(SqlEventSourceListener), nameof(this.OnEndExecute));
return;
}

View File

@ -25,76 +25,29 @@ namespace OpenTelemetry.Instrumentation
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation")]
public class InstrumentationEventSource : EventSource
internal class InstrumentationEventSource : EventSource
{
public static InstrumentationEventSource Log = new InstrumentationEventSource();
[Event(1, Message = "Span is NULL or blank in the '{0}' callback. Span will not be recorded.", Level = EventLevel.Warning)]
public void NullOrBlankSpan(string eventName)
{
this.WriteEvent(1, eventName);
}
[Event(2, Message = "Error getting custom sampler, the default sampler will be used. Exception : {0}", Level = EventLevel.Warning)]
public void ExceptionInCustomSampler(string ex)
{
this.WriteEvent(2, ex);
}
[Event(3, Message = "Current Activity is NULL the '{0}' callback. Span will not be recorded.", Level = EventLevel.Warning)]
[Event(1, Message = "Current Activity is NULL the '{0}' callback. Span will not be recorded.", Level = EventLevel.Warning)]
public void NullActivity(string eventName)
{
this.WriteEvent(3, eventName);
this.WriteEvent(1, eventName);
}
[NonEvent]
public void UnknownErrorProcessingEvent(string handlerName, string eventName, Exception ex)
{
if (!this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
return;
}
this.UnknownErrorProcessingEvent(handlerName, eventName, ToInvariantString(ex));
}
[Event(4, Message = "Unknown error processing event '{1}' from handler '{0}', Exception: {2}", Level = EventLevel.Error)]
public void UnknownErrorProcessingEvent(string handlerName, string eventName, string ex)
{
this.WriteEvent(4, handlerName, eventName, ex);
}
[Event(5, Message = "Payload is NULL in event '{1}' from handler '{0}', span will not be recorded.", Level = EventLevel.Warning)]
public void NullPayload(string handlerName, string eventName)
{
this.WriteEvent(5, handlerName, eventName);
}
[Event(6, Message = "Request is filtered out.", Level = EventLevel.Verbose)]
public void RequestIsFilteredOut(string eventName)
{
this.WriteEvent(6, eventName);
}
[NonEvent]
public void ExceptionInitializingInstrumentation(string instrumentationType, Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.ExceptionInitializingInstrumentation(instrumentationType, ToInvariantString(ex));
this.UnknownErrorProcessingEvent(handlerName, eventName, ToInvariantString(ex));
}
}
[Event(7, Message = "Error initializing instrumentation type {0}. Exception : {1}", Level = EventLevel.Error)]
public void ExceptionInitializingInstrumentation(string instrumentationType, string ex)
[Event(2, Message = "Unknown error processing event '{1}' from handler '{0}', Exception: {2}", Level = EventLevel.Error)]
public void UnknownErrorProcessingEvent(string handlerName, string eventName, string ex)
{
this.WriteEvent(7, instrumentationType, ex);
}
[Event(8, Message = "Payload is invalid in event '{1}' from handler '{0}', span will not be recorded.", Level = EventLevel.Warning)]
public void InvalidPayload(string handlerName, string eventName)
{
this.WriteEvent(8, handlerName, eventName);
this.WriteEvent(2, handlerName, eventName, ex);
}
/// <summary>