apmz

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2019 License: MIT Imports: 23 Imported by: 4

Documentation

Overview

Package apmz provides an interface to submit telemetry to Application Insights. See more at https://azure.microsoft.com/en-us/services/application-insights/

Index

Constants

View Source
const (
	Measurement = contracts.Measurement
	Aggregation = contracts.Aggregation
)

Type of the metric data measurement.

View Source
const (
	Verbose     = contracts.Verbose
	Information = contracts.Information
	Warning     = contracts.Warning
	Error       = contracts.Error
	Critical    = contracts.Critical
)

Defines the level of severity for the event.

Variables

This section is empty.

Functions

func GetCallstack

func GetCallstack(skip int) []*contracts.StackFrame

GetCallstack generates a callstack suitable for inclusion in Application Insights exception telemetry for the current goroutine, skipping a number of frames specified by skip.

func TrackPanic

func TrackPanic(client TelemetryClient, rethrow bool)

TrackPanic recovers from any active panics and tracks them to the specified TelemetryClient. If rethrow is set to true, then this will panic. Should be invoked via defer in functions to monitor.

Types

type AggregateMetricTelemetry

type AggregateMetricTelemetry struct {
	BaseTelemetry
	BaseTelemetryNoMeasurements

	// Metric name
	Name string

	// Sum of individual measurements
	Value float64

	// Minimum value of the aggregated metric
	Min float64

	// Maximum value of the aggregated metric
	Max float64

	// Count of measurements in the sample
	Count int

	// Standard deviation of the aggregated metric
	StdDev float64

	// Variance of the aggregated metric.  As an invariant,
	// either this or the StdDev should be zero at any given time.
	// If both are non-zero then StdDev takes precedence.
	Variance float64
}

AggregateMetricTelemetry items represent an aggregation of data points over time. These values can be calculated by the caller or with the AddData function.

func NewAggregateMetricTelemetry

func NewAggregateMetricTelemetry(name string) *AggregateMetricTelemetry

NewAggregateMetricTelemetry creates a new aggregated metric telemetry item with the specified name. Values should be set on the object returned before submission.

func (*AggregateMetricTelemetry) AddData

func (agg *AggregateMetricTelemetry) AddData(values []float64)

AddData adds data points to the aggregate totals included in this telemetry item. This can be used for all the data at once or incrementally. Calculates Min, Max, Sum, Count, and StdDev (by way of Variance).

func (*AggregateMetricTelemetry) AddSampledData

func (agg *AggregateMetricTelemetry) AddSampledData(values []float64)

AddSampledData adds sampled data points to the aggregate totals included in this telemetry item. This can be used for all the data at once or incrementally. Differs from AddData in how it calculates standard deviation, and should not be used interchangeably with AddData.

func (*AggregateMetricTelemetry) TelemetryData

func (agg *AggregateMetricTelemetry) TelemetryData() TelemetryData

TelemetryData gets TelemetryData for an AggregateMetricTelemetry

type AvailabilityTelemetry

type AvailabilityTelemetry struct {
	BaseTelemetry
	BaseTelemetryMeasurements

	// Identifier of a test run. Used to correlate steps of test run and
	// telemetry generated by the service.
	ID string

	// Name of the test that this result represents.
	Name string

	// Duration of the test run.
	Duration time.Duration

	// Success flag.
	Success bool

	// Name of the location where the test was run.
	RunLocation string

	// Diagnostic message for the result.
	Message string
}

AvailabilityTelemetry items represent the result of executing an availability test.

func NewAvailabilityTelemetry

func NewAvailabilityTelemetry(name string, duration time.Duration, success bool) *AvailabilityTelemetry

NewAvailabilityTelemetry creates a new availability telemetry item with the specified test name, duration and success code.

func (*AvailabilityTelemetry) MarkTime

func (at *AvailabilityTelemetry) MarkTime(startTime, endTime time.Time)

MarkTime sets the timestamp and duration of this telemetry item based on the provided start and end times.

func (*AvailabilityTelemetry) TelemetryData

