forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vendor OpenTelemetry.NET Instrumentation Libraries into Azure.Monitor…
….OpenTelemetry.AspNetCore (Azure#38848) * Vendoring * Update copyrights * Update Notice file
- Loading branch information
1 parent
1bd0293
commit 441e303
Showing
64 changed files
with
7,310 additions
and
4 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
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: 2 additions & 0 deletions
2
sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/DefaultAzureMonitorOptions.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
55 changes: 55 additions & 0 deletions
55
...tCore/src/Vendoring/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentation.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,55 @@ | ||
// <copyright file="AspNetCoreInstrumentation.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 OpenTelemetry.Instrumentation.AspNetCore.Implementation; | ||
|
||
namespace OpenTelemetry.Instrumentation.AspNetCore | ||
{ | ||
/// <summary> | ||
/// Asp.Net Core Requests instrumentation. | ||
/// </summary> | ||
internal sealed class AspNetCoreInstrumentation : IDisposable | ||
{ | ||
private static readonly HashSet<string> DiagnosticSourceEvents = new() | ||
{ | ||
"Microsoft.AspNetCore.Hosting.HttpRequestIn", | ||
"Microsoft.AspNetCore.Hosting.HttpRequestIn.Start", | ||
"Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop", | ||
"Microsoft.AspNetCore.Mvc.BeforeAction", | ||
"Microsoft.AspNetCore.Diagnostics.UnhandledException", | ||
"Microsoft.AspNetCore.Hosting.UnhandledException", | ||
}; | ||
|
||
private readonly Func<string, object, object, bool> isEnabled = (eventName, _, _) | ||
=> DiagnosticSourceEvents.Contains(eventName); | ||
|
||
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; | ||
|
||
public AspNetCoreInstrumentation(HttpInListener httpInListener) | ||
{ | ||
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(httpInListener, this.isEnabled); | ||
this.diagnosticSourceSubscriber.Subscribe(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
this.diagnosticSourceSubscriber?.Dispose(); | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...rc/Vendoring/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.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,110 @@ | ||
// <copyright file="AspNetCoreInstrumentationOptions.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; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using static OpenTelemetry.Internal.HttpSemanticConventionHelper; | ||
|
||
namespace OpenTelemetry.Instrumentation.AspNetCore | ||
{ | ||
/// <summary> | ||
/// Options for requests instrumentation. | ||
/// </summary> | ||
internal class AspNetCoreInstrumentationOptions | ||
{ | ||
internal readonly HttpSemanticConvention HttpSemanticConvention; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AspNetCoreInstrumentationOptions"/> class. | ||
/// </summary> | ||
public AspNetCoreInstrumentationOptions() | ||
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) | ||
{ | ||
} | ||
|
||
internal AspNetCoreInstrumentationOptions(IConfiguration configuration) | ||
{ | ||
Debug.Assert(configuration != null, "configuration was null"); | ||
|
||
this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets a filter function that determines whether or not to | ||
/// collect telemetry on a per request basis. | ||
/// </summary> | ||
/// <remarks> | ||
/// Notes: | ||
/// <list type="bullet"> | ||
/// <item>The return value for the filter function is interpreted as: | ||
/// <list type="bullet"> | ||
/// <item>If filter returns <see langword="true" />, the request is | ||
/// collected.</item> | ||
/// <item>If filter returns <see langword="false" /> or throws an | ||
/// exception the request is NOT collected.</item> | ||
/// </list></item> | ||
/// </list> | ||
/// </remarks> | ||
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><see cref="HttpRequest"/>: the HttpRequest object from which additional information can be extracted to enrich the activity.</para> | ||
/// </remarks> | ||
public Action<Activity, HttpRequest> EnrichWithHttpRequest { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets an action to enrich an Activity. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para><see cref="Activity"/>: the activity being enriched.</para> | ||
/// <para><see cref="HttpResponse"/>: the HttpResponse object from which additional information can be extracted to enrich the activity.</para> | ||
/// </remarks> | ||
public Action<Activity, HttpResponse> EnrichWithHttpResponse { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets an action to enrich an Activity. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para><see cref="Activity"/>: the activity being enriched.</para> | ||
/// <para><see cref="Exception"/>: the Exception object from which additional information can be extracted to enrich the activity.</para> | ||
/// </remarks> | ||
public Action<Activity, Exception> EnrichWithException { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether the exception will be recorded as ActivityEvent or not. | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/exceptions.md. | ||
/// </remarks> | ||
public bool RecordException { get; set; } | ||
|
||
#if NETSTANDARD2_1 || NET6_0_OR_GREATER | ||
/// <summary> | ||
/// Gets or sets a value indicating whether RPC attributes are added to an Activity when using Grpc.AspNetCore. Default is true. | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/rpc.md. | ||
/// </remarks> | ||
public bool EnableGrpcAspNetCoreSupport { get; set; } = true; | ||
#endif | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ry.AspNetCore/src/Vendoring/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.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,64 @@ | ||
// <copyright file="AspNetCoreMetrics.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.Metrics; | ||
using System.Reflection; | ||
using OpenTelemetry.Instrumentation.AspNetCore.Implementation; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Instrumentation.AspNetCore | ||
{ | ||
/// <summary> | ||
/// Asp.Net Core Requests instrumentation. | ||
/// </summary> | ||
internal sealed class AspNetCoreMetrics : IDisposable | ||
{ | ||
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName(); | ||
internal static readonly string InstrumentationName = AssemblyName.Name; | ||
internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString(); | ||
|
||
private static readonly HashSet<string> DiagnosticSourceEvents = new() | ||
{ | ||
"Microsoft.AspNetCore.Hosting.HttpRequestIn", | ||
"Microsoft.AspNetCore.Hosting.HttpRequestIn.Start", | ||
"Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop", | ||
}; | ||
|
||
private readonly Func<string, object, object, bool> isEnabled = (eventName, _, _) | ||
=> DiagnosticSourceEvents.Contains(eventName); | ||
|
||
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; | ||
private readonly Meter meter; | ||
|
||
internal AspNetCoreMetrics(AspNetCoreMetricsInstrumentationOptions options) | ||
{ | ||
Guard.ThrowIfNull(options); | ||
this.meter = new Meter(InstrumentationName, InstrumentationVersion); | ||
var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore", this.meter, options); | ||
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(metricsListener, this.isEnabled); | ||
this.diagnosticSourceSubscriber.Subscribe(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
this.diagnosticSourceSubscriber?.Dispose(); | ||
this.meter?.Dispose(); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...oring/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetricsInstrumentationOptions.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,80 @@ | ||
// <copyright file="AspNetCoreMetricsInstrumentationOptions.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; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using static OpenTelemetry.Internal.HttpSemanticConventionHelper; | ||
|
||
namespace OpenTelemetry.Instrumentation.AspNetCore | ||
{ | ||
/// <summary> | ||
/// Options for metrics requests instrumentation. | ||
/// </summary> | ||
internal class AspNetCoreMetricsInstrumentationOptions | ||
{ | ||
internal readonly HttpSemanticConvention HttpSemanticConvention; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AspNetCoreMetricsInstrumentationOptions"/> class. | ||
/// </summary> | ||
public AspNetCoreMetricsInstrumentationOptions() | ||
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) | ||
{ | ||
} | ||
|
||
internal AspNetCoreMetricsInstrumentationOptions(IConfiguration configuration) | ||
{ | ||
Debug.Assert(configuration != null, "configuration was null"); | ||
|
||
this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Delegate for enrichment of recorded metric with additional tags. | ||
/// </summary> | ||
/// <param name="name">The name of the metric being enriched.</param> | ||
/// <param name="context"><see cref="HttpContext"/>: the HttpContext object. Both Request and Response are available.</param> | ||
/// <param name="tags"><see cref="TagList"/>: List of current tags. You can add additional tags to this list. </param> | ||
public delegate void AspNetCoreMetricEnrichmentFunc(string name, HttpContext context, ref TagList tags); | ||
|
||
/// <summary> | ||
/// Gets or sets a filter function that determines whether or not to | ||
/// collect telemetry on a per request basis. | ||
/// </summary> | ||
/// <remarks> | ||
/// Notes: | ||
/// <list type="bullet"> | ||
/// <item>The first parameter is the name of the metric being | ||
/// filtered.</item> | ||
/// <item>The return value for the filter function is interpreted as: | ||
/// <list type="bullet"> | ||
/// <item>If filter returns <see langword="true" />, the request is | ||
/// collected.</item> | ||
/// <item>If filter returns <see langword="false" /> or throws an | ||
/// exception the request is NOT collected.</item> | ||
/// </list></item> | ||
/// </list> | ||
/// </remarks> | ||
public Func<string, HttpContext, bool> Filter { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets an function to enrich a recorded metric with additional custom tags. | ||
/// </summary> | ||
public AspNetCoreMetricEnrichmentFunc Enrich { get; set; } | ||
} | ||
} |
Oops, something went wrong.