0
votes

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?

2
Regarding TelemetryClient API, could you please share some links or sample? is it app insights REST API?Ivan Yang
I am using the Java SDK. Here is a link on using Java with Application Insights: docs.microsoft.com/en-us/azure/application-insights/… Here is another link describing the API: docs.microsoft.com/en-us/azure/application-insights/…plex4r
I know the dimension, but the namespace for a custom metric means what? Maybe I'm not understanding the issue well, could you help clarify it more clearly?Ivan Yang
Ivan you should read the docs I posted.plex4r

2 Answers

1
votes

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;
        }
    }
}
0
votes

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.

enter image description here

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.