func (at *AvailabilityTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for an AvailabilityTelemetry

type BaseTelemetry

type BaseTelemetry struct {
	// The time this when this item was measured
	Timestamp time.Time

	// Custom properties
	Properties map[string]string

	// Telemetry Context containing extra, optional tags.
	Tags contracts.ContextTags
}

BaseTelemetry is the common base struct for telemetry items.

func (*BaseTelemetry) ContextTags

func (item *BaseTelemetry) ContextTags() map[string]string

ContextTags gets context data containing extra, optional tags. Overrides values found on client TelemetryContext.

func (*BaseTelemetry) GetProperties

func (item *BaseTelemetry) GetProperties() map[string]string

GetProperties gets custom properties to submit with the telemetry item.

func (*BaseTelemetry) SetTime

func (item *BaseTelemetry) SetTime(t time.Time)

SetTime sets the timestamp to the specified time.

func (*BaseTelemetry) Time

func (item *BaseTelemetry) Time() time.Time

Time returns the timestamp when this was measured.

type BaseTelemetryMeasurements

type BaseTelemetryMeasurements struct {
	// Custom measurements
	Measurements map[string]float64
}

BaseTelemetryMeasurements provides the Measurements field for telemetry items that support it.

func (*BaseTelemetryMeasurements) GetMeasurements

func (item *BaseTelemetryMeasurements) GetMeasurements() map[string]float64

GetMeasurements gets custom measurements to submit with the telemetry item.

type BaseTelemetryNoMeasurements

type BaseTelemetryNoMeasurements struct {
}

BaseTelemetryNoMeasurements provides no Measurements field for telemetry items that omit it.

func (*BaseTelemetryNoMeasurements) GetMeasurements

func (item *BaseTelemetryNoMeasurements) GetMeasurements() map[string]float64

GetMeasurements returns nil for telemetry items that do not support measurements.

type DiagnosticsMessageHandler

type DiagnosticsMessageHandler func(string) error

DiagnosticsMessageHandler is a handler function for receiving diagnostics messages. If this returns an error, then the listener will be removed.

type DiagnosticsMessageListener

type DiagnosticsMessageListener interface {
	// Stop receiving diagnostics messages from this listener.
	Remove()
}

DiagnosticsMessageListener is a listener type returned by NewDiagnosticsMessageListener.

func NewDiagnosticsMessageListener

func NewDiagnosticsMessageListener(handler DiagnosticsMessageHandler) DiagnosticsMessageListener

NewDiagnosticsMessageListener subscribes the specified handler to diagnostics messages from the SDK. The returned interface can be used to unsubscribe.

type EventTelemetry

type EventTelemetry struct {
	BaseTelemetry
	BaseTelemetryMeasurements

	// Event name
	Name string
}

EventTelemetry items represent structured event records.

func NewEventTelemetry

func NewEventTelemetry(name string) *EventTelemetry

NewEventTelemetry creates an event telemetry item with the specified name.

func (*EventTelemetry) TelemetryData

func (event *EventTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for an EventTelemetry

type ExceptionTelemetry

type ExceptionTelemetry struct {
	BaseTelemetry
	BaseTelemetryMeasurements

	// Panic message: string, error, or Stringer
	Error interface{}

	// List of stack frames. Use GetCallstack to generate this data.
	Frames []*contracts.StackFrame

	// Severity level.
	SeverityLevel contracts.SeverityLevel
}

ExceptionTelemetry items represent an exception that has occurred during execution of the monitored application.

func NewExceptionTelemetry

func NewExceptionTelemetry(err interface{}) *ExceptionTelemetry

NewExceptionTelemetry creates a new exception telemetry item with the specified error and the current callstack. This should be used directly from a function that handles a recover(), or to report an unexpected error return value from a function.

func (*ExceptionTelemetry) TelemetryData

func (telem *ExceptionTelemetry) TelemetryData() TelemetryData

TelemetryData returns a new TelemetryData structure

type InMemoryChannel

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

InMemoryChannel is a telemetry channel that stores events exclusively in memory. Presently the only telemetry channel implementation available.

func NewInMemoryChannel

func NewInMemoryChannel(config *TelemetryConfiguration) *InMemoryChannel

NewInMemoryChannel creates an InMemoryChannel instance and starts a background submission goroutine.

func (*InMemoryChannel) Close

func (channel *InMemoryChannel) Close(timeout ...time.Duration) <-chan struct{}

Close flushes and tears down the submission goroutine and closes internal channels. Returns a channel that is closed when all pending telemetry items have been submitted and it is safe to shut down without losing telemetry.

If retryTimeout is specified and non-zero, then failed submissions will be retried until one succeeds or the timeout expires, whichever occurs first. A retryTimeout of zero indicates that failed submissions will be retried as usual. An omitted retryTimeout indicates that submissions should not be retried if they fail.

Note that the returned channel may not be closed before retryTimeout even if it is specified. This is because retryTimeout only applies to the latest telemetry buffer. This may be typical for applications that submit a large amount of telemetry or are prone to being throttled. When exiting, you should select on the result channel and your own timer to avoid long delays.

func (*InMemoryChannel) EndpointAddress

func (channel *InMemoryChannel) EndpointAddress() string

EndpointAddress is the address of the endpoint to which telemetry is sent

func (*InMemoryChannel) Flush

func (channel *InMemoryChannel) Flush()

Flush forces the current queue to be sent

func (*InMemoryChannel) IsThrottled

func (channel *InMemoryChannel) IsThrottled() bool

IsThrottled returns true if this channel has been throttled by the data collector.

func (*InMemoryChannel) Send

func (channel *InMemoryChannel) Send(item *contracts.Envelope)

Send queues a single telemetry item

func (*InMemoryChannel) Stop

func (channel *InMemoryChannel) Stop()

Stop tears down the submission goroutines, closes internal channels. Any telemetry waiting to be sent is discarded. Further calls to Send() have undefined behavior. This is a more abrupt version of Close().

type MetricTelemetry

type MetricTelemetry struct {
	BaseTelemetry
	BaseTelemetryNoMeasurements

	// Metric name
	Name string

	// Sampled value
	Value float64
}

MetricTelemetry items each represent a single data point.

func NewMetricTelemetry

func NewMetricTelemetry(name string, value float64) *MetricTelemetry

NewMetricTelemetry creates a metric telemetry sample with the specified name and value.

func (*MetricTelemetry) TelemetryData

func (metric *MetricTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for a MetricTelemetry

type PageViewTelemetry

type PageViewTelemetry struct {
	BaseTelemetry
	BaseTelemetryMeasurements

	// Request URL with all query string parameters
	URL string

	// Request duration.
	Duration time.Duration

	// Event name.
	Name string
}

PageViewTelemetry items represent generic actions on a page like a button click.

func NewPageViewTelemetry

func NewPageViewTelemetry(name, url string) *PageViewTelemetry

NewPageViewTelemetry creates a new page view telemetry item with the specified name and url.

func (*PageViewTelemetry) MarkTime

func (pvt *PageViewTelemetry) MarkTime(startTime, endTime time.Time)

MarkTime sets the timestamp and duration of this telemetry item based on the provided start and end times.

func (*PageViewTelemetry) TelemetryData

func (pvt *PageViewTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for a PageViewTelemetry

type RemoteDependencyTelemetry

type RemoteDependencyTelemetry struct {
	BaseTelemetry
	BaseTelemetryMeasurements

	// Name of the command that initiated this dependency call. Low cardinality
	// value. Examples are stored procedure name and URL path template.
	Name string

	// Identifier of a dependency call instance. Used for correlation with the
	// request telemetry item corresponding to this dependency call.
	ID string

	// Result code of a dependency call. Examples are SQL error code and HTTP
	// status code.
	ResultCode string

	// Duration of the remote call.
	Duration time.Duration

	// Indication of successful or unsuccessful call.
	Success bool

	// Command initiated by this dependency call. Examples are SQL statement and
	// HTTP URL's with all the query parameters.
	Data string

	// Dependency type name. Very low cardinality. Examples are SQL, Azure table,
	// and HTTP.
	Type string

	// Target site of a dependency call. Examples are server name, host address.
	Target string
}

RemoteDependencyTelemetry items represent interactions of the monitored component with a remote component/service like SQL or an HTTP endpoint.

func NewRemoteDependencyTelemetry

func NewRemoteDependencyTelemetry(name, dependencyType, target string, success bool) *RemoteDependencyTelemetry

NewRemoteDependencyTelemetry creates a new Remote Dependency telemetry item, with the specified name, dependency type, target site, and success status.

func (*RemoteDependencyTelemetry) MarkTime

func (rdt *RemoteDependencyTelemetry) MarkTime(startTime, endTime time.Time)

MarkTime sets the timestamp and duration of this telemetry item based on the provided start and end times.

func (*RemoteDependencyTelemetry) TelemetryData

func (rdt *RemoteDependencyTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for a RemoteDependencyTelemetry

type RequestTelemetry

type RequestTelemetry struct {
	BaseTelemetry
	BaseTelemetryMeasurements

	// Identifier of a request call instance. Used for correlation between request
	// and other telemetry items.
	ID string

	// Request name. For HTTP requests it represents the HTTP method and URL path template.
	Name string

	// URL of the request with all query string parameters.
	URL string

	// Duration to serve the request.
	Duration time.Duration

	// Results of a request execution. HTTP status code for HTTP requests.
	ResponseCode string

	// Indication of successful or unsuccessful call.
	Success bool

	// Source of the request. Examplese are the instrumentation key of the caller
	// or the ip address of the caller.
	Source string
}

RequestTelemetry items represents completion of an external request to the application and contains a summary of that request execution and results.

func NewRequestTelemetry

func NewRequestTelemetry(method, uri string, duration time.Duration, responseCode string) *RequestTelemetry

NewRequestTelemetry creates a new request telemetry item for HTTP requests. The success value will be computed from responseCode, and the timestamp will be set to the current time minus the duration.

func (*RequestTelemetry) MarkTime

func (request *RequestTelemetry) MarkTime(startTime, endTime time.Time)

MarkTime sets the timestamp and duration of this telemetry item based on the provided start and end times.

func (*RequestTelemetry) TelemetryData

func (request *RequestTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for a RequestTelemetry

type Telemetry

type Telemetry interface {
	// Gets the time when this item was measured
	Time() time.Time

	// Sets the timestamp to the specified time.
	SetTime(time.Time)

	// Gets context data containing extra, optional tags.  Overrides
	// values found on client TelemetryContext.
	ContextTags() map[string]string

	// Gets the data contract as it will be submitted to the data
	// collector.
	TelemetryData() TelemetryData

	// Gets custom properties to submit with the telemetry item.
	GetProperties() map[string]string

	// Gets custom measurements to submit with the telemetry item.
	GetMeasurements() map[string]float64
}

Telemetry is a common interface implemented by telemetry items that can be passed to TelemetryClient.Track

type TelemetryChannel

type TelemetryChannel interface {
	// EndpointAddress is the address of the endpoint to which telemetry is sent
	EndpointAddress() string

	// Send queues a single telemetry item
	Send(*contracts.Envelope)

	// Flush forces the current queue to be sent
	Flush()

	// Stop tears down the submission goroutines, closes internal channels.
	// Any telemetry waiting to be sent is discarded.  Further calls to
	// Send() have undefined behavior.  This is a more abrupt version of
	// Close().
	Stop()

	// IsThrottled returns true if this channel has been throttled by the data
	// collector.
	IsThrottled() bool

	// Close flushes and tears down the submission goroutine and closes
	// internal channels.  Returns a channel that is closed when all
	// pending telemetry items have been submitted and it is safe to
	// shut down without losing telemetry.
	//
	// If retryTimeout is specified and non-zero, then failed
	// submissions will be retried until one succeeds or the timeout
	// expires, whichever occurs first.  A retryTimeout of zero
	// indicates that failed submissions will be retried as usual.  An
	// omitted retryTimeout indicates that submissions should not be
	// retried if they fail.
	//
	// Note that the returned channel may not be closed before
	// retryTimeout even if it is specified.  This is because
	// retryTimeout only applies to the latest telemetry buffer.  This
	// may be typical for applications that submit a large amount of
	// telemetry or are prone to being throttled.  When exiting, you
	// should select on the result channel and your own timer to avoid
	// long delays.
	Close(retryTimeout ...time.Duration) <-chan struct{}
}

TelemetryChannel is responsible for queueing and periodically submitting telemetry items.

type TelemetryClient

type TelemetryClient interface {
	// Gets the telemetry context for this client. Values found on this
	// context will get written out to every telemetry item tracked by
	// this client.
	Context() *TelemetryContext

	// Gets the instrumentation key assigned to this telemetry client.
	InstrumentationKey() string

	// Channel gets the telemetry channel used to submit data to the backend.
	Channel() TelemetryChannel

	// SetChannel will set the telemetry channel used to submit data to the backend.
	SetChannel(channel TelemetryChannel)

	// Gets whether this client is enabled and will accept telemetry.
	IsEnabled() bool

	// Enables or disables the telemetry client. When disabled, telemetry
	// is silently swallowed by the client. Defaults to enabled.
	SetIsEnabled(enabled bool)

	// Submits the specified telemetry item.
	Track(telemetry Telemetry)

	// Log a user action with the specified name
	TrackEvent(name string)

	// Log a numeric value that is not specified with a specific event.
	// Typically used to send regular reports of performance indicators.
	TrackMetric(name string, value float64)

	// Log a trace message with the specified severity level.
	TrackTrace(name string, severity contracts.SeverityLevel)

	// Log an HTTP request with the specified method, URL, duration and
	// response code.
	TrackRequest(method, url string, duration time.Duration, responseCode string)

	// Log a dependency with the specified name, type, target, and
	// success status.
	TrackRemoteDependency(name, dependencyType, target string, success bool)

	// Log an availability test result with the specified test name,
	// duration, and success status.
	TrackAvailability(name string, duration time.Duration, success bool)

	// Log an exception with the specified error, which may be a string,
	// error or Stringer. The current callstack is collected
	// automatically.
	TrackException(err interface{})
}

TelemetryClient is the Application Insights telemetry client provides interface to track telemetry items.

func NewTelemetryClient

func NewTelemetryClient(iKey string) TelemetryClient

NewTelemetryClient creates a new telemetry client instance that submits telemetry with the specified instrumentation key.

func NewTelemetryClientFromConfig

func NewTelemetryClientFromConfig(config *TelemetryConfiguration) TelemetryClient

NewTelemetryClientFromConfig creates a new telemetry client instance configured by the specified TelemetryConfiguration object.

type TelemetryConfiguration

type TelemetryConfiguration struct {
	// Instrumentation key for the client.
	InstrumentationKey string

	// Endpoint URL where data will be submitted.
	EndpointURL 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.
	MaxBatchSize int

	// Maximum time to wait before sending a batch of telemetry.
	MaxBatchInterval time.Duration
}

TelemetryConfiguration is configuration data used to initialize a new TelemetryClient.

func NewTelemetryConfiguration

func NewTelemetryConfiguration(instrumentationKey string) *TelemetryConfiguration

NewTelemetryConfiguration creates a new TelemetryConfiguration object with the specified instrumentation key and default values.

type TelemetryContext

type TelemetryContext struct {

	// Tags is a collection of tag data to attach to the telemetry item.
	Tags contracts.ContextTags

	// CommonProperties are common properties to add to each telemetry item.  This only has
	// an effect from the TelemetryClient's context instance.  This will
	// be nil on telemetry items.
	CommonProperties map[string]string
	// contains filtered or unexported fields
}

TelemetryContext encapsulates contextual data common to all telemetry submitted through a TelemetryClient instance such as including instrumentation key, tags, and common properties.

func NewTelemetryContext

func NewTelemetryContext(ikey string) *TelemetryContext

NewTelemetryContext creates a new, empty TelemetryContext

func (*TelemetryContext) InstrumentationKey

func (context *TelemetryContext) InstrumentationKey() string

InstrumentationKey gets the instrumentation key associated with this TelemetryContext. This will be an empty string on telemetry items' context instances.

type TelemetryData

type TelemetryData interface {
	EnvelopeName(string) string
	BaseType() string
	Sanitize() []string
}

TelemetryData is aommon interface implemented by telemetry data contracts

type TraceTelemetry

type TraceTelemetry struct {
	BaseTelemetry
	BaseTelemetryNoMeasurements

	// Trace message
	Message string

	// Severity level
	SeverityLevel contracts.SeverityLevel
}

TraceTelemetry items represent printf-like trace statements that can be text searched.

func NewTraceTelemetry

func NewTraceTelemetry(message string, severityLevel contracts.SeverityLevel) *TraceTelemetry

NewTraceTelemetry creates a trace telemetry item with the specified message and severity level.

func (*TraceTelemetry) TelemetryData

func (trace *TraceTelemetry) TelemetryData() TelemetryData

TelemetryData gets the TelemetryData for a TraceTelemetry

Directories

Path Synopsis
Data contract definitions for telemetry submitted to Application Insights.
Data contract definitions for telemetry submitted to Application Insights.

Jump to

Keyboard shortcuts

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