azure-functions-java-worker icon indicating copy to clipboard operation
azure-functions-java-worker copied to clipboard

Application Insights Telemetry custom properties

Open tonto7973 opened this issue 5 years ago • 1 comments

Hi, I am wondering if there is currently any support for custom AI telemetry properties. I could not find any documentation describing this.

A log statement like this:

context.getLogger().info("Some message");

Shows under traces in Application Insights:

traces
| where message contains "Some message"

with customDimensions that read something like this:

{"HostInstanceId":"548cfa44-cbee-4a92-b930-a4ee99f63466","LogLevel":"Information","Category":"Function.HttpTrigger-Java.User","InvocationId":"ae9b0c4d-9111-4c31-b788-66fa3644d165","ProcessId":"7428"}

Is there a way to add any additional information to customDimensions from Java Azure Function?

Sample case - I'd like to capture some input information from HttpTrigger query, e.g. the id below:

http://localhost:7071/api/HttpTrigger-Java?id=1234

I do not know how to capture the id into customDimensions. I can capture it inside the actual message:

String id = request.getQueryParameters().get("id");
context.getLogger().info("Some message: " + id);

I'd ideally want to propagate such custom properties to any telemetry (traces, dependencies, customEvents, metrics) captured when the function is running. In C# Azure Function I can use Activity tags in combination with ITelemetryInitializer but I am not sure how to do this in Java Azure Function.

Thanks for your help.

tonto7973 avatar Jul 04 '20 15:07 tonto7973

cc @trask

amamounelsayed avatar Jul 22 '20 18:07 amamounelsayed

Hi @tonto7973 , thanks for reaching out and apologies for the late reply. Application Insight provide you the ability to track your own events, metrics, traces and allow you to viem them in corrsponding customEvents, customMetric, traces on the portal. Below is an example of track customize metrics

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import java.util.Optional;

public class Function {
    private static final TelemetryClient telemetryClient = new TelemetryClient();

    @FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<Optional<String>> request) {

        telemetryClient.trackMetric("test", 123);

        telemetryClient.trackTrace("test");

        return request.createResponseBuilder(HttpStatus.OK).body("Hello world!").build();
    }
}

In the portal you can review it in the customMetric and traces as below image image

kaibocai avatar Feb 28 '23 18:02 kaibocai