Skip to content

Commit

Permalink
Merge branch 'master' into cijothomas/activity3adapter1a_alternate
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Jun 2, 2020
2 parents 737f9b6 + 96e1eb9 commit 036a9bb
Show file tree
Hide file tree
Showing 22 changed files with 967 additions and 951 deletions.
16 changes: 15 additions & 1 deletion samples/Exporters/AspNet/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System.Web;
using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Configuration;

namespace OpenTelemetry.Exporter.AspNet
{
public class WebApiApplication : HttpApplication
{
private TracerFactory tracerFactory;
private IDisposable openTelemetry;

protected void Application_Start()
{
Expand All @@ -24,6 +27,16 @@ protected void Application_Start()
.AddDependencyInstrumentation();
});

TracerFactoryBase.SetDefault(this.tracerFactory);

this.openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
(builder) => builder.AddDependencyInstrumentation()
.UseJaegerActivityExporter(c =>
{
c.AgentHost = "localhost";
c.AgentPort = 6831;
}));

GlobalConfiguration.Configure(WebApiConfig.Register);

AreaRegistration.RegisterAllAreas();
Expand All @@ -33,6 +46,7 @@ protected void Application_Start()
protected void Application_End()
{
this.tracerFactory?.Dispose();
this.openTelemetry?.Dispose();
}
}
}
4 changes: 4 additions & 0 deletions samples/Exporters/AspNet/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<assemblyIdentity name="System.Memory" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Api/Trace/SpanAttributeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ public static class SpanAttributeConstants
public const string ComponentKey = "component";
public const string PeerServiceKey = "peer.service";

public const string StatusCodeKey = "ot.status_code";
public const string StatusDescriptionKey = "ot.status_description";

public const string HttpMethodKey = "http.method";
public const string HttpSchemeKey = "http.scheme";
public const string HttpTargetKey = "http.target";
public const string HttpStatusCodeKey = "http.status_code";
public const string HttpStatusTextKey = "http.status_text";
public const string HttpUserAgentKey = "http.user_agent";
public const string HttpPathKey = "http.path";
public const string HttpHostKey = "http.host";
Expand Down
58 changes: 18 additions & 40 deletions src/OpenTelemetry.Api/Trace/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public static TelemetrySpan PutHttpHostAttribute(this TelemetrySpan span, string
return span;
}

/// <summary>
/// Helper method that populates span properties from host and port
/// to https://github.com/open-telemetry/opentelemetry-specification/blob/2316771e7e0ca3bfe9b2286d13e3a41ded6b8858/specification/data-http.md.
/// </summary>
/// <param name="span">Span to fill out.</param>
/// <param name="hostAndPort">Host and port value.</param>
/// <returns>Span with populated host properties.</returns>
public static TelemetrySpan PutHttpHostAttribute(this TelemetrySpan span, string hostAndPort)
{
if (!string.IsNullOrEmpty(hostAndPort))
{
span.SetAttribute(SpanAttributeConstants.HttpHostKey, hostAndPort);
}

return span;
}

