Application Insights API

Implements

Constructors

Properties

appInsights: AnalyticsPlugin

Access to the Dynamic Configuration for the current instance

context: ITelemetryContext
otelApi: IOTelApi

The OpenTelemetry API instance associated with this instance Unlike OpenTelemetry, this API does not return a No-Op implementation and returns null if the SDK has been torn down or not yet initialized.

pluginVersionString: string

The formatted string of the installed plugins that contain a version number

pluginVersionStringArr: string[]

An array of the installed plugins that provide a version

snippet: Snippet
trace: ITraceApi

OpenTelemetry trace API for creating spans. Unlike OpenTelemetry, this API does not return a No-Op implementation and returns null if the SDK has been torn down or not yet initialized.

Methods

  • Add an dependency telemetry initializer callback function to allow populating additional properties or drop the request. It is called after the dependency call has completed and any available performance details are available. A dependency initializer is similar to the TelemetryInitializer function but it allows you to block the reporting of the dependency request so that it doesn't count against the maxAjaxCallsPerView.

    Parameters

    Returns IDependencyInitializerHandler

    • A IDependencyInitializerHandler to enable the initializer to be removed
  • Add a new plugin to the installation

    Type Parameters

    Parameters

    • plugin: T

      The new plugin to add

    • OptionalreplaceExisting: boolean

      should any existing plugin be replaced, default is false

    • OptionaldoAsync: boolean

      Should the add be performed asynchronously

    • OptionaladdCb: ((added?: boolean) => void)

      [Optional] callback to call after the plugin has been added

        • (added?): void
        • Parameters

          • Optionaladded: boolean

          Returns void

    Returns void

  • Add an unload handler that will be called when the SDK is being unloaded

    Parameters

    • handler: UnloadHandler

      the handler

    Returns void

  • Call any functions that were queued before the main script was loaded

    Returns void

  • Attempt to flush data immediately; If executing asynchronously (the default) and you DO NOT pass a callback function then a IPromise will be returned which will resolve once the flush is complete. The actual implementation of the IPromise will be a native Promise (if supported) or the default as supplied by ts-async library

    Parameters

    • Optionalasync: boolean

      send data asynchronously when true

    • OptionalcallBack: (() => void)

      if specified, notify caller when send is complete, the channel should return true to indicate to the caller that it will be called. If the caller doesn't return true the caller should assume that it may never be called.

        • (): void
        • Returns void

    Returns void | IPromise<void>

    • If a callback is provided true to indicate that callback will be called after the flush is complete otherwise the caller should assume that any provided callback will never be called, Nothing or if occurring asynchronously a IPromise which will be resolved once the unload is complete, the IPromise will only be returned when no callback is provided and async is true.
  • Return the current active span, if no trace provider is available null will be returned but when a trace provider is available a span instance will always be returned, even if there is no active span (in which case a non-recording span will be returned).

    Parameters

    • OptionalcreateNew: boolean

      Optional flag to create a non-recording span if no active span exists, defaults to true. When false, returns the existing active span or null without creating a non-recording span.

    Returns IReadableSpan

    The current active span or null if no trace provider is available or if createNew is false and no active span exists

    3.4.0

  • Watches and tracks changes for accesses to the current config, and if the accessed config changes the handler will be recalled.

    Parameters

    • handler: WatcherFunction<IConfiguration>

      The handler to call when the config changes

    Returns IUnloadHook

    A watcher handler instance that can be used to remove itself when being unloaded

  • Manually trigger an immediate send of all telemetry still in the buffer using beacon Sender. Fall back to xhr sender if beacon is not supported.

    Parameters

    • async: boolean = true

      send data asynchronously when true, default is true

    Returns void

  • Set the current Active Span, if no trace provider is available the span will be not be set as the active span.

    Parameters

    • span: IReadableSpan

      The span to set as the active span

    Returns ISpanScope<ITraceHost<IConfiguration>>

    An ISpanScope instance that provides the current scope, the span will always be the span passed in even when no trace provider is available

    3.4.0

  • Set the authenticated user id and the account id. Used for identifying a specific signed-in user. Parameters must not contain whitespace or ,;=|

    The method will only set the authenticatedUserId and accountId in the current page view. To set them for the whole session, you should set storeInCookie = true

    Parameters

    • authenticatedUserId: string

      The account ID to set

    • OptionalaccountId: string

      The account ID to set

    • storeInCookie: boolean = false

      Whether the values should be set for the whole session

    Returns void

  • Sets the current distributed trace context for this instance if available

    Parameters

    • newTraceCtx: IDistributedTraceContext

    Returns void

  • Set the trace provider for creating spans. This allows different SKUs to provide their own span implementations.

    Parameters

    • provider: ICachedValue<ITraceProvider>

      The trace provider to use for span creation

    Returns void

    3.4.0

  • Start a new span with the given name and optional parent context.

    Note: This method only creates and returns the span. It does not automatically set the span as the active trace context. Context management should be handled separately using setTraceCtx() if needed.

    Parameters

    • name: string

      The name of the span

    • Optionaloptions: IOTelSpanOptions

      Options for creating the span (kind, attributes, startTime)

    • Optionalparent: IDistributedTraceContext

      Optional parent context. If not provided, uses the current active trace context

    Returns IReadableSpan

    A new span instance, or null if no trace provider is available

    3.4.0

  • Starts the timer for tracking a page load time. Use this instead of trackPageView if you want to control when the page view timer starts and stops, but don't want to calculate the duration yourself. This method doesn't send any telemetry. Call stopTrackPage to log the end of the page view and send the event.

    Parameters

    • Optionalname: string

      A string that idenfities this item, unique within this HTML document. Defaults to the document title.

    Returns void

  • Returns void

  • Log an extended event that you started timing with startTrackEvent.

    Parameters

    • name: string

      The string you used to identify this event in startTrackEvent.

    • Optionalproperties: {
          [key: string]: string;
      }

      map[string, string] - additional data used to filter events and metrics in the portal. Defaults to empty.

      • [key: string]: string
    • Optionalmeasurements: {
          [key: string]: number;
      }

      map[string, number] - metrics associated with this event, displayed in Metrics Explorer on the portal. Defaults to empty.

      • [key: string]: number

    Returns void

  • Stops the timer that was started by calling startTrackPage and sends the pageview load time telemetry with the specified properties and measurements. The duration of the page view will be the time between calling startTrackPage and stopTrackPage.

    Parameters

    • Optionalname: string

      The string you used as the name in startTrackPage. Defaults to the document title.

    • Optionalurl: string

      a relative or absolute URL that identifies the page or other item. Defaults to the window location.

    • Optionalproperties: {
          [key: string]: string;
      }

      additional data used to filter pages and metrics in the portal. Defaults to empty.

      • [key: string]: string
    • Optionalmeasurements: {
          [key: string]: number;
      }

      metrics associated with this page, displayed in Metrics Explorer on the portal. Defaults to empty.

      • [key: string]: number

    Returns void

  • Log a numeric value that is not associated with a specific event. Typically used to send regular reports of performance indicators.

    To send a single measurement, just use the name and average fields of IMetricTelemetry.

    If you take measurements frequently, you can reduce the telemetry bandwidth by aggregating multiple measurements and sending the resulting average and modifying the sampleCount field of IMetricTelemetry.

    Parameters

    • metric: IMetricTelemetry

      input object argument. Only name and average are mandatory.

    • OptionalcustomProperties: ICustomProperties

      Additional custom properties to include in the event.

    Returns void

  • Unload and Tear down the SDK and any initialized plugins, after calling this the SDK will be considered to be un-initialized and non-operational, re-initializing the SDK should only be attempted if the previous unload call return true stating that all plugins reported that they also unloaded, the recommended approach is to create a new instance and initialize that instance. This is due to possible unexpected side effects caused by plugins not supporting unload / teardown, unable to successfully remove any global references or they may just be completing the unload process asynchronously. If you pass isAsync as true and do not provide If you pass isAsync as true (also the default) and DO NOT pass a callback function then an IPromise will be returned which will resolve once the unload is complete. The actual implementation of the IPromise will be a native Promise (if supported) or the default as supplied by ts-async library

    Parameters

    • OptionalisAsync: boolean

      Can the unload be performed asynchronously (default)

    • OptionalunloadComplete: ((unloadState: ITelemetryUnloadState) => void)

      An optional callback that will be called once the unload has completed

        • (unloadState): void
        • Parameters

          • unloadState: ITelemetryUnloadState

          Returns void

    • OptionalcbTimeout: number

      An optional timeout to wait for any flush operations to complete before proceeding with the unload. Defaults to 5 seconds.

    Returns void | IPromise<ITelemetryUnloadState>

    Nothing or if occurring asynchronously a IPromise which will be resolved once the unload is complete, the IPromise will only be returned when no callback is provided and isAsync is true

  • Update the configuration used and broadcast the changes to all loaded plugins

    Type Parameters

    Parameters

    • newConfig: T

      The new configuration is apply

    • OptionalmergeExisting: boolean

      Should the new configuration merge with the existing or just replace it. Default is to merge.

    Returns void

  • Overwrite the lazy loaded fields of global window snippet to contain the actual initialized API methods

    Parameters

    • snippet: Snippet

      The global snippet

    Returns void