Application Insights sdk for go is not sending availability test results

103 Views Asked by At

I am using following azure application insights sdk for go - https://github.com/microsoft/ApplicationInsights-Go/tree/master

and I wanted to use following close method from following file -

https://github.com/microsoft/ApplicationInsights-Go/blob/master/appinsights/telemetrychannel.go

but it is not sending telemetry to application insights but when I am using Flush method then it is sending the telemetry. But the problem with flush method is that I have to put extra sleep method (with 2 or 5 seconds of sleep).

Please not I am directly calling the method (cant share full code snippet):

appInsClient.Channel.Close() and result which I am getting is empty channel.

if possible please share the code snippet that I can if I want to execute following method.

1

There are 1 best solutions below

0
Suresh Chikkam On

If you're directly calling appInsClient.Channel.Close() and it is not sending telemetry to application insights.

It is due to the asynchronous nature of telemetry submission in the SDK.

  • Introduces a short delay (1 second in this case) after flushing the channel and before calling the Close method.

  • The delay allows time for the telemetry to be processed and sent. Adjust the delay duration based on the application's requirements.

Here is the code is to use a combination of the Flush and Close methods along with a short delay.

Code:

package main

import (
    "fmt"
    "github.com/microsoft/ApplicationInsights-Go/appinsights"
    "github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
    "time"
)

func main() {
    // Initialize Application Insights client
    appInsClient := appinsights.NewTelemetryClient("YOUR_INSTRUMENTATION_KEY")

    // Run some operations or generate telemetry items
    for i := 1; i <= 5; i++ {
        // Simulate some telemetry
        telemetryItem := contracts.NewTraceTelemetry(fmt.Sprintf("Telemetry message %d", i))
        appInsClient.Track(telemetryItem)
    }

    // Flush the telemetry channel
    appInsClient.Channel.Flush()

    // Introduce a short delay (e.g., 1 second) before closing
    time.Sleep(1 * time.Second)

    // Close the telemetry channel
    closeTimeout := 5 * time.Second
    resultChannel := appInsClient.Channel.Close(closeTimeout)

    // Wait for the result channel to be closed
    select {
    case <-resultChannel:
        fmt.Println("Telemetry items sent successfully.")
    case <-time.After(closeTimeout):
        fmt.Println("Timeout: Telemetry items may not have been sent.")
    }

    // Optionally, add a sleep to ensure the telemetry is sent before exiting
    time.Sleep(2 * time.Second)
}
  • This delay is introduced to allow the SDK time to process and send any telemetry items that are pending.

Availability Check: enter image description here