Skip to content

Commit

Permalink
Add attach statsbeat dimensions (Azure#30522)
Browse files Browse the repository at this point in the history
* add attach statsbeat dimensions

* refactor

* Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>

* Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>

* rmv vmId

* Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/Statsbeat.cs

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>

* refactor

* refactor

* refactor

* resolve PR comments

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
  • Loading branch information
vishweshbankwar and rajkumar-rangaraj authored Aug 16, 2022
1 parent c1fb11d commit 4973d43
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static string GetSdkVersion()
}
}

private static string GetVersion(Type type)
internal static string GetVersion(Type type)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,8 +16,18 @@ internal static class Statsbeat

private const string StatsBeat_ConnectionString = "<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());
Expand All @@ -38,8 +51,80 @@ static Statsbeat()

private static Measurement<int> 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<int>(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<VmMetadataResponse>(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";
}
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit 4973d43

Please sign in to comment.