Skip to main content
Version: v1

@fluidframework/telemetry-utils Package

Interfaces

InterfaceDescription
IConfigProviderExplicitly typed interface for reading configurations
IConfigProviderBaseBase interface for providing configurations to enable/disable/control features
IFluidErrorAnnotationsMetadata to annotate an error object when annotating or normalizing it
IFluidErrorBaseAll normalized errors flowing through the Fluid Framework adhere to this readonly interface. It features errorType and errorInstanceId on top of Error's members as readonly, and a getter/setter for telemetry props to be included when the error is logged.
IPerformanceEventMarkersDescribes what events PerformanceEvent should log By default, all events are logged, but client can override this behavior For example, there is rarely a need to record start event, as we really after success / failure tracking, including duration (on success).
ITelemetryLoggerPropertyBag
ITelemetryLoggerPropertyBags
MonitoringContextA type containing both a telemetry logger and a configuration provider

Classes

ClassAlertsDescription
ChildLoggerChildLogger class contains various helper telemetry methods, encoding in one place schemas for various types of Fluid telemetry events. Creates sub-logger that appends properties to all events
DebugLoggerImplementation of debug logger
EventEmitterWithErrorHandlingEvent Emitter helper class Any exceptions thrown by listeners will be caught and raised through "error" event. Any exception thrown by "error" listeners will propagate to the caller.
LoggingError

Base class for "trusted" errors we create, whose properties can generally be logged to telemetry safely. All properties set on the object, or passed in (via the constructor or addTelemetryProperties), will be logged in accordance with their tag, if present.

PLEASE take care to avoid setting sensitive data on this object without proper tagging!

MockLoggerThe MockLogger records events sent to it, and then can walk back over those events searching for a set of expected events to match against the logged events.
MultiSinkLoggerMulti-sink logger Takes multiple ITelemetryBaseLogger objects (sinks) and logs all events into each sink Implements ITelemetryBaseLogger (through static create() method)
PerformanceEventHelper class to log performance events
SampledTelemetryHelperHelper class that executes a specified code block and writes an @fluidframework/common-definitions#ITelemetryPerformanceEvent to a specified logger every time a specified number of executions is reached (or when the class is disposed). The duration field in the telemetry event is the duration of the latest execution (sample) of the specified function. See the documentation of the includeAggregateMetrics parameter for additional details that can be included.
TaggedLoggerAdapterDeprecated
TelemetryLoggerTelemetryLogger class contains various helper telemetry methods, encoding in one place schemas for various types of Fluid telemetry events. Creates sub-logger that appends properties to all events
TelemetryUTLoggerLogger that is useful for UT It can be used in places where logger instance is required, but events should be not send over.
ThresholdCounterUtility counter which will send event only if the provided value is above a configured threshold

Enumerations

EnumDescription
TelemetryDataTagBroad classifications to be applied to individual properties as they're prepared to be logged to telemetry. Please do not modify existing entries for backwards compatibility.

Types

TypeAliasDescription
ConfigTypes
TelemetryEventPropertyTypes

Functions

FunctionReturn TypeDescription
extractLogSafeErrorProperties(error, sanitizeStack){ message: string; errorType?: string | undefined; stack?: string | undefined; }Inspect the given error for common "safe" props and return them
generateErrorWithStack()ErrorThe purpose of this function is to provide ability to capture stack context quickly. Accessing new Error().stack is slow, and the slowest part is accessing stack property itself. There are scenarios where we generate error with stack, but error is handled in most cases and stack property is not accessed. For such cases it's better to not read stack property right away, but rather delay it until / if it's needed Some browsers will populate stack right away, others require throwing Error, so we do auto-detection on the fly.
generateStack()string | undefined
isExternalError(e)booleanTrue for any error object that is an (optionally normalized) external error False for any error we created and raised within the FF codebase, or wrapped in a well-known error type
isFluidError(e)e is IFluidErrorBasetype guard for IFluidErrorBase interface
isTaggedTelemetryPropertyValue(x)x is ITaggedTelemetryPropertyTypeType guard to identify if a particular value (loosely) appears to be a tagged telemetry property
isValidLegacyError(e)e is Omit<IFluidErrorBase, "errorInstanceId">type guard for old standard of valid/known errors
loggerToMonitoringContext(logger)MonitoringContext<L>
logIfFalse(condition, logger, event)condition is trueLike assert, but logs only if the condition is false, rather than throwing
mixinMonitoringContext(logger, configs)MonitoringContext<L>
normalizeError(error, annotations)IFluidErrorBaseNormalize the given error yielding a valid Fluid Error
raiseConnectedEvent(logger, emitter, connected, clientId)void
safeRaiseEvent(emitter, logger, event, args)void
wrapError(innerError, newErrorFn)TCreate a new error using newErrorFn, wrapping and caused by the given unknown error. Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present
wrapErrorAndLog(innerError, newErrorFn, logger)TThe same as wrapError, but also logs the innerError, including the wrapping error's instance id

