appinsights

package
v0.0.0-...-d1f9c14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 16 Imported by: 0

README

appinsights

This package is a trimmed down version of https://github.com/microsoft/ApplicationInsights-Go. It is tailored for the use of the Microsoft build of Go.

These are the changes made to the original package:

  • Remove all external dependencies.
  • Remove all telemetry types except for Event.
  • Simplify implementation.
  • Modernize the code.
  • Improve testing.

Documentation

Overview

Package appinsights provides an interface to submit telemetry to Application Insights, a component of Azure Monitor. This package calls the Classic API. See https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics

Index

Constants

View Source
const (
	Version = "v0.0.1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// The instrumentation key used to identify the application.
	// This key is required and must be set before sending any telemetry.
	InstrumentationKey string

	// The endpoint URL to which telemetry will be sent.
	// If empty, it defaults to https://dc.services.visualstudio.com/v2/track.
	Endpoint string

	// Maximum number of telemetry items that can be submitted in each
	// request. If this many items are buffered, the buffer will be
	// flushed before MaxBatchInterval expires.
	// If zero, it defaults to 1024.
	MaxBatchSize int

	// Maximum time to wait before sending a batch of telemetry.
	// If zero, it defaults to 10 seconds.
	MaxBatchInterval time.Duration

	// Customized http client.
	// If nil, it defaults to http.DefaultClient.
	HTTPClient *http.Client

	// Tags to be sent with every telemetry item.
	// If nil, no additional tags will be sent.
	Tags map[string]string

	// Logger specifies a structured logger.
	// If nil nothing is logged.
	Logger *slog.Logger

	// Function to filter out telemetry items by name before they are sent.
	// If nil, all telemetry items are sent.
	UploadFilter func(name string) bool
	// contains filtered or unexported fields
}

Client is the main entry point for sending telemetry to Application Insights. Changing its properties after a telemetry item is created will have no effect.

func (*Client) Close

func (c *Client) Close(ctx context.Context)

Close flushes and tears down the submission goroutine and closes internal channels. Waits until all pending telemetry items have been submitted.

func (*Client) Flush

func (c *Client) Flush()

Forces the current queue to be sent.

func (*Client) NewEvent

func (c *Client) NewEvent(name string, properties map[string]string) *Event

NewEvent creates a new event with the specified name. If c is nil, returns a usable Event that does not send any telemetry.

func (*Client) Stop

func (c *Client) Stop()

Stop tears down the submission goroutines, closes internal channels. Any telemetry waiting to be sent is discarded. This is a more abrupt version of Client.Close.

func (*Client) TrackEvent

func (c *Client) TrackEvent(name string, properties map[string]string)

TrackEvent logs a user action with the specified name. If c is nil, nothing is logged.

type Event

type Event struct {
	// contains filtered or unexported fields
}

Event represents an event to be tracked.

func (*Event) Add

func (e *Event) Add(n int64)

Add adds n to the counter. n cannot be negative, as counts cannot decrease.

func (*Event) Inc

func (e *Event) Inc()

Inc adds 1 to the counter.

Directories

Path Synopsis
internal
appinsightstest
This package contains test utilities for the Application Insights SDK.
This package contains test utilities for the Application Insights SDK.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL