forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from cijothomas/cijothomas/migrateazureinstr
Lift and shift Azure instrumentation from main repo
- Loading branch information
Showing
9 changed files
with
423 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/OpenTelemetry.Exporter.Stackdriver/OpenTelemetry.Exporter.Stackdriver.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/OpenTelemetry.Instrumentation.Azure/AzureClientsInstrumentation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// <copyright file="AzureClientsInstrumentation.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 OpenTelemetry.Instrumentation.Azure.Implementation; | ||
using System; | ||
|
||
namespace OpenTelemetry.Instrumentation.Azure | ||
{ | ||
/// <summary> | ||
/// AzureClients instrumentation. | ||
/// TODO: Azure specific listeners would be moved out of this repo. | ||
/// I believe this was initially put here for quick validation. | ||
/// There were no unit tests covering this feature, so | ||
/// cannot validate after Span is replaced with Activity. | ||
/// </summary> | ||
public class AzureClientsInstrumentation : IDisposable | ||
{ | ||
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzureClientsInstrumentation"/> class. | ||
/// </summary> | ||
public AzureClientsInstrumentation() | ||
{ | ||
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber( | ||
name => new AzureSdkDiagnosticListener(name), | ||
listener => listener.Name.StartsWith("Azure."), | ||
null); | ||
this.diagnosticSourceSubscriber.Subscribe(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
this.diagnosticSourceSubscriber?.Dispose(); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/OpenTelemetry.Instrumentation.Azure/AzurePipelineInstrumentation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// <copyright file="AzurePipelineInstrumentation.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 OpenTelemetry.Instrumentation.Azure.Implementation; | ||
using System; | ||
|
||
namespace OpenTelemetry.Instrumentation.Azure | ||
{ | ||
/// <summary> | ||
/// AzurePipeline instrumentation. | ||
/// TODO: Azure specific listeners would be moved out of this repo. | ||
/// I believe this was initially put here for quick validation. | ||
/// There were no unit tests covering this feature, so | ||
/// cannot validate after Span is replaced with Activity. | ||
/// </summary> | ||
public class AzurePipelineInstrumentation : IDisposable | ||
{ | ||
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzurePipelineInstrumentation"/> class. | ||
/// </summary> | ||
public AzurePipelineInstrumentation() | ||
{ | ||
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new AzureSdkDiagnosticListener("Azure.Pipeline"), null); | ||
this.diagnosticSourceSubscriber.Subscribe(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
this.diagnosticSourceSubscriber?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace OpenTelemetry.Instrumentation.Azure | ||
{ | ||
public class Class1 | ||
{ | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
src/OpenTelemetry.Instrumentation.Azure/Implementation/AzureSdkDiagnosticListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// <copyright file="AzureSdkDiagnosticListener.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 OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Instrumentation.Azure.Implementation | ||
{ | ||
internal class AzureSdkDiagnosticListener : ListenerHandler | ||
{ | ||
internal const string ActivitySourceName = "AzureSDK"; | ||
internal const string ActivityName = ActivitySourceName + ".HttpRequestOut"; | ||
private static readonly Version Version = typeof(AzureSdkDiagnosticListener).Assembly.GetName().Version; | ||
private static readonly ActivitySource AzureSDKActivitySource = new ActivitySource(ActivitySourceName, Version.ToString()); | ||
|
||
// all fetchers must not be reused between DiagnosticSources. | ||
private readonly PropertyFetcher linksPropertyFetcher = new PropertyFetcher("Links"); | ||
|
||
public AzureSdkDiagnosticListener(string sourceName) | ||
: base(sourceName, null) | ||
{ | ||
} | ||
|
||
public void OnCompleted() | ||
{ | ||
} | ||
|
||
public void OnError(Exception error) | ||
{ | ||
} | ||
|
||
public override void OnStartActivity(Activity activity, object valueValue) | ||
{ | ||
string operationName = null; | ||
var activityKind = ActivityKind.Internal; | ||
|
||
foreach (var keyValuePair in activity.Tags) | ||
{ | ||
if (keyValuePair.Key == "http.url") | ||
{ | ||
operationName = keyValuePair.Value; | ||
activityKind = ActivityKind.Client; | ||
break; | ||
} | ||
|
||
if (keyValuePair.Key == "kind") | ||
{ | ||
if (Enum.TryParse(keyValuePair.Value, true, out ActivityKind parsedActivityKind)) | ||
{ | ||
activityKind = parsedActivityKind; | ||
} | ||
} | ||
} | ||
|
||
if (operationName == null) | ||
{ | ||
operationName = this.GetOperationName(activity); | ||
} | ||
|
||
List<ActivityLink> links = null; | ||
if (this.linksPropertyFetcher.Fetch(valueValue) is IEnumerable<Activity> activityLinks) | ||
{ | ||
if (activityLinks.Any()) | ||
{ | ||
links = new List<ActivityLink>(); | ||
foreach (var link in activityLinks) | ||
{ | ||
if (link != null) | ||
{ | ||
links.Add(new ActivityLink(new ActivityContext(link.TraceId, link.ParentSpanId, link.ActivityTraceFlags))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Ignore the activity and create a new one using ActivitySource. | ||
// The new one will have Sampling decision made using extracted Links as well. | ||
AzureSDKActivitySource.StartActivity(operationName, activityKind, activity.Id, activity.Tags, links); | ||
} | ||
|
||
public override void OnStopActivity(Activity current, object valueValue) | ||
{ | ||
// nothing to be done. | ||
} | ||
|
||
public override void OnException(Activity activity, object valueValue) | ||
{ | ||
Status status = Status.Unknown; | ||
activity.AddTag(SpanAttributeConstants.StatusCodeKey, SpanHelper.GetCachedCanonicalCodeString(status.CanonicalCode)); | ||
activity.AddTag(SpanAttributeConstants.StatusDescriptionKey, valueValue?.ToString()); | ||
} | ||
|
||
private string GetOperationName(Activity activity) | ||
{ | ||
// activity name looks like 'Azure.<...>.<Class>.<Name>' | ||
// as namespace is too verbose, we'll just take the last two nodes from the activity name as telemetry name | ||
// this will change with https://github.com/Azure/azure-sdk-for-net/issues/9071 ~Feb 2020 | ||
|
||
string activityName = activity.OperationName; | ||
int methodDotIndex = activityName.LastIndexOf('.'); | ||
if (methodDotIndex <= 0) | ||
{ | ||
return activityName; | ||
} | ||
|
||
int classDotIndex = activityName.LastIndexOf('.', methodDotIndex - 1); | ||
|
||
if (classDotIndex == -1) | ||
{ | ||
return activityName; | ||
} | ||
|
||
return activityName.Substring(classDotIndex + 1, activityName.Length - classDotIndex - 1); | ||
} | ||
} | ||
} |
Oops, something went wrong.