how to add custom property in application insight from apim policies

Shah, Maitree 0 Reputation points
2024-03-27T09:50:06.1266667+00:00

i need to access my variable values of the apim in application insight's custom properties (inside transaction history )

how can i do it

i have tried using trace

in my apim policy i have added this in my policy how can i access the value of the variable

<outbound>
        <set-variable name="transferWSResponseVariable" value="@((string)context.Response.Body?.As<string>())" />
        <!-- Add a trace to log the variable value -->
        <trace source="PetStore API" severity="information">
            <message>@((string)context.Variables["transferWSResponseVariable"])</message>
            <metadata name="Operation Name" value="New-Order" />
        </trace>
</outbound>


tried this log query not getting any results

traces
| where timestamp >= ago(1d)  // Filter by time range as needed
| where customDimensions.OperationName == "New-Order"  // Adjust based on your metadata
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,802 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 3,561 Reputation points
    2024-03-28T16:13:16.9933333+00:00

    Hello Shah, Maitree ,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Sequel to your questions, I understand that you would like to access variable values set in Azure API Management (APIM) within Application Insights custom properties for transaction history. You have configured APIM policies to set a variable and log its value using a trace, but unable to retrieve the variable value from Application Insights.

    So, based on what you've done so far. To be able to retrieve the variable value from Application Insights and to create custom property. Examine the codes below and follow the listed steps:

    1. Double-check your trace configuration in APIM policy to ensure it's correctly logging the variable value.
    2. Ensure that your Application Insights instance is set up correctly to receive and process traces from APIM.
    3. Try logging a different variable or a static message in the trace to see if it appears in Application Insights.

    Therefore, to add a custom property in Application Insights from APIM policies, you typically utilize the setProperty function within your policy. Below is an example of how to achieve this:

    <outbound>
        <!-- Set a custom property in Application Insights -->
        <set-header name="x-ms-request-id" exists-action="override">
            <value>@(context.RequestId)</value>
        </set-header>
        <!-- Log a trace with the custom property -->
        <trace source="APIM" severity="information">
            <message>@("Request ID: " + context.RequestId)</message>
        </trace>
    </outbound>
    

    In the above code, context.RequestId is used to obtain a unique identifier for the request. This value is then set as a custom property in Application Insights using the set-header policy. Additionally, a trace is logged with the custom property to provide visibility into the request ID.

    You can customize the set-header policy according to your requirements, setting different values or extracting data from various parts of the request or response. The key is to ensure that the custom property you set in the policy aligns with what you intend to track in Application Insights.

    Secondly, to retrieve the variable value from Application Insights, you need to use log queries. Here is the step-by-step guide:

    • First, ensure that the variable value is logged correctly in Application Insights. This is typically done using custom properties or traces within your Azure API Management (APIM) policies.
    • Understand how the variable value is logged in Application Insights. Depending on how you've configured logging in your APIM policies, the variable value might be logged as a custom property or within trace messages.
    • Use the Log Analytics query language to retrieve the logged data from Application Insights. Your query should filter by the appropriate criteria to find the logged variable value.
    • Run the log query in the Application Insights Logs Explorer or through the Azure portal to retrieve the variable value.
    • Once you have executed the query, analyze the results to ensure that the variable value is included and matches your expectations.

    This is an example of a log query to retrieve variable values logged as custom properties:

    traces
    | where timestamp >= ago(1d)  // Filter by time range as needed
    | where customDimensions.OperationName == "New-Order"  // Filter by operation name
    | project customDimensions.transferWSResponseVariable  // Select the custom property containing the variable value
    

    In this query:

    traces specifies that we're querying trace data.

    timestamp >= ago(1d) filters the data to the last day (adjust as needed).

    customDimensions.OperationName == "New-Order" filters by the operation name to narrow down the results. Project customDimensions.transferWSResponseVariable selects the custom property containing the variable value for display.

    After executing this similar query, you should see the variable values logged in the Application Insights trace data. If the variable values are not appearing as expected, double-check your logging configuration in APIM policies and ensure that the correct custom properties or traces are being logged. Adjust the query as necessary to match the structure of your logged data.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    Please remember to "Accept Answer" if answer helped, so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    0 comments No comments