-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AzureMonitorExporter] transmitter factory (#34354)
* transmitter factory * cleanup
- Loading branch information
1 parent
5a0d69b
commit 1875198
Showing
5 changed files
with
98 additions
and
3 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
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
42 changes: 42 additions & 0 deletions
42
sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitterFactory.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,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using Azure.Core; | ||
|
||
namespace Azure.Monitor.OpenTelemetry.Exporter.Internals | ||
{ | ||
/// <summary> | ||
/// This Factory encapsulates the <see cref="AzureMonitorTransmitter"/>. | ||
/// An ideal users will create a single exporter for each signal (Logs, Metrics, Traces). | ||
/// This factory should ensure that only one instance of the Transmitter is created for | ||
/// any unique connection string. | ||
/// </summary> | ||
internal class TransmitterFactory | ||
{ | ||
public static TransmitterFactory Instance = new(); | ||
|
||
internal readonly Dictionary<string, AzureMonitorTransmitter> _transmitters = new(); | ||
private readonly object _lockObj = new(); | ||
|
||
public AzureMonitorTransmitter Get(AzureMonitorExporterOptions azureMonitorExporterOptions, TokenCredential? tokenCredential = null) | ||
{ | ||
var key = azureMonitorExporterOptions.ConnectionString ?? string.Empty; | ||
|
||
if (!_transmitters.TryGetValue(key, out AzureMonitorTransmitter transmitter)) | ||
{ | ||
lock (_lockObj) | ||
{ | ||
if (!_transmitters.TryGetValue(key, out transmitter)) | ||
{ | ||
transmitter = new AzureMonitorTransmitter(azureMonitorExporterOptions, tokenCredential); | ||
|
||
_transmitters.Add(key, transmitter); | ||
} | ||
} | ||
} | ||
|
||
return transmitter; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...etry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TransmitterFactoryTests.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,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Monitor.OpenTelemetry.Exporter.Internals; | ||
using Xunit; | ||
|
||
namespace Azure.Monitor.OpenTelemetry.Exporter.Tests | ||
{ | ||
public class TransmitterFactoryTests | ||
{ | ||
/// <summary> | ||
/// Users may not specify their connection string in the <see cref="AzureMonitorExporterOptions"/>. | ||
/// In this scenario, the sdk would use the environment variable. | ||
/// | ||
/// This test confirms that the factory correctly handles a null value. | ||
/// </summary> | ||
[Fact] | ||
public void VerifyNullConnectionString() | ||
{ | ||
Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=00000000-0000-0000-0000-000000000000;"); | ||
|
||
try | ||
{ | ||
var factory = new TransmitterFactory(); | ||
var options = new AzureMonitorExporterOptions(); | ||
|
||
var transmitter = factory.Get(options); | ||
|
||
Assert.Single(factory._transmitters); | ||
Assert.True(factory._transmitters.ContainsKey(string.Empty)); | ||
} | ||
finally | ||
{ | ||
Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING", null); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void VerifyRepeatedCallsGenerateOnlyOneTransmitter() | ||
{ | ||
var factory = new TransmitterFactory(); | ||
var options = new AzureMonitorExporterOptions { ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;" }; | ||
|
||
var transmitter1 = factory.Get(options); | ||
var transmitter2 = factory.Get(options); | ||
|
||
Assert.Single(factory._transmitters); | ||
Assert.True(factory._transmitters.ContainsKey(options.ConnectionString)); | ||
Assert.Equal(transmitter1, transmitter2); | ||
} | ||
} | ||
} |