Skip to content

Commit

Permalink
[AzureMonitorExporter] api feedback (#37996)
Browse files Browse the repository at this point in the history
* merge Extensions classes

* update Options class

* changelog

* update public api

* remove nullables from public api

* changelog

* changelog

* export public api

* reset enum integer
  • Loading branch information
TimothyMothra authored Aug 2, 2023
1 parent 7b97ef7 commit 7e36acc
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AzureMonitorOptions : ClientOptions
/// The Connection String provides users with a single configuration setting to identify the Azure Monitor resource and endpoint.
/// </summary>
/// <remarks>
/// (https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string).
/// <see href="https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string"/>.
/// </remarks>
public string ConnectionString { get; set; }

Expand All @@ -27,7 +27,7 @@ public class AzureMonitorOptions : ClientOptions
/// and Instrumentation Key from the Connection String will be used.
/// </summary>
/// <remarks>
/// https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=net#is-the-connection-string-a-secret
/// <see href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=net#is-the-connection-string-a-secret"/>.
/// </remarks>
public TokenCredential Credential { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
* Location ip on server spans will now be set using `client.address` tag key on
activity instead of `http.client_ip`.
([#37707](https://github.com/Azure/azure-sdk-for-net/pull/37707))
* Removing `ServiceVersion.V2020_09_15_Preview`. This is no longer in use and
the exporter has already defaulted to the latest `ServiceVersion.v2_1`.
([#37996](https://github.com/Azure/azure-sdk-for-net/pull/37996))
* Remove Nullable Annotations from the Exporter's public API.
([#37996](https://github.com/Azure/azure-sdk-for-net/pull/37996))

### Bugs Fixed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
namespace Azure.Monitor.OpenTelemetry.Exporter
{
public static partial class AzureMonitorExporterLoggingExtensions
public static partial class AzureMonitorExporterExtensions
{
public static OpenTelemetry.Logs.OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions>? configure = null, Azure.Core.TokenCredential? credential = null) { throw null; }
}
public static partial class AzureMonitorExporterMetricExtensions
{
public static OpenTelemetry.Metrics.MeterProviderBuilder AddAzureMonitorMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions>? configure = null, Azure.Core.TokenCredential? credential = null, string? name = null) { throw null; }
public static OpenTelemetry.Logs.OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions> configure = null, Azure.Core.TokenCredential credential = null) { throw null; }
public static OpenTelemetry.Metrics.MeterProviderBuilder AddAzureMonitorMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions> configure = null, Azure.Core.TokenCredential credential = null, string name = null) { throw null; }
public static OpenTelemetry.Trace.TracerProviderBuilder AddAzureMonitorTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions> configure = null, Azure.Core.TokenCredential credential = null, string name = null) { throw null; }
}
public partial class AzureMonitorExporterOptions : Azure.Core.ClientOptions
{
public AzureMonitorExporterOptions() { }
public AzureMonitorExporterOptions(Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion version = Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion.v2_1) { }
public string? ConnectionString { get { throw null; } set { } }
public Azure.Core.TokenCredential? Credential { get { throw null; } set { } }
public string ConnectionString { get { throw null; } set { } }
public Azure.Core.TokenCredential Credential { get { throw null; } set { } }
public bool DisableOfflineStorage { get { throw null; } set { } }
public float SamplingRatio { get { throw null; } set { } }
public string? StorageDirectory { get { throw null; } set { } }
public string StorageDirectory { get { throw null; } set { } }
public Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion Version { get { throw null; } set { } }
public enum ServiceVersion
{
V2020_09_15_Preview = 1,
v2_1 = 2,
v2_1 = 1,
}
}
public static partial class AzureMonitorExporterTraceExtensions
{
public static OpenTelemetry.Trace.TracerProviderBuilder AddAzureMonitorTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions>? configure = null, Azure.Core.TokenCredential? credential = null, string? name = null) { throw null; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System;
using System.Diagnostics;
using Azure.Core;
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Azure.Monitor.OpenTelemetry.Exporter
{
/// <summary>
/// Extension methods to simplify registering of Azure Monitor Exporter for all signals.
/// </summary>
public static class AzureMonitorExporterExtensions
{
/// <summary>
/// Adds Azure Monitor Trace exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <param name="configure">Callback action for configuring <see cref="AzureMonitorExporterOptions"/>.</param>
/// <param name="credential">
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
/// the Options will take precedence.
/// </param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddAzureMonitorTraceExporter(
this TracerProviderBuilder builder,
Action<AzureMonitorExporterOptions> configure = null,
TokenCredential credential = null,
string name = null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

var finalOptionsName = name ?? Options.DefaultName;

if (name != null && configure != null)
{
// If we are using named options we register the
// configuration delegate into options pipeline.
builder.ConfigureServices(services => services.Configure(finalOptionsName, configure));
}

var deferredBuilder = builder as IDeferredTracerProviderBuilder;
if (deferredBuilder == null)
{
throw new InvalidOperationException("The provided TracerProviderBuilder does not implement IDeferredTracerProviderBuilder.");
}

return deferredBuilder.Configure((sp, builder) =>
{
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName);
if (name == null && configure != null)
{
// If we are NOT using named options, we execute the
// configuration delegate inline. The reason for this is
// AzureMonitorExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together. See:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
configure(exporterOptions);
}

builder.SetSampler(new ApplicationInsightsSampler(exporterOptions.SamplingRatio));

if (credential != null)
{
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
// Options should take precedence.
exporterOptions.Credential ??= credential;
}

builder.AddProcessor(new CompositeProcessor<Activity>(new BaseProcessor<Activity>[]
{
new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(exporterOptions)),
new BatchActivityExportProcessor(new AzureMonitorTraceExporter(exporterOptions))
}));
});
}

/// <summary>
/// Adds Azure Monitor Metric exporter.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
/// <param name="configure">Exporter configuration options.</param>
/// <param name="credential">
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
/// the Options will take precedence.
/// </param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddAzureMonitorMetricExporter(
this MeterProviderBuilder builder,
Action<AzureMonitorExporterOptions> configure = null,
TokenCredential credential = null,
string name = null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

var finalOptionsName = name ?? Options.DefaultName;

if (name != null && configure != null)
{
// If we are using named options we register the
// configuration delegate into options pipeline.
builder.ConfigureServices(services => services.Configure(finalOptionsName, configure));
}

return builder.AddReader(sp =>
{
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName);

if (name == null && configure != null)
{
// If we are NOT using named options, we execute the
// configuration delegate inline. The reason for this is
// AzureMonitorExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together. See:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
configure(exporterOptions);
}

if (credential != null)
{
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
// Options should take precedence.
exporterOptions.Credential ??= credential;
}

return new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(exporterOptions))
{ TemporalityPreference = MetricReaderTemporalityPreference.Delta };
});
}

/// <summary>
/// Adds Azure Monitor Log Exporter with OpenTelemetryLoggerOptions.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="configure">Exporter configuration options.</param>
/// <param name="credential">
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
/// the Options will take precedence.
/// </param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(
this OpenTelemetryLoggerOptions loggerOptions,
Action<AzureMonitorExporterOptions> configure = null,
TokenCredential credential = null)
{
if (loggerOptions == null)
{
throw new ArgumentNullException(nameof(loggerOptions));
}

var options = new AzureMonitorExporterOptions();
configure?.Invoke(options);

if (credential != null)
{
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
// Options should take precedence.
options.Credential ??= credential;
}

return loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options)));
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7e36acc

Please sign in to comment.