github.com/ethereum-optimism/optimism@v1.7.2/packages/common-ts/src/base-service/metrics.ts (about)

     1  import {
     2    Gauge as PGauge,
     3    Counter as PCounter,
     4    Histogram as PHistogram,
     5    Summary as PSummary,
     6  } from 'prom-client'
     7  
     8  import { OptionsSpec, getPublicOptions } from './options'
     9  
    10  // Prometheus metrics re-exported.
    11  export class Gauge extends PGauge<string> {}
    12  export class Counter extends PCounter<string> {}
    13  export class Histogram extends PHistogram<string> {}
    14  export class Summary extends PSummary<string> {}
    15  export type Metric = Gauge | Counter | Histogram | Summary
    16  
    17  /**
    18   * Metrics that are available for a given service.
    19   */
    20  export type Metrics = Record<any, Metric>
    21  
    22  /**
    23   * Specification for metrics.
    24   */
    25  export type MetricsSpec<TMetrics extends Metrics> = {
    26    [P in keyof Required<TMetrics>]: {
    27      type: new (configuration: any) => TMetrics[P]
    28      desc: string
    29      labels?: string[]
    30    }
    31  }
    32  
    33  /**
    34   * Standard metrics that are always available.
    35   */
    36  export type StandardMetrics = {
    37    metadata: Gauge
    38    unhandledErrors: Counter
    39  }
    40  
    41  /**
    42   * Generates a standard metrics specification. Needs to be a function because the labels for
    43   * service metadata are dynamic dependent on the list of given options.
    44   *
    45   * @param options Options to include in the service metadata.
    46   * @returns Metrics specification.
    47   */
    48  export const makeStdMetricsSpec = (
    49    optionsSpec: OptionsSpec<any>
    50  ): MetricsSpec<StandardMetrics> => {
    51    return {
    52      // Users cannot set these options.
    53      metadata: {
    54        type: Gauge,
    55        desc: 'Service metadata',
    56        labels: ['name', 'version'].concat(getPublicOptions(optionsSpec)),
    57      },
    58      unhandledErrors: {
    59        type: Counter,
    60        desc: 'Unhandled errors',
    61      },
    62    }
    63  }