-
Notifications
You must be signed in to change notification settings - Fork 295
Exporter options for base module #3065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
350 changes: 350 additions & 0 deletions
350
...Test/Microsoft.ApplicationInsights.Tests/TelemetryConfigurationSetExporterOptionsTests.cs
This file contains hidden or 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,350 @@ | ||
| namespace Microsoft.ApplicationInsights.Tests | ||
| { | ||
| using System; | ||
| using System.Reflection; | ||
| using Azure.Core; | ||
| using Azure.Monitor.OpenTelemetry.Exporter; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using OpenTelemetry; | ||
| using Xunit; | ||
|
|
||
| /// <summary> | ||
| /// Tests for Azure Monitor Exporter configuration methods in TelemetryConfiguration. | ||
| /// </summary> | ||
| public class TelemetryConfigurationSetExporterOptionsTests : IDisposable | ||
| { | ||
| private TelemetryConfiguration telemetryConfiguration; | ||
|
|
||
| public TelemetryConfigurationSetExporterOptionsTests() | ||
| { | ||
| this.telemetryConfiguration = new TelemetryConfiguration(); | ||
harsimar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.telemetryConfiguration?.Dispose(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Helper method to get AzureMonitorExporterOptions from the built OpenTelemetrySdk. | ||
| /// </summary> | ||
| private AzureMonitorExporterOptions GetExporterOptions(OpenTelemetrySdk sdk) | ||
| { | ||
| // Use reflection to access the internal Services property | ||
| var servicesProperty = typeof(OpenTelemetrySdk).GetProperty( | ||
| "Services", | ||
| BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| var serviceProvider = servicesProperty?.GetValue(sdk) as IServiceProvider; | ||
| Assert.NotNull(serviceProvider); | ||
|
|
||
| var options = serviceProvider.GetService<IOptions<AzureMonitorExporterOptions>>(); | ||
| Assert.NotNull(options); | ||
|
|
||
| return options.Value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Helper method to build the configuration by creating a TelemetryClient. | ||
| /// Creating a TelemetryClient triggers the Build() method internally. | ||
| /// </summary> | ||
| private OpenTelemetrySdk BuildConfiguration() | ||
| { | ||
| // Creating a TelemetryClient triggers configuration.Build() internally | ||
| var client = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Access the private 'sdk' field from TelemetryClient via reflection | ||
| var sdkField = typeof(TelemetryClient).GetField( | ||
| "sdk", | ||
| BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| return sdkField?.GetValue(client) as OpenTelemetrySdk; | ||
| } | ||
|
|
||
| #region SetSamplingRatio Tests | ||
|
|
||
| [Fact] | ||
| public void SetSamplingRatio_WithValidRatio_SetsSamplingRatioInExporterOptions() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.SetSamplingRatio(0.5F); | ||
harsimar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify the actual value | ||
| Assert.Equal(0.5F, exporterOptions.SamplingRatio); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetSamplingRatio_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| _ = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.SetSamplingRatio(0.5F)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetSamplingRatio_WithRatioGreaterThanOne_ThrowsArgumentOutOfRangeException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<ArgumentOutOfRangeException>(() => | ||
| this.telemetryConfiguration.SetSamplingRatio(1.5F)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetSamplingRatio_WithNegativeRatio_ThrowsArgumentOutOfRangeException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<ArgumentOutOfRangeException>(() => | ||
| this.telemetryConfiguration.SetSamplingRatio(-0.5F)); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region SetTracesPerSecond Tests | ||
|
|
||
| [Fact] | ||
| public void SetTracesPerSecond_WithValidValue_SetsTracesPerSecondInExporterOptions() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.SetTracesPerSecond(1.5); | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify the actual value | ||
| Assert.Equal(1.5, exporterOptions.TracesPerSecond); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTracesPerSecond_WithNegativeValue_ThrowsArgumentOutOfRangeException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<ArgumentOutOfRangeException>(() => | ||
| this.telemetryConfiguration.SetTracesPerSecond(-1.0)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTracesPerSecond_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| _ = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.SetTracesPerSecond(1.5)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTracesPerSecond_WithValidValue_AndDisableLiveMetrics_BeforeBuild() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act - chain multiple configuration calls | ||
| this.telemetryConfiguration.SetTracesPerSecond(1.5); | ||
| this.telemetryConfiguration.DisableLiveMetrics(); | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify both values | ||
| Assert.Equal(1.5, exporterOptions.TracesPerSecond); | ||
| Assert.False(exporterOptions.EnableLiveMetrics); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region SetStorageDirectory Tests | ||
|
|
||
| [Fact] | ||
| public void SetStorageDirectory_WithValidPath_SetsStorageDirectoryInExporterOptions() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.SetStorageDirectory("C:\\TelemetryStorage"); | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify the actual value | ||
| Assert.Equal("C:\\TelemetryStorage", exporterOptions.StorageDirectory); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetStorageDirectory_WithNullPath_ThrowsArgumentException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<ArgumentException>(() => | ||
| this.telemetryConfiguration.SetStorageDirectory(null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetStorageDirectory_WithEmptyPath_ThrowsArgumentException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<ArgumentException>(() => | ||
| this.telemetryConfiguration.SetStorageDirectory(string.Empty)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetStorageDirectory_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| _ = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.SetStorageDirectory("C:\\TelemetryStorage")); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region DisableOfflineStorage Tests | ||
|
|
||
| [Fact] | ||
| public void DisableOfflineStorage_SetsDisableOfflineStorageInExporterOptions() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.DisableOfflineStorage(); | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify the actual value | ||
| Assert.True(exporterOptions.DisableOfflineStorage); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DisableOfflineStorage_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| _ = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.DisableOfflineStorage()); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region DisableLiveMetrics Tests | ||
|
|
||
| [Fact] | ||
| public void DisableLiveMetrics_SetsEnableLiveMetricsFalseInExporterOptions() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.DisableLiveMetrics(); | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify the actual value | ||
| Assert.False(exporterOptions.EnableLiveMetrics); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DisableLiveMetrics_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| _ = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.DisableLiveMetrics()); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region DisableTraceBasedLogsSampling Tests | ||
|
|
||
| [Fact] | ||
| public void DisableTraceBasedLogsSampling_SetsEnableTraceBasedLogsSamplerFalseInExporterOptions() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.DisableTraceBasedLogsSampling(); | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| var sdk = this.BuildConfiguration(); | ||
| var exporterOptions = this.GetExporterOptions(sdk); | ||
|
|
||
| // Assert - verify the actual value | ||
| Assert.False(exporterOptions.EnableTraceBasedLogsSampler); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DisableTraceBasedLogsSampling_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration by creating a TelemetryClient | ||
| _ = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.DisableTraceBasedLogsSampling()); | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.