Variables

VariableTypeDescription
connectedEventName
disconnectedEventName
getCircularReplacer() => (key: string, value: any) => anyBorrowed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples Avoids runtime errors with circular references. Not ideal, as will cut values that are not necessarily circular references. Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)
hasErrorInstanceId(x: any) => x is { errorInstanceId: string; }
isILoggingError(x: any) => x is ILoggingErrortype guard for ILoggingError interface
sessionStorageConfigProviderLazy<IConfigProviderBase>Creates a base configuration provider based on sessionStorage

Function Details

extractLogSafeErrorProperties

Inspect the given error for common "safe" props and return them

Signature

export declare function extractLogSafeErrorProperties(error: any, sanitizeStack: boolean): {
message: string;
errorType?: string | undefined;
stack?: string | undefined;
};

Parameters

ParameterTypeDescription
errorany
sanitizeStackboolean

Returns

Return type: { message: string; errorType?: string | undefined; stack?: string | undefined; }

generateErrorWithStack

The purpose of this function is to provide ability to capture stack context quickly. Accessing new Error().stack is slow, and the slowest part is accessing stack property itself. There are scenarios where we generate error with stack, but error is handled in most cases and stack property is not accessed. For such cases it's better to not read stack property right away, but rather delay it until / if it's needed Some browsers will populate stack right away, others require throwing Error, so we do auto-detection on the fly.

Signature

export declare function generateErrorWithStack(): Error;

Returns

Error object that has stack populated.

Return type: Error

generateStack

Signature

export declare function generateStack(): string | undefined;

Returns

Return type: string | undefined

isExternalError

True for any error object that is an (optionally normalized) external error False for any error we created and raised within the FF codebase, or wrapped in a well-known error type

Signature

export declare function isExternalError(e: any): boolean;

Parameters

ParameterTypeDescription
eany

Returns

Return type: boolean

isFluidError

type guard for IFluidErrorBase interface

Signature

export declare function isFluidError(e: any): e is IFluidErrorBase;

Parameters

ParameterTypeDescription
eany

Returns

Return type: e is IFluidErrorBase

isTaggedTelemetryPropertyValue

Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property

Signature

export declare function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType;

Parameters

ParameterTypeDescription
xany

Returns

Return type: x is ITaggedTelemetryPropertyType

isValidLegacyError

type guard for old standard of valid/known errors

Signature

export declare function isValidLegacyError(e: any): e is Omit<IFluidErrorBase, "errorInstanceId">;

Parameters

ParameterTypeDescription
eany

Returns

Return type: e is Omit<IFluidErrorBase, "errorInstanceId">

loggerToMonitoringContext

Signature

export declare function loggerToMonitoringContext<L extends ITelemetryBaseLogger = ITelemetryLogger>(logger: L): MonitoringContext<L>;
Type Parameters
ParameterConstraintDefaultDescription
LITelemetryBaseLoggerITelemetryLogger

Parameters

ParameterTypeDescription
loggerL

Returns

Return type: MonitoringContext<L>

logIfFalse

Like assert, but logs only if the condition is false, rather than throwing

