简体   繁体   中英

Assign namespace and dimension for Azure Application Insights for a custom metric from Java

I am submitting custom metrics from my Java application to Azure Application Insights. Every few seconds a thread wakes up, gets the metrics from the application, and pushes them to Azure. Here is some sample code for how I am doing this:

TelemetryClient telemetryClient = new TelemetryClient();
MetricTelemetry telemetry = new MetricTelemetry();
telemetry.setTimestamp(metricbean.getMetricTimestamp());
telemetry.setName("My custom metric");
telemetry.setValue( metricbean.getValue());
telemetry.setCount(1);
telemetryClient.trackMetric(telemetry);

I am seeing the metrics on the Azure portal which is good. Azure is supposed to support Dimensions and Namespaces. How can I set this using the TelemetryClient API in Java?

Also, is there anyway to check a return code? The "trackMetric()" method is void and does not throw any checked exceptions?

You can add properties to MetricTelemetry by using the following method.

telemetry.getProperties.putIfAbsent(key, value);

trackMetric() is void type and this is by design. If you turn on SDKLogs by adding the following tag in ApplicationInsights.xml, you would see error message when backend responds with error code. SDK also retries on specific error codes.

In order to see the custom dimensions in metric explorer you need to go to "Usage and Estimated Cost" Section and check the Custom metrics preview section.

在此处输入图片说明

Please note that the above steps to enable custom metrics is only needed for viewing them in metrics explore on azure portal. You can still use Analytics tile to view metrics with custom dimensions and do charting with the help of queries.

Although not in Java, I had same issue using Microsoft.AI.PerfCounterCollector in C#.

Basically have to create a custom TelemetryInitializer and add that to the app insights configuration.

Example of adding initializer via C#:

AppInsightsConfig = TelemetryConfiguration.Active;

AppInsightsConfig.TelemetryInitializers.Add(new AppInsightsCloudIdInitializer());

Example of custom AppInsightsCloudIdInitializer:

public class AppInsightsCloudIdInitializer : ITelemetryInitializer
{
    private readonly string CloudRoleName;
    private readonly string CloudRoleInstance;

    public AppInsightsCloudIdInitializer()
    {
        CloudRoleName = "MyRole";
        CloudRoleInstance = "MyInstance";
    }

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is MetricTelemetry metric)
        {
            metric.MetricNamespace = CloudRoleName;
        }
        if (string.IsNullOrWhiteSpace(telemetry.Context.Cloud.RoleInstance) || string.IsNullOrWhiteSpace(telemetry.Context.Cloud.RoleName))
        {
            telemetry.Context.Cloud.RoleInstance = CloudRoleInstance;
            telemetry.Context.Cloud.RoleName = CloudRoleName;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM