/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import type { IDisposable, ITelemetryBaseProperties } from "@fluidframework/core-interfaces"; import type { ITelemetryGenericEventExt, TelemetryLoggerExt } from "./telemetryTypesUndeprecated.js"; /** * Helper type for an object whose properties are all numbers * * @internal */ export type CustomMetrics = { [K in keyof TKey]: K extends string ? number : never; }; /** * Potentially part of the structure of the return value of the function provided to {@link SampledTelemetryHelper.measure}. * * @see {@link MeasureReturnType} for more details on how this type is used. * * @internal */ export interface ICustomData { customData: CustomMetrics; } /** * Encapsulates the type-level logic for {@link SampledTelemetryHelper.measure}, to determine the expected return type * for the function that method receives (and by extension, its own return type). In words: {@link SampledTelemetryHelper} * is optionally provided with two generic types: one for custom metrics, and one for the actual return value of the * code that will be measured. * * - If no generic type is provided for custom metrics, then this type is simply the generic type provided for the actual * return value of the measured code (which could be void!). * - If a generic type is provided for custom metrics, then this type has a `customData` property whose type matches that * generic. Then if the generic type for the actual return value is not void, this type also has a property `returnValue` * whose type matches the generic type for the actual return value; if the generic type for the actual return value is * void, then this type _forbids_ a `returnValue` property (technically, it can exist but must be undefined in that case), * to try to ensure that the caller doesn't accidentally provide a function that actually returns a value. * * @internal */ export type MeasureReturnType = TCustomMetrics extends void ? TMeasureReturn : ICustomData & (TMeasureReturn extends void ? Partial> : { returnValue: TMeasureReturn; }); /** * Helper class that executes a specified code block and writes an * {@link @fluidframework/core-interfaces#ITelemetryPerformanceEvent} to a specified logger every time a specified * number of executions is reached (or when the class is disposed). * * @remarks * The `duration` field in the telemetry event this class generates is the duration of the latest execution (sample) * of the specified code block. * See the documentation of the `includeAggregateMetrics` parameter for additional details that can be included. * * Telemetry events emitted by this class (both at the sample threshold and on dispose) are sent with * {@link @fluidframework/core-interfaces#LogLevelConst.info | LogLevel.info}. * * @typeParam TMeasurementReturn - The return type (in a vacuum) of the code block that will be measured, ignoring * any custom metric data that might be required by this class. E.g., the code might just return a boolean. * @typeParam TCustomMetrics - A type that contains the custom properties that will be used by an instance of this class * for custom metrics. Each property in this type should be a number. * * @internal */ export declare class SampledTelemetryHelper = void> implements IDisposable { private readonly eventBase; private readonly logger; private readonly sampleThreshold; private readonly includeAggregateMetrics; private readonly perBucketProperties; private _disposed; /** * {@inheritDoc @fluidframework/core-interfaces#IDisposable.disposed} */ get disposed(): boolean; private readonly measurementsMap; /** * @param eventBase - Custom properties to include in the telemetry performance event when it is written. * @param logger - The logger to use to write the telemetry performance event. * @param sampleThreshold - Telemetry performance events will be generated every time we hit this many executions * of the code block. * @param includeAggregateMetrics - If set to `true`, the telemetry performance event will include aggregated * metrics (total duration, min duration, max duration) for all the executions in between generated events. * @param perBucketProperties - Map of strings that represent different buckets (which can be specified when calling * the 'measure' method), to properties which should be added to the telemetry event for that bucket. * If a bucket being measured does not have an entry in this map, no additional properties will be added to its * telemetry events. The following keys are reserved for use by this class: "duration", "count", "totalDuration", * "minDuration", "maxDuration". If any of them is specified as a key in one of the ITelemetryBaseProperties objects * in this map, that key-value pair will be ignored. */ constructor(eventBase: ITelemetryGenericEventExt, logger: TelemetryLoggerExt, sampleThreshold: number, includeAggregateMetrics?: boolean, perBucketProperties?: Map); /** * Executes the specified code and keeps track of execution time statistics. * When it's been called enough times (the sampleThreshold for the class) then it generates a log message with the * necessary information. * * @remarks It's the responsibility of the caller to ensure that the same same set of custom metric properties is * provided each time this method is called on a given instance of {@link SampledTelemetryHelper}. * Otherwise the final measurements in the telemetry event may not be accurate. * * @param codeToMeasure - The code to be executed and measured. * @param bucket - A key to track executions of the code block separately. * Each different value of this parameter has a separate set of executions and metrics tracked by the class. * If no such distinction needs to be made, do not provide a value. * @returns Whatever the passed-in code block returns. */ measure(codeToMeasure: () => MeasureReturnType, bucket?: string): MeasureReturnType; private isCustomData; private accumulateCustomData; private processCustomData; private flushBucket; dispose(error?: Error | undefined): void; } //# sourceMappingURL=sampledTelemetryHelper.d.ts.map