/WebJobs.ApplicationInsightsTracer

An Application Insights tracer extension for the Azure WebJob SDK

Primary LanguageC#MIT LicenseMIT

WebJobs SDK Application Insights Extension

This extension adds a binding for sending traces and telemetry (Requrests, Exceptions, Dependencies) to Application Insights .

public void SimpleAITracerBinding([WebHookTrigger] Message m, AITracer aiTracer)
{
    aiTracer.TraceInformation("Function started!");
}

See here for more info about Azure WebJobs SDK Extensions.

Installation

You can obtain it through Nuget with:

Install-Package WebJobs.ApplicationInsightsTracer

Or clone this repo and reference it.

Usage

The Application Insights binding returns a AITracer object implements an interface for sending tracers and telemetry messages to Application Insights endpoint.

A full example with different telemetry options:

public void FullAITracerBinding([WebHookTrigger] Message m, AITracer aiTracer)
{
    // Create a request operation to wrap all the current trigger telemetry under a single group (i.e. Operation)
        aiTracer.StartOperation("Test Operation");

        try
        {
            // Simple trace
            aiTracer.TraceInformation("Function started!");
            throw new Exception("Test Failure");
        }
        catch (Exception e)
        {
            // Report the exception to see full exception details in the Application Insights portal (including full Stack Trace)
            aiTracer.ReportException(e);

            // Mark the operation as failure to see it in failed requests section
            aiTracer.MarkOperationAsFailure();
        }
        finally
        {
            // Eventually, close the operation for this job
            aiTracer.DispatchOperation();

            // Remeber to flush the telemetry buffer before finising the job
            aiTracer.Flush();
        }
}

You can enable the Extension via the JobHostConfiguration object.

var config = new JobHostConfiguration();
// Use the AI Tracer extension with default configuration
// The Instrumentation key can be also taken from the app settings
config.UseAITracer("<IKEY_HERE>");

Or you can create a new TelemetryConfiguration from the Applciation Insights SDK and pass it as a parameter.

var config = new JobHostConfiguration();
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.InstrumentationKey = "<IKEY_HERE>";
config.UseAITracer(telemetryConfiguration);

Using the AITracer on different functions

If your scenario includes creating a unique AITracer for each function that sends telemetry to a different Ikey, this can achieved using the AITracerparameter attributes.

public void SimpleAITracerBinding([WebHookTrigger] Message m,
    [AITracerConfiguration(InstrumentationKey = "<IKEY_HERE>")] AITracer aiTracer)
{
    aiTracer.TraceInformation("Function started!");
}

This creates a default TelemetryConfiguration with the new Ikey. The AITracerConfiguration can also take a TelemetryConfiguration as a parameter instead of using the default one.

Notes

This is currently still in development. Not for production use.

License

MIT