Post custom events application insghts using rest api

103 views Asked by At

I'm trying to post custom events in application insights via their REST API.

I have created an API key and try below in postmand but its not working using post method https://api.applicationinsights.io/v1/apps/appid/metadata headers x-api-key and in body passing value like { "name": "customEvents", "time": "2024-02-12T12:00:00Z", "properties": { "customProperty": "customValue" } but if i post nothing get added what i am doing wrong can anyone help?

1

There are 1 answers

2
ZakiMa On

It looks like that ChatGPT suggested your approach. This is not correct. Probably it got confused because ingestion REST API is not documented and as a result there were no samples on the Internet for it to learn.

Despite that Ingestion REST API is not officially documented, this is a public contract. So, one approach is to write an app in any language, get it emit an event you want, inspect the payload (for instance, using Fiddler) and then construct such payload manually and submit it yourself using regular HTTP client for your language of choice.

Here is a sample program which emits Custom Event into Application Insights using .NET console app:

private static void Main(string[] args)
{
    // Create the DI container.
    IServiceCollection services = new ServiceCollection();
    services.AddApplicationInsightsTelemetryWorkerService(
        options => options.ConnectionString =
            "<your connection string from Application Insights resource>");
    // Build ServiceProvider.
    IServiceProvider serviceProvider = services.BuildServiceProvider();

    // Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
    var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

    telemetryClient.TrackEvent("Program Started",
        new Dictionary<string, string>([new KeyValuePair<string, string>("key1", "value1")]));

    telemetryClient.Flush();

    Thread.Sleep(60000);
}

Here is the HTTP call:

POST https://<endpoint from connection string>/v2/track HTTP/1.1

{
    "name": "AppEvents",
    "time": "2024-02-12T18:38:49.0743201Z",
    "iKey": "<instrumentation key>",
    "tags": {
        "ai.application.ver": "1.0.0.0",
        "ai.cloud.roleInstance": "<your instance>",
    },
    "data": {
        "baseType": "EventData",
        "baseData": {
            "ver": 2,
            "name": "Program Started",
            "properties": {
                "key1": "value1"
            }
        }
    }
}

Here is the successful response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
Date: Mon, 12 Feb 2024 18:50:36 GMT
Connection: close
Content-Length: 49

{
  "itemsReceived": 1,
  "itemsAccepted": 1,
  "errors": []
}

In case there are some errors (for instance, invalid instrumentation key was specified), errors property will have detailed explanation:

{
  "itemsReceived": 1,
  "itemsAccepted": 0,
  "errors": [
    {
      "index": 0,
      "statusCode": 400,
      "message": "Invalid instrumentation key"
    }
  ]
}