/// <summary>
/// Helper method that populates span properties from route
/// to https://github.com/open-telemetry/opentelemetry-specification/blob/2316771e7e0ca3bfe9b2286d13e3a41ded6b8858/specification/data-http.md.
Expand Down Expand Up @@ -171,46 +188,7 @@ public static TelemetrySpan PutHttpStatusCode(this TelemetrySpan span, int statu
{
span.PutHttpStatusCodeAttribute(statusCode);

var newStatus = Status.Unknown;

if (statusCode >= 200 && statusCode <= 399)
{
newStatus = Status.Ok;
}
else if (statusCode == 400)
{
newStatus = Status.InvalidArgument;
}
else if (statusCode == 401)
{
newStatus = Status.Unauthenticated;
}
else if (statusCode == 403)
{
newStatus = Status.PermissionDenied;
}
else if (statusCode == 404)
{
newStatus = Status.NotFound;
}
else if (statusCode == 429)
{
newStatus = Status.ResourceExhausted;
}
else if (statusCode == 501)
{
newStatus = Status.Unimplemented;
}
else if (statusCode == 503)
{
newStatus = Status.Unavailable;
}
else if (statusCode == 504)
{
newStatus = Status.DeadlineExceeded;
}

span.Status = newStatus.WithDescription(reasonPhrase);
span.Status = SpanHelper.ResolveSpanStatusForHttpStatusCode(statusCode).WithDescription(reasonPhrase);

return span;
}
Expand Down
111 changes: 111 additions & 0 deletions src/OpenTelemetry.Api/Trace/SpanHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// <copyright file="SpanHelper.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.Collections.Generic;

namespace OpenTelemetry.Trace
{
/// <summary>
/// A collection of helper methods to be used when building spans.
/// </summary>
public static class SpanHelper
{
private static readonly Dictionary<StatusCanonicalCode, string> StatusCanonicalCodeToStringCache = new Dictionary<StatusCanonicalCode, string>()
{
[StatusCanonicalCode.Ok] = StatusCanonicalCode.Ok.ToString(),
[StatusCanonicalCode.Cancelled] = StatusCanonicalCode.Cancelled.ToString(),
[StatusCanonicalCode.Unknown] = StatusCanonicalCode.Unknown.ToString(),
[StatusCanonicalCode.InvalidArgument] = StatusCanonicalCode.InvalidArgument.ToString(),
[StatusCanonicalCode.DeadlineExceeded] = StatusCanonicalCode.DeadlineExceeded.ToString(),
[StatusCanonicalCode.NotFound] = StatusCanonicalCode.NotFound.ToString(),
[StatusCanonicalCode.AlreadyExists] = StatusCanonicalCode.AlreadyExists.ToString(),
[StatusCanonicalCode.PermissionDenied] = StatusCanonicalCode.PermissionDenied.ToString(),
[StatusCanonicalCode.ResourceExhausted] = StatusCanonicalCode.ResourceExhausted.ToString(),
[StatusCanonicalCode.FailedPrecondition] = StatusCanonicalCode.FailedPrecondition.ToString(),
[StatusCanonicalCode.Aborted] = StatusCanonicalCode.Aborted.ToString(),
[StatusCanonicalCode.OutOfRange] = StatusCanonicalCode.OutOfRange.ToString(),
[StatusCanonicalCode.Unimplemented] = StatusCanonicalCode.Unimplemented.ToString(),
[StatusCanonicalCode.Internal] = StatusCanonicalCode.Internal.ToString(),
[StatusCanonicalCode.Unavailable] = StatusCanonicalCode.Unavailable.ToString(),
[StatusCanonicalCode.DataLoss] = StatusCanonicalCode.DataLoss.ToString(),
[StatusCanonicalCode.Unauthenticated] = StatusCanonicalCode.Unauthenticated.ToString(),
};

/// <summary>
/// Helper method that returns the string version of a <see cref="StatusCanonicalCode"/> using a cache to save on allocations.
/// </summary>
/// <param name="statusCanonicalCode"><see cref="StatusCanonicalCode"/>.</param>
/// <returns>String version of the supplied <see cref="StatusCanonicalCode"/>.</returns>
public static string GetCachedCanonicalCodeString(StatusCanonicalCode statusCanonicalCode)
{
if (!StatusCanonicalCodeToStringCache.TryGetValue(statusCanonicalCode, out string canonicalCode))
{
return statusCanonicalCode.ToString();
}

return canonicalCode;
}

/// <summary>
/// Helper method that populates span properties from http status code according
/// to https://github.com/open-telemetry/opentelemetry-specification/blob/2316771e7e0ca3bfe9b2286d13e3a41ded6b8858/specification/data-http.md.
/// </summary>
/// <param name="httpStatusCode">Http status code.</param>
/// <returns>Resolved span <see cref="Status"/> for the Http status code.</returns>
public static Status ResolveSpanStatusForHttpStatusCode(int httpStatusCode)
{
var newStatus = Status.Unknown;

if (httpStatusCode >= 200 && httpStatusCode <= 399)
{
newStatus = Status.Ok;
}
else if (httpStatusCode == 400)
{
newStatus = Status.InvalidArgument;
}
else if (httpStatusCode == 401)
{
newStatus = Status.Unauthenticated;
}
else if (httpStatusCode == 403)
{
newStatus = Status.PermissionDenied;
}
else if (httpStatusCode == 404)
{
newStatus = Status.NotFound;
}
else if (httpStatusCode == 429)
{
newStatus = Status.ResourceExhausted;
}
else if (httpStatusCode == 501)
{
newStatus = Status.Unimplemented;
}
else if (httpStatusCode == 503)
{
newStatus = Status.Unavailable;
}
else if (httpStatusCode == 504)
{
newStatus = Status.DeadlineExceeded;
}

return newStatus;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
{
internal static class JaegerConversionExtensions
{
private const string StatusCode = "ot.status_code";
private const string StatusDescription = "ot.status_description";

private const int DaysPerYear = 365;

// Number of days in 4 years
Expand Down Expand Up @@ -56,8 +53,6 @@ internal static class JaegerConversionExtensions
["db.instance"] = 4, // peer.service for Redis.
};

private static readonly Dictionary<StatusCanonicalCode, string> CanonicalCodeDictionary = new Dictionary<StatusCanonicalCode, string>();

private static readonly DictionaryEnumerator<string, object, TagState>.ForEachDelegate ProcessAttributeRef = ProcessAttribute;
private static readonly DictionaryEnumerator<string, object, TagState>.ForEachDelegate ProcessLibraryAttributeRef = ProcessLibraryAttribute;
private static readonly ListEnumerator<Link, PooledListState<JaegerSpanRef>>.ForEachDelegate ProcessLinkRef = ProcessLink;
Expand Down Expand Up @@ -127,17 +122,11 @@ public static JaegerSpan ToJaegerSpan(this SpanData span)

if (status.IsValid)
{
if (!CanonicalCodeDictionary.TryGetValue(status.CanonicalCode, out string statusCode))
{
statusCode = status.CanonicalCode.ToString();
CanonicalCodeDictionary.Add(status.CanonicalCode, statusCode);
}

PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag(StatusCode, JaegerTagType.STRING, vStr: statusCode));
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag(SpanAttributeConstants.StatusCodeKey, JaegerTagType.STRING, vStr: SpanHelper.GetCachedCanonicalCodeString(status.CanonicalCode)));

if (status.Description != null)
{
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag(StatusDescription, JaegerTagType.STRING, vStr: status.Description));
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag(SpanAttributeConstants.StatusDescriptionKey, JaegerTagType.STRING, vStr: status.Description));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation
{
internal static class ZipkinConversionExtensions
{
private const string StatusCode = "ot.status_code";
private const string StatusDescription = "ot.status_description";

private static readonly Dictionary<string, int> RemoteEndpointServiceNameKeyResolutionDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
[SpanAttributeConstants.PeerServiceKey] = 0, // RemoteEndpoint.ServiceName primary.
Expand All @@ -41,7 +38,6 @@ internal static class ZipkinConversionExtensions

private static readonly ConcurrentDictionary<string, ZipkinEndpoint> LocalEndpointCache = new ConcurrentDictionary<string, ZipkinEndpoint>();
private static readonly ConcurrentDictionary<string, ZipkinEndpoint> RemoteEndpointCache = new ConcurrentDictionary<string, ZipkinEndpoint>();
private static readonly ConcurrentDictionary<StatusCanonicalCode, string> CanonicalCodeCache = new ConcurrentDictionary<StatusCanonicalCode, string>();

private static readonly DictionaryEnumerator<string, object, AttributeEnumerationState>.ForEachDelegate ProcessAttributesRef = ProcessAttributes;
private static readonly DictionaryEnumerator<string, object, AttributeEnumerationState>.ForEachDelegate ProcessLibraryResourcesRef = ProcessLibraryResources;
Expand Down Expand Up @@ -96,17 +92,11 @@ internal static ZipkinSpan ToZipkinSpan(this SpanData otelSpan, ZipkinEndpoint d

if (status.IsValid)
{
if (!CanonicalCodeCache.TryGetValue(status.CanonicalCode, out string canonicalCode))
{
canonicalCode = status.CanonicalCode.ToString();
CanonicalCodeCache.TryAdd(status.CanonicalCode, canonicalCode);
}

PooledList<KeyValuePair<string, string>>.Add(ref attributeEnumerationState.Tags, new KeyValuePair<string, string>(StatusCode, canonicalCode));
PooledList<KeyValuePair<string, string>>.Add(ref attributeEnumerationState.Tags, new KeyValuePair<string, string>(SpanAttributeConstants.StatusCodeKey, SpanHelper.GetCachedCanonicalCodeString(status.CanonicalCode)));

if (status.Description != null)
{
PooledList<KeyValuePair<string, string>>.Add(ref attributeEnumerationState.Tags, new KeyValuePair<string, string>(StatusDescription, status.Description));
PooledList<KeyValuePair<string, string>>.Add(ref attributeEnumerationState.Tags, new KeyValuePair<string, string>(SpanAttributeConstants.StatusDescriptionKey, status.Description));
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/OpenTelemetry.Instrumentation.Dependencies/Constants.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ public DependenciesInstrumentation(TracerFactoryBase tracerFactory, HttpClientIn
var assemblyVersion = typeof(DependenciesInstrumentation).Assembly.GetName().Version;

var httpClientListener = new HttpClientInstrumentation(tracerFactory.GetTracer(nameof(HttpClientInstrumentation), "semver:" + assemblyVersion), httpOptions ?? new HttpClientInstrumentationOptions());
var httpWebRequestInstrumentation = new HttpWebRequestInstrumentation(tracerFactory.GetTracer(nameof(HttpWebRequestInstrumentation), "semver:" + assemblyVersion), httpOptions ?? new HttpClientInstrumentationOptions());
var azureClientsListener = new AzureClientsInstrumentation(tracerFactory.GetTracer(nameof(AzureClientsInstrumentation), "semver:" + assemblyVersion));
var azurePipelineListener = new AzurePipelineInstrumentation(tracerFactory.GetTracer(nameof(AzurePipelineInstrumentation), "semver:" + assemblyVersion));
var sqlClientListener = new SqlClientInstrumentation(tracerFactory.GetTracer(nameof(SqlClientInstrumentation), "semver:" + assemblyVersion), sqlOptions ?? new SqlClientInstrumentationOptions());

this.instrumentations.Add(httpClientListener);
this.instrumentations.Add(httpWebRequestInstrumentation);
this.instrumentations.Add(azureClientsListener);
this.instrumentations.Add(azurePipelineListener);
this.instrumentations.Add(sqlClientListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
// limitations under the License.
// </copyright>
using System;
using System.Net;
using System.Net.Http;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Dependencies.Implementation;

namespace OpenTelemetry.Instrumentation.Dependencies
{
Expand Down Expand Up @@ -101,18 +99,6 @@ private static bool IsHttpOutgoingPostRequest(string activityName, object arg1,
return true;
}
}
#if NET461
else if (activityName == HttpWebRequestDiagnosticSource.ActivityName)
{
if (arg1 is HttpWebRequest request &&
request.RequestUri != null &&
request.Method == "POST")
{
requestUri = request.RequestUri;
return true;
}
}
#endif

requestUri = null;
return false;
Expand Down

0 comments on commit 036a9bb

Please sign in to comment.