{"version":3,"file":"SimpleLogRecordProcessor.cjs","names":["BindOnceFuture","createNoopMeter","LogRecordProcessorMetrics","OTEL_COMPONENT_TYPE_VALUE_SIMPLE_LOG_PROCESSOR","internal","ExportResultCode","globalErrorHandler"],"sources":["../../src/export/SimpleLogRecordProcessor.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { createNoopMeter } from '@opentelemetry/api';\nimport type { ExportResult } from '@opentelemetry/core';\nimport {\n  BindOnceFuture,\n  ExportResultCode,\n  globalErrorHandler,\n  internal,\n} from '@opentelemetry/core';\nimport type { LogRecordExporter } from './LogRecordExporter';\nimport type { LogRecordProcessor } from '../LogRecordProcessor';\nimport type { ReadWriteLogRecord } from './ReadWriteLogRecord';\nimport { OTEL_COMPONENT_TYPE_VALUE_SIMPLE_LOG_PROCESSOR } from '../semconv';\nimport { LogRecordProcessorMetrics } from './LogRecordProcessorMetrics';\nimport type { Context } from '@opentelemetry/api';\nimport type { SimpleLogRecordProcessorOptions } from '../types';\n\n/**\n * An implementation of the {@link LogRecordProcessor} interface that exports\n * each {@link LogRecord} as it is emitted.\n *\n * NOTE: This {@link LogRecordProcessor} exports every {@link LogRecord}\n * individually instead of batching them together, which can cause significant\n * performance overhead with most exporters. For production use, please consider\n * using the {@link BatchLogRecordProcessor} instead.\n */\nexport class SimpleLogRecordProcessor implements LogRecordProcessor {\n  private readonly _exporter: LogRecordExporter;\n  private readonly _metrics: LogRecordProcessorMetrics;\n  private _shutdownOnce: BindOnceFuture<void>;\n  private _unresolvedExports: Set<Promise<void>>;\n\n  constructor(options: SimpleLogRecordProcessorOptions) {\n    this._exporter = options.exporter;\n    this._shutdownOnce = new BindOnceFuture(this._shutdown, this);\n    this._unresolvedExports = new Set<Promise<void>>();\n\n    const meter = options?.selfObsMeterProvider\n      ? options.selfObsMeterProvider.getMeter('@opentelemetry/sdk-logs')\n      : createNoopMeter();\n    this._metrics = new LogRecordProcessorMetrics(\n      OTEL_COMPONENT_TYPE_VALUE_SIMPLE_LOG_PROCESSOR,\n      meter\n    );\n  }\n\n  public onEmit(logRecord: ReadWriteLogRecord, _context?: Context): void {\n    if (this._shutdownOnce.isCalled) {\n      return;\n    }\n\n    const doExport = () =>\n      internal\n        ._export(this._exporter, [logRecord])\n        .then((result: ExportResult) => {\n          this._metrics.finishLogs(1, result.error);\n          if (result.code !== ExportResultCode.SUCCESS) {\n            globalErrorHandler(\n              result.error ??\n                new Error(\n                  `SimpleLogRecordProcessor: log record export failed (status ${result})`\n                )\n            );\n          }\n        })\n        .catch(globalErrorHandler);\n\n    // Avoid scheduling a promise to make the behavior more predictable and easier to test\n    if (logRecord.resource.asyncAttributesPending) {\n      const exportPromise = logRecord.resource\n        .waitForAsyncAttributes?.()\n        .then(() => {\n          // Using TS Non-null assertion operator because exportPromise could not be null in here\n          // if waitForAsyncAttributes is not present this code will never be reached\n          this._unresolvedExports.delete(exportPromise!);\n          return doExport();\n        }, globalErrorHandler);\n\n      // store the unresolved exports\n      if (exportPromise != null) {\n        this._unresolvedExports.add(exportPromise);\n      }\n    } else {\n      void doExport();\n    }\n  }\n\n  public async forceFlush(): Promise<void> {\n    // await unresolved resources before resolving\n    await Promise.all(Array.from(this._unresolvedExports));\n  }\n\n  public shutdown(): Promise<void> {\n    return this._shutdownOnce.call();\n  }\n\n  private _shutdown(): Promise<void> {\n    this._metrics.shutdown();\n    return this._exporter.shutdown();\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AA8BA,IAAa,2BAAb,MAAoE;CAClE;CACA;CACA;CACA;CAEA,YAAY,SAA0C;EACpD,KAAK,YAAY,QAAQ;EACzB,KAAK,gBAAgB,IAAIA,oBAAAA,eAAe,KAAK,WAAW,IAAI;EAC5D,KAAK,qCAAqB,IAAI,IAAmB;EAEjD,MAAM,QAAQ,SAAS,uBACnB,QAAQ,qBAAqB,SAAS,yBAAyB,KAAA,GAC/DC,mBAAAA,gBAAAA,CAAgB;EACpB,KAAK,W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