-
Notifications
You must be signed in to change notification settings - Fork 307
Bump Microsoft.ApplicationInsights to 3.1.2 and add local telemetry export #9725
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
base: main
Are you sure you want to change the base?
Changes from all commits
e84cd28
faf88bf
5051d30
143bd79
5ab57e0
33b6063
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Testing.Platform; | ||
|
|
||
| namespace Microsoft.Testing.Extensions.Telemetry; | ||
|
|
||
| internal sealed class AppInsightTelemetryClientFactory : ITelemetryClientFactory | ||
| { | ||
| private readonly string? _localExportFilePath; | ||
|
|
||
| public AppInsightTelemetryClientFactory(string? localExportFilePath = null) | ||
| => _localExportFilePath = localExportFilePath; | ||
|
|
||
| public ITelemetryClient Create(string? currentSessionId, string osVersion) | ||
| => new AppInsightTelemetryClient(currentSessionId, osVersion); | ||
| => RoslynString.IsNullOrWhiteSpace(_localExportFilePath) | ||
| ? new AppInsightTelemetryClient(currentSessionId, osVersion) | ||
| : new LocalFileTelemetryClient(_localExportFilePath, currentSessionId, osVersion); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| #nullable enable | ||
| *REMOVED*Microsoft.Testing.Extensions.Telemetry.AppInsightTelemetryClientFactory.AppInsightTelemetryClientFactory() -> void | ||
| Microsoft.Testing.Extensions.Telemetry.AppInsightTelemetryClientFactory.AppInsightTelemetryClientFactory(string? localExportFilePath = null) -> void | ||
| Microsoft.Testing.Extensions.Telemetry.LocalFileTelemetryClient | ||
| Microsoft.Testing.Extensions.Telemetry.LocalFileTelemetryClient.Flush() -> void | ||
| Microsoft.Testing.Extensions.Telemetry.LocalFileTelemetryClient.LocalFileTelemetryClient(string! filePath, string? currentSessionId, string! osVersion) -> void | ||
| Microsoft.Testing.Extensions.Telemetry.LocalFileTelemetryClient.TrackEvent(string! eventName, System.Collections.Generic.Dictionary<string!, string!>! properties, System.Collections.Generic.Dictionary<string!, double>! metrics) -> void | ||
| Microsoft.Testing.Platform.Helpers.RuntimeFeatureHelper | ||
| Microsoft.Testing.Platform.Messages.SingleConsumerUnboundedChannel<T>.WaitToRead() -> bool | ||
| Microsoft.Testing.Platform.RoslynDebug | ||
| static Microsoft.Testing.Platform.Helpers.RuntimeFeatureHelper.IsMultiThreaded.get -> bool | ||
| static Microsoft.Testing.Platform.RoslynDebug.Assert(bool b) -> void | ||
| static Microsoft.Testing.Platform.RoslynDebug.Assert(bool b, string! message) -> void | ||
| static readonly Microsoft.Testing.Extensions.Telemetry.AppInsightsProvider.LocalExportPathEnvVar -> string! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Testing.Platform; | ||
|
|
||
| namespace Microsoft.Testing.Extensions.Telemetry; | ||
|
|
||
| /// <summary> | ||
| /// A local, network-free <see cref="ITelemetryClient"/> that appends every telemetry event to a | ||
| /// file as a single JSON line (JSON Lines / NDJSON). It is the "local exporter" equivalent used to | ||
| /// verify what telemetry would be collected — without shipping anything to Application Insights. | ||
| /// It is selected instead of <see cref="AppInsightTelemetryClient"/> when the | ||
| /// <see cref="AppInsightsProvider.LocalExportPathEnvVar"/> environment variable points at a file. | ||
| /// </summary> | ||
| internal sealed class LocalFileTelemetryClient : ITelemetryClient | ||
| { | ||
| private readonly string _filePath; | ||
| private readonly string? _sessionId; | ||
| private readonly string _osVersion; | ||
|
|
||
| public LocalFileTelemetryClient(string filePath, string? currentSessionId, string osVersion) | ||
| { | ||
| _filePath = filePath; | ||
| _sessionId = currentSessionId; | ||
| _osVersion = osVersion; | ||
|
|
||
| string? directory = Path.GetDirectoryName(_filePath); | ||
| if (!RoslynString.IsNullOrEmpty(directory)) | ||
| { | ||
| Directory.CreateDirectory(directory); | ||
| } | ||
| } | ||
|
|
||
| public void TrackEvent(string eventName, Dictionary<string, string> properties, Dictionary<string, double> metrics) | ||
| { | ||
| var builder = new StringBuilder(); | ||
| builder.Append('{'); | ||
| AppendJsonString(builder, "eventName", eventName); | ||
| builder.Append(','); | ||
| AppendJsonString(builder, "sessionId", _sessionId ?? string.Empty); | ||
| builder.Append(','); | ||
| AppendJsonString(builder, "osVersion", _osVersion); | ||
|
|
||
| builder.Append(",\"properties\":{"); | ||
| bool first = true; | ||
| foreach (KeyValuePair<string, string> property in properties) | ||
| { | ||
| if (!first) | ||
| { | ||
| builder.Append(','); | ||
| } | ||
|
|
||
| AppendJsonString(builder, property.Key, property.Value); | ||
| first = false; | ||
| } | ||
|
|
||
| builder.Append("},\"metrics\":{"); | ||
| first = true; | ||
| foreach (KeyValuePair<string, double> metric in metrics) | ||
| { | ||
| if (!first) | ||
| { | ||
| builder.Append(','); | ||
| } | ||
|
|
||
| AppendJsonKey(builder, metric.Key); | ||
| builder.Append(metric.Value.ToString("R", CultureInfo.InvariantCulture)); | ||
| first = false; | ||
| } | ||
|
|
||
| builder.Append("}}"); | ||
|
|
||
| // TrackEvent is only ever invoked from the provider's single-consumer ingest loop, so no | ||
| // synchronization is needed here (mirroring AppInsightTelemetryClient). | ||
| File.AppendAllText(_filePath, builder.ToString() + Environment.NewLine); | ||
| } | ||
|
|
||
| // No-op: writes are flushed to disk synchronously as each event is tracked. | ||
| public void Flush() | ||
| { | ||
| } | ||
|
|
||
| private static void AppendJsonString(StringBuilder builder, string key, string value) | ||
| { | ||
| AppendJsonKey(builder, key); | ||
| AppendEscaped(builder, value); | ||
| } | ||
|
|
||
| private static void AppendJsonKey(StringBuilder builder, string key) | ||
| { | ||
| AppendEscaped(builder, key); | ||
| builder.Append(':'); | ||
| } | ||
|
|
||
| private static void AppendEscaped(StringBuilder builder, string value) | ||
| { | ||
| builder.Append('"'); | ||
| foreach (char c in value) | ||
| { | ||
| switch (c) | ||
| { | ||
| case '"': | ||
| builder.Append("\\\""); | ||
| break; | ||
| case '\\': | ||
| builder.Append("\\\\"); | ||
| break; | ||
| case '\b': | ||
| builder.Append("\\b"); | ||
| break; | ||
| case '\f': | ||
| builder.Append("\\f"); | ||
| break; | ||
| case '\n': | ||
| builder.Append("\\n"); | ||
| break; | ||
| case '\r': | ||
| builder.Append("\\r"); | ||
| break; | ||
| case '\t': | ||
| builder.Append("\\t"); | ||
| break; | ||
| default: | ||
| if (c < ' ') | ||
| { | ||
| builder.Append("\\u"); | ||
| builder.Append(((int)c).ToString("x4", CultureInfo.InvariantCulture)); | ||
| } | ||
| else | ||
| { | ||
| builder.Append(c); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| builder.Append('"'); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,11 @@ internal static class TelemetryProperties | |
| public const string ReporterIdPropertyName = "reporter id"; | ||
| public const string IsCIPropertyName = "is ci"; | ||
|
|
||
| public const string VersionValue = "20"; | ||
| // Bump this whenever the telemetry schema changes so dashboards can branch old vs new. | ||
| // 21: Microsoft.ApplicationInsights 3.x (OpenTelemetry shim) removed TrackEvent's metrics | ||
| // parameter, so numeric measurements are now folded into event properties | ||
| // (customDimensions) instead of customMeasurements. See #7465. | ||
| public const string VersionValue = "21"; | ||
|
Comment on lines
+13
to
+17
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I traced this and the analysis holds. I am deliberately not flipping the constant here. The bump to
Either is a handful of lines, but picking between them changes the Kusto contract documented in the PR description, so I would rather not choose unsupervised. Leaving this unresolved for a maintainer call - happy to implement whichever you prefer. Note Auto-replied by the GitHub Copilot app. |
||
|
|
||
| public const string True = "true"; | ||
| public const string False = "false"; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.