-
Notifications
You must be signed in to change notification settings - Fork 294
Add DisableTelemetry support using OpenTelemetry OTEL_SDK_DISABLED #3084
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d6c053
Initial commit
rajkumar-rangaraj aa66a7f
update concept
rajkumar-rangaraj c26f340
Merge remote-tracking branch 'origin/main' into rajrang/disablesdk
rajkumar-rangaraj df63239
changelog update
rajkumar-rangaraj a801f8c
update concepts
rajkumar-rangaraj 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
107 changes: 107 additions & 0 deletions
107
...ts.Test/Microsoft.ApplicationInsights.Tests/TelemetryConfigurationOtelSdkDisabledTests.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,107 @@ | ||
| namespace Microsoft.ApplicationInsights.Tests | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using Microsoft.ApplicationInsights.DataContracts; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
| using Microsoft.Extensions.Logging; | ||
| using OpenTelemetry; | ||
| using OpenTelemetry.Logs; | ||
| using OpenTelemetry.Metrics; | ||
| using OpenTelemetry.Trace; | ||
| using Xunit; | ||
|
|
||
| /// <summary> | ||
| /// Tests for DisableTelemetry property in TelemetryConfiguration. | ||
| /// Verifies that when DisableTelemetry is set to true, the OTEL_SDK_DISABLED environment variable | ||
| /// is set and telemetry data does not flow through the OpenTelemetry SDK. | ||
| /// </summary> | ||
| [Collection("TelemetryClientTests")] | ||
| public class TelemetryConfigurationOtelSdkDisabledTests | ||
| { | ||
| private const string OtelSdkDisabledEnvVar = "OTEL_SDK_DISABLED"; | ||
|
|
||
| [Fact] | ||
| public void WhenDisableTelemetryIsTrue_TelemetryDataIsNotExported() | ||
| { | ||
| // Save original value to restore after test | ||
| var originalEnvValue = Environment.GetEnvironmentVariable(OtelSdkDisabledEnvVar); | ||
|
|
||
| try | ||
| { | ||
| // Arrange | ||
| var logItems = new List<LogRecord>(); | ||
| var activityItems = new List<Activity>(); | ||
| var configuration = new TelemetryConfiguration(); | ||
| configuration.ConnectionString = "InstrumentationKey=" + Guid.NewGuid().ToString(); | ||
| configuration.DisableTelemetry = true; | ||
| configuration.ConfigureOpenTelemetryBuilder(b => b | ||
| .WithLogging(l => l.AddInMemoryExporter(logItems)) | ||
| .WithTracing(t => t.AddInMemoryExporter(activityItems))); | ||
|
|
||
| var telemetryClient = new TelemetryClient(configuration); | ||
|
|
||
| // Act | ||
| telemetryClient.TrackEvent("TestEvent"); | ||
| telemetryClient.TrackTrace("TestTrace"); | ||
| telemetryClient.TrackDependency("HTTP", "example.com", "GET /api", DateTimeOffset.Now, TimeSpan.FromMilliseconds(100), true); | ||
| telemetryClient.Flush(); | ||
|
|
||
| // Assert - No data should be exported when telemetry is disabled | ||
| Assert.Empty(logItems); | ||
| Assert.Empty(activityItems); | ||
| Assert.Equal("true", Environment.GetEnvironmentVariable(OtelSdkDisabledEnvVar)); | ||
|
|
||
| // Cleanup | ||
| configuration.Dispose(); | ||
| } | ||
| finally | ||
| { | ||
| // Restore original environment variable value | ||
| Environment.SetEnvironmentVariable(OtelSdkDisabledEnvVar, originalEnvValue); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenDisableTelemetryIsFalse_TelemetryDataIsExported() | ||
| { | ||
| // Save original value to restore after test | ||
| var originalEnvValue = Environment.GetEnvironmentVariable(OtelSdkDisabledEnvVar); | ||
|
|
||
| try | ||
| { | ||
| // Arrange | ||
| var logItems = new List<LogRecord>(); | ||
| var configuration = new TelemetryConfiguration(); | ||
| configuration.SamplingRatio = 1.0f; | ||
| configuration.ConnectionString = "InstrumentationKey=" + Guid.NewGuid().ToString(); | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| configuration.DisableTelemetry = false; | ||
| configuration.ConfigureOpenTelemetryBuilder(b => b | ||
| .WithLogging(l => l.AddInMemoryExporter(logItems))); | ||
|
|
||
| var telemetryClient = new TelemetryClient(configuration); | ||
|
|
||
| // Act | ||
| telemetryClient.TrackEvent("TestEvent"); | ||
| telemetryClient.Flush(); | ||
|
|
||
| // Assert - Data should be exported when telemetry is enabled | ||
| Assert.NotEmpty(logItems); | ||
| var logRecord = logItems.FirstOrDefault(l => | ||
| l.Attributes != null && l.Attributes.Any(a => | ||
| a.Key == "microsoft.custom_event.name" && a.Value?.ToString() == "TestEvent")); | ||
| Assert.NotNull(logRecord); | ||
|
|
||
| // Cleanup | ||
| configuration.Dispose(); | ||
| } | ||
| finally | ||
| { | ||
| // Restore original environment variable value | ||
| Environment.SetEnvironmentVariable(OtelSdkDisabledEnvVar, originalEnvValue); | ||
| } | ||
| } | ||
| } | ||
| } | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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
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
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
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
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
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
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
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.