Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomProperty improvements #1261

Merged
merged 10 commits into from
Oct 6, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using System.Web;
using OpenTelemetry.Context.Propagation;

Expand All @@ -41,5 +42,16 @@ public class AspNetInstrumentationOptions
/// If Filter returns false or throw exception, the request is filtered out.
/// </summary>
public Func<HttpContext, bool> Filter { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
eddynaka marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,20 @@ public void RequestFilterException(string exception)
{
this.WriteEvent(3, exception);
}

[NonEvent]
public void EnrichmentException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.EnrichmentException(ex.ToInvariantString());
}
}

[Event(4, Message = "Enrichment threw exception. Exception {0}.", Level = EventLevel.Error)]
public void EnrichmentException(string exception)
{
this.WriteEvent(4, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ public override void OnStartActivity(Activity activity, object payload)

if (activity.IsAllDataRequested)
{
activity.SetCustomProperty(RequestCustomPropertyName, request);
try
{
this.options.Enrich?.Invoke(activity, "OnStartActivity", request);
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.EnrichmentException(ex);
}

if (request.Url.Port == 80 || request.Url.Port == 443)
{
activity.SetTag(SemanticConventions.AttributeHttpHost, request.Url.Host);
Expand Down Expand Up @@ -159,7 +167,15 @@ public override void OnStopActivity(Activity activity, object payload)

var response = context.Response;

activityToEnrich.SetCustomProperty(ResponseCustomPropertyName, response);
try
{
this.options.Enrich?.Invoke(activity, "OnStopActivity", response);
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.EnrichmentException(ex);
}

activityToEnrich.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode);

activityToEnrich.SetStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using OpenTelemetry.Context.Propagation;

Expand All @@ -41,5 +42,16 @@ public class AspNetCoreInstrumentationOptions
/// If Filter returns false or throw exception, the request is filtered out.
/// </summary>
public Func<HttpContext, bool> Filter { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,20 @@ public void RequestFilterException(string exception)
{
this.WriteEvent(3, exception);
}

[NonEvent]
public void EnrichmentException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.EnrichmentException(ex.ToInvariantString());
}
}

[Event(4, Message = "Enrichment threw exception. Exception {0}.", Level = EventLevel.Error)]
public void EnrichmentException(string exception)
{
this.WriteEvent(4, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ public override void OnStartActivity(Activity activity, object payload)

if (activity.IsAllDataRequested)
{
activity.SetCustomProperty(RequestCustomPropertyName, request);
try
{
this.options.Enrich?.Invoke(activity, "OnStartActivity", request);
}
catch (Exception ex)
{
AspNetCoreInstrumentationEventSource.Log.EnrichmentException(ex);
}

var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
activity.DisplayName = path;
Expand Down Expand Up @@ -152,7 +159,16 @@ public override void OnStopActivity(Activity activity, object payload)
}

var response = context.Response;
activity.SetCustomProperty(ResponseCustomPropertyName, response);

try
{
this.options.Enrich?.Invoke(activity, "OnStopActivity", response);
}
catch (Exception ex)
{
AspNetCoreInstrumentationEventSource.Log.EnrichmentException(ex);
}

activity.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode);

if (TryGetGrpcMethod(activity, out var grpcMethod))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// limitations under the License.
// </copyright>

using System;
using System.Diagnostics;

namespace OpenTelemetry.Instrumentation.GrpcNetClient
{
/// <summary>
Expand All @@ -25,5 +28,16 @@ public class GrpcClientInstrumentationOptions
/// Gets or sets a value indicating whether down stream instrumentation is suppressed (disabled).
/// </summary>
public bool SuppressDownstreamInstrumentation { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ public override void OnStartActivity(Activity activity, object payload)

if (activity.IsAllDataRequested)
{
activity.SetCustomProperty(RequestCustomPropertyName, request);
try
{
this.options.Enrich?.Invoke(activity, "OnStartActivity", request);
}
catch (Exception ex)
{
GrpcInstrumentationEventSource.Log.EnrichmentException(ex);
}

activity.SetTag(SemanticConventions.AttributeRpcSystem, GrpcTagHelper.RpcSystemGrpc);

if (GrpcTagHelper.TryParseRpcServiceAndRpcMethod(grpcMethod, out var rpcService, out var rpcMethod))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// limitations under the License.
// </copyright>

using System;
using System.Diagnostics.Tracing;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Instrumentation.GrpcNetClient.Implementation
{
Expand All @@ -31,5 +33,20 @@ public void NullPayload(string handlerName, string eventName)
{
this.WriteEvent(1, handlerName, eventName);
}

[NonEvent]
public void EnrichmentException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.EnrichmentException(ex.ToInvariantString());
}
}

[Event(2, Message = "Enrichment thrw exception. Exception {0}.", Level = EventLevel.Error)]
public void EnrichmentException(string exception)
{
this.WriteEvent(2, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Internal\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Trace\SemanticConventions.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Runtime.CompilerServices;
using OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -50,6 +51,17 @@ public class HttpClientInstrumentationOptions
/// </summary>
public Func<HttpRequestMessage, bool> Filter { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool EventFilter(string activityName, object arg1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ public override void OnStartActivity(Activity activity, object payload)

if (activity.IsAllDataRequested)
{
activity.SetCustomProperty(RequestCustomPropertyName, request);
try
{
this.options.Enrich?.Invoke(activity, "OnStartActivity", request);
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.EnrichmentException(ex);
}

activity.SetTag(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
activity.SetTag(SemanticConventions.AttributeHttpUrl, request.RequestUri.OriginalString);
Expand Down Expand Up @@ -139,7 +147,14 @@ public override void OnStopActivity(Activity activity, object payload)

if (this.stopResponseFetcher.Fetch(payload) is HttpResponseMessage response)
{
activity.SetCustomProperty(ResponseCustomPropertyName, response);
try
{
this.options.Enrich?.Invoke(activity, "OnStopActivity", response);
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.EnrichmentException(ex);
}

activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);

Expand All @@ -163,7 +178,14 @@ public override void OnException(Activity activity, object payload)
return;
}

activity.SetCustomProperty(ExceptionCustomPropertyName, exc);
try
{
this.options.Enrich?.Invoke(activity, "OnException", exc);
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.EnrichmentException(ex);
}

if (exc is HttpRequestException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,20 @@ public void RequestFilterException(string exception)
{
this.WriteEvent(4, exception);
}

[NonEvent]
public void EnrichmentException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.EnrichmentException(ex.ToInvariantString());
}
}

[Event(5, Message = "Enrichment thrw exception. Exception {0}.", Level = EventLevel.Error)]
public void EnrichmentException(string exception)
{
this.WriteEvent(5, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ public override void OnCustom(string name, Activity activity, object payload)
var database = this.databaseFetcher.Fetch(connection);

activity.DisplayName = (string)database;
activity.SetCustomProperty(CommandCustomPropertyName, command);
try
{
this.options.Enrich?.Invoke(activity, "OnCustom", command);
}
catch (Exception ex)
{
SqlClientInstrumentationEventSource.Log.EnrichmentException(ex);
}

var dataSource = this.dataSourceFetcher.Fetch(connection);
var commandText = this.commandTextFetcher.Fetch(command);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,20 @@ public void InvalidPayload(string handlerName, string eventName)
{
this.WriteEvent(4, handlerName, eventName);
}

[NonEvent]
public void EnrichmentException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.EnrichmentException(ex.ToInvariantString());
}
}

[Event(5, Message = "Enrichment thrw exception. Exception {0}.", Level = EventLevel.Error)]
public void EnrichmentException(string exception)
{
this.WriteEvent(5, exception);
}
}
}
Loading