From 4973d43e7156841184624b392e00d91a60d2c236 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 16 Aug 2022 11:58:09 -0700 Subject: [PATCH] Add attach statsbeat dimensions (#30522) * add attach statsbeat dimensions * refactor * Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs Co-authored-by: Rajkumar Rangaraj * Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs Co-authored-by: Rajkumar Rangaraj * rmv vmId * Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs Co-authored-by: Rajkumar Rangaraj * refactor * refactor * refactor * resolve PR comments Co-authored-by: Rajkumar Rangaraj --- .../src/Internals/SdkVersionUtils.cs | 2 +- .../src/Internals/Statsbeat/Statsbeat.cs | 89 ++++++++++++++++++- .../Internals/Statsbeat/VmMetadataResponse.cs | 14 +++ 3 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/VmMetadataResponse.cs diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/SdkVersionUtils.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/SdkVersionUtils.cs index c6e36867ba6fc..19d6e0481b8f9 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/SdkVersionUtils.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/SdkVersionUtils.cs @@ -33,7 +33,7 @@ private static string GetSdkVersion() } } - private static string GetVersion(Type type) + internal static string GetVersion(Type type) { try { diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs index 2c731e0771b30..1d6527a40a6b6 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs @@ -1,7 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; using System.Diagnostics.Metrics; +using System.Net.Http; +using System.Text.Json; using OpenTelemetry; using OpenTelemetry.Metrics; @@ -13,8 +16,18 @@ internal static class Statsbeat private const string StatsBeat_ConnectionString = ""; + private const string AMS_Url = "http://169.254.169.254/metadata/instance/compute?api-version=2017-08-01&format=json"; + internal const int AttachStatsBeatInterval = 86400000; + private static string s_resourceProviderId; + + private static string s_resourceProvider; + + private static string s_runtimeVersion => SdkVersionUtils.GetVersion(typeof(object)); + + private static string s_sdkVersion => SdkVersionUtils.GetVersion(typeof(AzureMonitorTraceExporter)); + static Statsbeat() { s_myMeter.CreateObservableGauge("AttachStatsBeat", () => GetAttachStatsBeat()); @@ -38,8 +51,80 @@ static Statsbeat() private static Measurement GetAttachStatsBeat() { - // TODO: Add additional properties required for statbeat - return new(1, new("cikey", Customer_Ikey), new("language", "dotnet")); + if (s_resourceProvider == null) + { + SetResourceProviderDetails(); + } + + // TODO: Add os to the list + return + new Measurement(1, + new("rp", s_resourceProvider), + new("rpId", s_resourceProviderId), + new("attach", "sdk"), + new("cikey", Customer_Ikey), + new("runtimeVersion", s_runtimeVersion), + new("language", "dotnet"), + new("version", s_sdkVersion)); + } + + private static VmMetadataResponse GetVmMetadataResponse() + { + try + { + using (var httpClient = new HttpClient()) + { + httpClient.DefaultRequestHeaders.Add("Metadata", "True"); + var responseString = httpClient.GetStringAsync(AMS_Url); + var vmMetadata = JsonSerializer.Deserialize(responseString.Result); + + return vmMetadata; + } + } + catch (Exception ex) + { + AzureMonitorExporterEventSource.Log.WriteWarning("Failed to get VM metadata details", ex); + return null; + } + } + + private static void SetResourceProviderDetails() + { + var appSvcWebsiteName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"); + if (appSvcWebsiteName != null) + { + s_resourceProvider = "appsvc"; + s_resourceProviderId = appSvcWebsiteName; + var appSvcWebsiteHostName = Environment.GetEnvironmentVariable("WEBSITE_HOME_STAMPNAME"); + if (!string.IsNullOrEmpty(appSvcWebsiteHostName)) + { + s_resourceProviderId += "/" + appSvcWebsiteHostName; + } + + return; + } + + var functionsWorkerRuntime = Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME"); + if (functionsWorkerRuntime != null) + { + s_resourceProvider = "functions"; + s_resourceProviderId = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME"); + + return; + } + + var vmMetadata = GetVmMetadataResponse(); + + if (vmMetadata != null) + { + s_resourceProvider = "vm"; + s_resourceProviderId = s_resourceProviderId = vmMetadata.vmId + "/" + vmMetadata.subscriptionId; + + return; + } + + s_resourceProvider = "unknown"; + s_resourceProviderId = "unknown"; } } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/VmMetadataResponse.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/VmMetadataResponse.cs new file mode 100644 index 0000000000000..d338ea2f0a8fc --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/VmMetadataResponse.cs @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Azure.Monitor.OpenTelemetry.Exporter.Internals +{ + internal class VmMetadataResponse + { + public string osType { get; set; } + + public string subscriptionId { get; set; } + + public string vmId { get; set; } + } +}