Skip to content

Commit

Permalink
[AzureMonitorDistro] Rename public API to AddAzureMonitor (Azure#34672)
Browse files Browse the repository at this point in the history
* Rename public API to AddAzureMonitor

* change comment
  • Loading branch information
rajkumar-rangaraj authored Mar 2, 2023
1 parent 58a969f commit f29b99b
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Azure.Monitor.OpenTelemetry
{
public static partial class AzureMonitorOpenTelemetryExtensions
public static partial class AzureMonitorExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAzureMonitorOpenTelemetry(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAzureMonitorOpenTelemetry(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Azure.Monitor.OpenTelemetry.AzureMonitorOpenTelemetryOptions options) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAzureMonitorOpenTelemetry(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Azure.Monitor.OpenTelemetry.AzureMonitorOpenTelemetryOptions> configureAzureMonitorOpenTelemetry) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAzureMonitor(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAzureMonitor(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Azure.Monitor.OpenTelemetry.AzureMonitorOptions options) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAzureMonitor(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Azure.Monitor.OpenTelemetry.AzureMonitorOptions> configureAzureMonitor) { throw null; }
}
public partial class AzureMonitorOpenTelemetryOptions
public partial class AzureMonitorOptions
{
public AzureMonitorOpenTelemetryOptions() { }
public AzureMonitorOptions() { }
public string ConnectionString { get { throw null; } set { } }
public Azure.Core.TokenCredential Credential { get { throw null; } set { } }
public bool DisableOfflineStorage { get { throw null; } set { } }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Azure.Monitor.OpenTelemetry
{
/// <summary>
/// Extension methods for setting up Azure Monitor in an <see cref="IServiceCollection" />.
/// </summary>
public static class AzureMonitorExtensions
{
/// <summary>
/// Adds Azure Monitor into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddAzureMonitor(this IServiceCollection services)
{
services.TryAddSingleton<IConfigureOptions<AzureMonitorOptions>,
DefaultAzureMonitorOptions>();
return services.AddAzureMonitor(o => o = new AzureMonitorOptions());
}

/// <summary>
/// Adds Azure Monitor into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="options">The <see cref="AzureMonitorOptions" /> instance for configuration.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddAzureMonitor(this IServiceCollection services, AzureMonitorOptions options)
{
options ??= new AzureMonitorOptions();
return services.AddAzureMonitor(o => o.Clone(options));
}

/// <summary>
/// Adds Azure Monitor into service collection.
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/>.</param>
/// <param name="configureAzureMonitor">Callback action for configuring <see cref="AzureMonitorOptions"/>.</param>
/// <returns><see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddAzureMonitor(this IServiceCollection services, Action<AzureMonitorOptions> configureAzureMonitor)
{
return AzureMonitorImplementations.AddAzureMonitorWithAction(services, configureAzureMonitor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

namespace Azure.Monitor.OpenTelemetry
{
internal class AzureMonitorOpenTelemetryImplementations
internal class AzureMonitorImplementations
{
internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServiceCollection services, Action<AzureMonitorOpenTelemetryOptions> configureAzureMonitorOpenTelemetry)
internal static IServiceCollection AddAzureMonitorWithAction(IServiceCollection services, Action<AzureMonitorOptions> configureAzureMonitor)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (configureAzureMonitorOpenTelemetry != null)
if (configureAzureMonitor != null)
{
services.Configure(configureAzureMonitorOpenTelemetry);
services.Configure(configureAzureMonitor);
}

var builder = services.AddOpenTelemetry();
Expand All @@ -42,10 +42,10 @@ internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServi

services.AddLogging(logging =>
{
AzureMonitorOpenTelemetryOptions logExporterOptions = new();
if (configureAzureMonitorOpenTelemetry != null)
AzureMonitorOptions logExporterOptions = new();
if (configureAzureMonitor != null)
{
configureAzureMonitorOpenTelemetry(logExporterOptions);
configureAzureMonitor(logExporterOptions);
}
if (logExporterOptions.EnableLogs)
Expand Down Expand Up @@ -95,7 +95,7 @@ internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServi

services.AddSingleton(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOpenTelemetryOptions>>().Get("");
var options = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOptions>>().Get("");
if (!options.EnableTraces)
{
return new NoopTracerProvider();
Expand All @@ -111,7 +111,7 @@ internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServi

services.AddSingleton(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOpenTelemetryOptions>>().Get("");
var options = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOptions>>().Get("");
if (!options.EnableMetrics)
{
return new NoopMeterProvider();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace Azure.Monitor.OpenTelemetry
{
/// <summary>
/// Options that allow users to configure the Azure Monitor OpenTelemetry.
/// Options that allow users to configure the Azure Monitor.
/// </summary>
public class AzureMonitorOpenTelemetryOptions
public class AzureMonitorOptions
{
/// <summary>
/// The Connection String provides users with a single configuration setting to identify the Azure Monitor resource and endpoint.
Expand Down Expand Up @@ -59,7 +59,7 @@ public class AzureMonitorOpenTelemetryOptions
/// </summary>
public string StorageDirectory { get; set; }

internal AzureMonitorOpenTelemetryOptions Clone(AzureMonitorOpenTelemetryOptions options)
internal AzureMonitorOptions Clone(AzureMonitorOptions options)
{
if (options != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

namespace Azure.Monitor.OpenTelemetry
{
internal class DefaultAzureMonitorOpenTelemetryOptions : IConfigureOptions<AzureMonitorOpenTelemetryOptions>
internal class DefaultAzureMonitorOptions : IConfigureOptions<AzureMonitorOptions>
{
private const string AzureMonitorOpenTelemetrySectionFromConfig = "AzureMonitorOpenTelemetry";
private const string AzureMonitorSectionFromConfig = "AzureMonitor";
private const string ConnectionStringEnvironmentVariable = "APPLICATIONINSIGHTS_CONNECTION_STRING";
private readonly IConfiguration? _configuration;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultAzureMonitorOpenTelemetryOptions"/> class.
/// Initializes a new instance of the <see cref="DefaultAzureMonitorOptions"/> class.
/// </summary>
/// <param name="configuration"><see cref="IConfiguration"/> from which configuration for ApplicationInsights can be retrieved.</param>
public DefaultAzureMonitorOpenTelemetryOptions(IConfiguration? configuration = null)
/// <param name="configuration"><see cref="IConfiguration"/> from which configuration for Azure Monitor can be retrieved.</param>
public DefaultAzureMonitorOptions(IConfiguration? configuration = null)
{
_configuration = configuration;
}

public void Configure(AzureMonitorOpenTelemetryOptions options)
public void Configure(AzureMonitorOptions options)
{
if (_configuration != null)
{
_configuration.GetSection(AzureMonitorOpenTelemetrySectionFromConfig).Bind(options);
_configuration.GetSection(AzureMonitorSectionFromConfig).Bind(options);
}

string connectionString = Environment.GetEnvironmentVariable(ConnectionStringEnvironmentVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAzureMonitorOpenTelemetry();
builder.Services.AddAzureMonitor();
/*
builder.Services.AddAzureMonitorOpenTelemetry(o =>
builder.Services.AddAzureMonitor(o =>
{
o.ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000";
// Set the Credential property to enable AAD based authentication:
Expand All @@ -24,7 +24,7 @@
// To customize sampling, Set ApplicationInsightsSampler to desired sampling ratio and
// configure with OpenTelemetryTracerProvider.
// Please note that ConfigureOpenTelemetryTracerProvider should be called after
// builder.Services.AddAzureMonitorOpenTelemetry().
// builder.Services.AddAzureMonitor().
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.SetSampler(new ApplicationInsightsSampler(0.9F)));

var app = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"AzureMonitorOpenTelemetry": {
"AzureMonitor": {
"ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-000000000123"
}
}

0 comments on commit f29b99b

Please sign in to comment.