Signature

export declare function logIfFalse(condition: any, logger: ITelemetryBaseLogger, event: string | ITelemetryGenericEvent): condition is true;

Parameters

ParameterTypeDescription
conditionanyThe condition to attest too
loggerITelemetryBaseLoggerThe logger to log with
eventstring | ITelemetryGenericEventThe string or event to log

Returns

  • The outcome of the condition

Return type: condition is true

mixinMonitoringContext

Signature

export declare function mixinMonitoringContext<L extends ITelemetryBaseLogger = ITelemetryLogger>(logger: L, ...configs: (IConfigProviderBase | undefined)[]): MonitoringContext<L>;
Type Parameters
ParameterConstraintDefaultDescription
LITelemetryBaseLoggerITelemetryLogger

Parameters

ParameterTypeDescription
loggerL
configs(IConfigProviderBase | undefined)[]

Returns

Return type: MonitoringContext<L>

normalizeError

Normalize the given error yielding a valid Fluid Error

Signature

export declare function normalizeError(error: unknown, annotations?: IFluidErrorAnnotations): IFluidErrorBase;

Parameters

ParameterModifiersTypeDescription
errorunknownThe error to normalize
annotationsoptionalIFluidErrorAnnotationsAnnotations to apply to the normalized error

Returns

A valid Fluid Error with any provided annotations applied

Return type: IFluidErrorBase

raiseConnectedEvent

Signature

export declare function raiseConnectedEvent(logger: ITelemetryLogger, emitter: EventEmitter, connected: boolean, clientId?: string): void;

Parameters

ParameterModifiersTypeDescription
loggerITelemetryLogger
emitterEventEmitter
connectedboolean
clientIdoptionalstring

safeRaiseEvent

Signature

export declare function safeRaiseEvent(emitter: EventEmitter, logger: ITelemetryLogger, event: string, ...args: any[]): void;

Parameters

ParameterTypeDescription
emitterEventEmitter
loggerITelemetryLogger
eventstring
argsany[]

wrapError

Create a new error using newErrorFn, wrapping and caused by the given unknown error. Copies the inner error's stack, errorInstanceId and telemetry props over to the new error if present

Signature

export declare function wrapError<T extends LoggingError>(innerError: unknown, newErrorFn: (message: string) => T): T;
Type Parameters
ParameterConstraintDescription
TLoggingError

Parameters

ParameterTypeDescription
innerErrorunknownAn error from untrusted/unknown origins
newErrorFn(message: string) => Tcallback that will create a new error given the original error's message

Returns

A new error object "wrapping" the given error

Return type: T

wrapErrorAndLog

The same as wrapError, but also logs the innerError, including the wrapping error's instance id

Signature

export declare function wrapErrorAndLog<T extends LoggingError>(innerError: unknown, newErrorFn: (message: string) => T, logger: ITelemetryLogger): T;
Type Parameters
ParameterConstraintDescription
TLoggingError

Parameters

ParameterTypeDescription
innerErrorunknown
newErrorFn(message: string) => T
loggerITelemetryLogger

Returns

Return type: T

Variable Details

connectedEventName

Signature

connectedEventName = "connected"

disconnectedEventName

Signature

disconnectedEventName = "disconnected"

getCircularReplacer

Borrowed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples Avoids runtime errors with circular references. Not ideal, as will cut values that are not necessarily circular references. Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)

Signature

getCircularReplacer: () => (key: string, value: any) => any

Type: () => (key: string, value: any) => any

hasErrorInstanceId

Signature

hasErrorInstanceId: (x: any) => x is {
errorInstanceId: string;
}

Type: (x: any) => x is { errorInstanceId: string; }

isILoggingError

type guard for ILoggingError interface

Signature

isILoggingError: (x: any) => x is ILoggingError

Type: (x: any) => x is ILoggingError

sessionStorageConfigProvider

Creates a base configuration provider based on sessionStorage

Signature

sessionStorageConfigProvider: Lazy<IConfigProviderBase>

Type: Lazy<IConfigProviderBase>