Skip to content
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

Add attach statsbeat dimensions #30522

Merged
merged 11 commits into from
Aug 16, 2022
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,20 @@ 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;

internal static string s_resourceProviderId;
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

internal static string s_resourceProvider;

internal static string s_operatingSystem;

internal static string s_runtimeVersion => SdkVersionUtils.GetVersion(typeof(object));

internal static string s_sdkVersion => SdkVersionUtils.GetVersion(typeof(AzureMonitorTraceExporter));

static Statsbeat()
{
s_myMeter.CreateObservableGauge("AttachStatsBeat", () => GetAttachStatsBeat());
Expand All @@ -38,8 +53,79 @@ static Statsbeat()

private static Measurement<int> GetAttachStatsBeat()
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
// TODO: Add additional properties required for statbeat
return new(1, new("cikey", Customer_Ikey), new("language", "dotnet"));
if (s_resourceProvider == null)
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
SetResourceProviderDetails();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protect with try.catch.

}

// TODO: Add os to the list
return
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
new Measurement<int>(1,
new("rp", s_resourceProvider),
new("rpId", s_resourceProviderId),
new("attach", "sdk"),
new("cikey", Customer_Ikey),
new("runtimeVersion", s_runtimeVersion),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this .net runtime version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

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
{
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
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
public string osType { get; set; }

public string subscriptionId { get; set; }

public string vmId { get; set; }
}
}