{"version":3,"file":"AttributesProcessor.cjs","names":[],"sources":["../../src/view/AttributesProcessor.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Context, Attributes } from '@opentelemetry/api';\n\n/**\n * The {@link AttributesProcessor} is responsible for customizing which\n * attribute(s) are to be reported as metrics dimension(s) and adding\n * additional dimension(s) from the {@link Context}.\n */\nexport interface IAttributesProcessor {\n  /**\n   * Process the metric instrument attributes.\n   *\n   * @param incoming The metric instrument attributes.\n   * @param context The active context when the instrument is synchronous.\n   * `undefined` otherwise.\n   */\n  process: (incoming: Attributes, context?: Context) => Attributes;\n}\n\nclass NoopAttributesProcessor implements IAttributesProcessor {\n  process(incoming: Attributes, _context?: Context) {\n    return incoming;\n  }\n}\n\nclass MultiAttributesProcessor implements IAttributesProcessor {\n  private readonly _processors: IAttributesProcessor[];\n  constructor(processors: IAttributesProcessor[]) {\n    this._processors = processors;\n  }\n  process(incoming: Attributes, context?: Context): Attributes {\n    let filteredAttributes = incoming;\n    for (const processor of this._processors) {\n      filteredAttributes = processor.process(filteredAttributes, context);\n    }\n    return filteredAttributes;\n  }\n}\n\nclass AllowListProcessor implements IAttributesProcessor {\n  private readonly _allowedAttributeNames: Set<string>;\n  constructor(allowedAttributeNames: string[]) {\n    this._allowedAttributeNames = new Set(allowedAttributeNames);\n  }\n\n  process(incoming: Attributes, _context?: Context): Attributes {\n    const filteredAttributes: Attributes = {};\n    for (const attributeName in incoming) {\n      if (\n        Object.prototype.hasOwnProperty.call(incoming, attributeName) &&\n        this._allowedAttributeNames.has(attributeName)\n      ) {\n        filteredAttributes[attributeName] = incoming[attributeName];\n      }\n    }\n    return filteredAttributes;\n  }\n}\n\nclass DenyListProcessor implements IAttributesProcessor {\n  private readonly _deniedAttributeNames: Set<string>;\n  constructor(deniedAttributeNames: string[]) {\n    this._deniedAttributeNames = new Set(deniedAttributeNames);\n  }\n\n  process(incoming: Attributes, _context?: Context): Attributes {\n    const filteredAttributes: Attributes = {};\n    for (const attributeName in incoming) {\n      if (\n        Object.prototype.hasOwnProperty.call(incoming, attributeName) &&\n        !this._deniedAttributeNames.has(attributeName)\n      ) {\n        filteredAttributes[attributeName] = incoming[attributeName];\n      }\n    }\n    return filteredAttributes;\n  }\n}\n\n/**\n * @internal\n *\n * Create an {@link IAttributesProcessor} that acts as a simple pass-through for attributes.\n */\nexport function createNoopAttributesProcessor(): IAttributesProcessor {\n  return NOOP;\n}\n\n/**\n * @internal\n *\n * Create an {@link IAttributesProcessor} that applies all processors from the provided list in order.\n *\n * @param processors Processors to apply in order.\n */\nexport function createMultiAttributesProcessor(\n  processors: IAttributesProcessor[]\n): IAttributesProcessor {\n  return new MultiAttributesProcessor(processors);\n}\n\n/**\n * Create an {@link IAttributesProcessor} that filters by allowed attribute names and drops any names that are not in the\n * allow list.\n */\nexport function createAllowListAttributesProcessor(\n  attributeAllowList: string[]\n): IAttributesProcessor {\n  return new AllowListProcessor(attributeAllowList);\n}\n\n/**\n * Create an {@link IAttributesProcessor} that drops attributes based on the names provided in the deny list\n */\nexport function createDenyListAttributesProcessor(\n  attributeDenyList: string[]\n): IAttributesProcessor {\n  return new DenyListProcessor(attributeDenyList);\n}\n\nconst NOOP = new NoopAttributesProcessor();\n"],"mappings":";AAuBA,IAAM,0BAAN,MAA8D;CAC5D,QAAQ,UAAsB,UAAoB;EAChD,OAAO;CACT;AACF;AAEA,IAAM,2BAAN,MAA+D;CAC7D;CACA,YAAY,YAAoC;EAC9C,KAAK,cAAc;CACrB;CACA,QAAQ,UAAsB,SAA+B;EAC3D,IAAI,qBAAqB;EACzB,KAAK,MAAM,aAAa,KAAK,aAC3B,qBAAqB,UAAU,QAAQ,oBAAoB,OAAO;EAEpE,OAAO;CACT;AACF;AAEA,IAAM,qBAAN,MAAyD;CACvD;CACA,YAAY,uBAAiC;EAC3C,KAAK,yBAAyB,IAAI,IAAI,qBAAqB;CAC7D;CAEA,QAAQ,UAAsB,UAAgC;EAC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