{"version":3,"file":"LogRecordImpl.cjs","names":["timeInputToHrTime","api","maybeAddAttribute","AddAttributeDecision","ATTR_EXCEPTION_MESSAGE","ATTR_EXCEPTION_TYPE","ATTR_EXCEPTION_STACKTRACE"],"sources":["../src/LogRecordImpl.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { LogRecord, SeverityNumber } from '@opentelemetry/api';\nimport * as api from '@opentelemetry/api';\nimport type { InstrumentationScope } from '@opentelemetry/core';\nimport {\n  AddAttributeDecision,\n  maybeAddAttribute,\n  timeInputToHrTime,\n} from '@opentelemetry/core';\nimport type { Resource } from '@opentelemetry/resources';\nimport {\n  ATTR_EXCEPTION_MESSAGE,\n  ATTR_EXCEPTION_STACKTRACE,\n  ATTR_EXCEPTION_TYPE,\n} from '@opentelemetry/semantic-conventions';\nimport type { ReadableLogRecord } from './export/ReadableLogRecord';\nimport type { LogRecordLimits } from './types';\nimport type { LoggerProviderSharedState } from './internal/LoggerProviderSharedState';\n\nexport class LogRecordImpl implements ReadableLogRecord {\n  readonly resource: Resource;\n  readonly instrumentationScope: InstrumentationScope & {\n    attributes?: api.Attributes;\n    droppedAttributesCount?: number;\n  };\n  readonly attributes: api.Attributes = {};\n  private _hrTime: api.HrTime;\n  private _hrTimeObserved: api.HrTime;\n  private _spanContext?: api.SpanContext;\n  private _severityText?: string;\n  private _severityNumber?: SeverityNumber;\n  private _body?: api.AnyValue;\n  private _eventName?: string;\n  private _attributesCount: number = 0;\n  private _droppedAttributesCount: number = 0;\n\n  private _isReadonly: boolean = false;\n  private readonly _logRecordLimits: Required<LogRecordLimits>;\n\n  get hrTime(): api.HrTime {\n    return this._hrTime;\n  }\n  set hrTime(hrTime: api.HrTime) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._hrTime = hrTime;\n  }\n\n  get hrTimeObserved(): api.HrTime {\n    return this._hrTimeObserved;\n  }\n  set hrTimeObserved(hrTimeObserved: api.HrTime) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._hrTimeObserved = hrTimeObserved;\n  }\n\n  get spanContext(): api.SpanContext | undefined {\n    return this._spanContext;\n  }\n  set spanContext(spanContext: api.SpanContext | undefined) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._spanContext = spanContext;\n  }\n\n  set severityText(severityText: string | undefined) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._severityText = severityText;\n  }\n  get severityText(): string | undefined {\n    return this._severityText;\n  }\n\n  set severityNumber(severityNumber: SeverityNumber | undefined) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._severityNumber = severityNumber;\n  }\n  get severityNumber(): SeverityNumber | undefined {\n    return this._severityNumber;\n  }\n\n  set body(body: api.AnyValue | undefined) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._body = body;\n  }\n  get body(): api.AnyValue | undefined {\n    return this._body;\n  }\n\n  get eventName(): string | undefined {\n    return this._eventName;\n  }\n  set eventName(eventName: string | undefined) {\n    if (this._isLogRecordReadonly()) {\n      return;\n    }\n    this._eventName = eventName;\n  }\n\n  get droppedAttributesCount(): number {\n    return this._droppedAttributesCount;\n  }\n\n  constructor(\n    _sharedState: LoggerProviderSharedState,\n    instrumentationScope: InstrumentationScope,\n    logRecord: LogRecord\n  ) {\n    const {\n      timestamp,\n      observedTimestamp,\n      eventName,\n      severityNumber,\n      severityText,\n      body,\n      attributes = {},\n      exception,\n      context,\n    } = logRecord;\n\n    const now = Date.now();\n    this._hrTime = timeInputToHrTime(timestamp ?? now);\n    this._hrTimeObserved = timeInputToHrTime(observedTimestamp ?? now);\n\n    if (context) {\n      const spanContext = api.trace.getSpanContext(context);\n      if (spanContext && api.isSpanContextValid(spanContext)) {\n        this._spanContext = spanContext;\n      }\n    }\n    this.severityNumber = severityNumber;\n    this.severityText = severityText;\n    this.body = body;\n    this.resource = _sharedState.resource;\n    this.instrumentationScope = instrumentationScope;\n    this._logRecordLimits = _sharedState.logRecordLimits;\n    this._eventName = eventName;\n    this.setAttributes(attributes);\n    if (exception != null) {\n      this._setException(exception);\n    }\n  }\n\n  public setAttribute(key: string, value?: api.AnyValue) {\n    if (this._isLogRecordReadonly()) {\n      return this;\n    }\n\n    const decision = maybeAddAttribute({\n      key,\n      value,\n      attributes: this.attributes,\n      limits: this._logRecordLimits,\n      currentAttributesCount: this._attributesCount,\n    });\n\n    if (decision === AddAttributeDecision.DROP_LIMIT_REACHED) {\n      this._droppedAttributesCount++;\n      if (this._droppedAttributesCount === 1) {\n        // Only warn once per LogRecord to avoid log spam\n        api.diag.warn('Dropping extra attributes.');\n      }\n    } else if (decision === AddAttributeDecision.ADD_NEW) {\n      this._attributesCount++;\n    }\n\n    return this;\n  }\n\n  public setAttributes(attributes: api.Attributes) {\n    for (const [k, v] of Object.entries(attributes)) {\n      this.setAttribute(k, v);\n    }\n    return this;\n  }\n\n  public setBody(body: api.AnyValue) {\n    this.body = body;\n    return this;\n  }\n\n  public setEventName(eventName: string) {\n    this.eventName = eventName;\n    return this;\n  }\n\n  public setSeverityNumber(severityNumber: SeverityNumber) {\n    this.severityNumber = severityNumber;\n    return this;\n  }\n\n  public setSeverityText(severityText: string) {\n    this.severityText = severityText;\n    return this;\n  }\n\n  /**\n   * @internal\n   * A LogRecordProcessor may freely modify logRecord for the duration of the OnEmit call.\n   * If logRecord is needed after OnEmit returns (i.e. for asynchronous processing) only reads are permitted.\n   */\n  _makeReadonly() {\n    this._isReadonly = true;\n  }\n\n  private _setException(exception: unknown): void {\n    let hasMinimumAttributes = false;\n\n    if (typeof exception === 'string' || typeof exception === 'number') {\n      if (!Object.hasOwn(this.attributes, ATTR_EXCEPTION_MESSAGE)) {\n        this.setAttribute(ATTR_EXCEPTION_MESSAGE, String(exception));\n      }\n      hasMinimumAttributes = true;\n    } else if (exception && typeof exception === 'object') {\n      const exceptionObj = exception as {\n        code?: string | number;\n        name?: string;\n        message?: string;\n        stack?: string;\n      };\n\n      if (exceptionObj.code) {\n        if (!Object.hasOwn(this.attributes, ATTR_EXCEPTION_TYPE)) {\n          this.setAttribute(ATTR_EXCEPTION_TYPE, exceptionObj.code.toString());\n        }\n        hasMinimumAttributes = true;\n      } else if (exceptionObj.name) {\n        if (!Object.hasOwn(this.attributes, ATTR_EXCEPTION_TYPE)) {\n          this.setAttribute(ATTR_EXCEPTION_TYPE, exceptionObj.name);\n        }\n        hasMinimumAttributes = true;\n      }\n\n      if (exceptionObj.message) {\n        if (!Object.hasOwn(this.attributes, ATTR_EXCEPTION_MESSAGE)) {\n          this.setAttribute(ATTR_EXCEPTION_MESSAGE, exceptionObj.message);\n        }\n        hasMinimumAttributes = true;\n      }\n\n      if (exceptionObj.stack) {\n        if (!Object.hasOwn(this.attributes, ATTR_EXCEPTION_STACKTRACE)) {\n          this.setAttribute(ATTR_EXCEPTION_STACKTRACE, exceptionObj.stack);\n        }\n        hasMinimumAttributes = true;\n      }\n    }\n\n    if (!hasMinimumAttributes) {\n      api.diag.warn(`Failed to record an exception ${exception}`);\n    }\n  }\n\n  private _isLogRecordReadonly(): boolean {\n    if (this._isReadonly) {\n      api.diag.warn('Can not execute the operation on emitted log record');\n    }\n    return this._isReadonly;\n  }\n}\n"],"mappings":";;;;;;AAuBA,IAAa,gBAAb,MAAwD;CACtD;CACA;CAIA,aAAsC,CAAC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA,mBAAmC;CACnC,0BAA0C;CAE1C,cAA+B;CAC/B;CAEA,IAAI,SAAqB;EACvB,OAAO,KAAK;CACd;CACA,IAAI,OAAO,QAAoB;EAC7B,IAAI,KAAK,qBAAqB,GAC5B;EAEF,KAAK,UAAU;CACjB;CAEA,IAAI,iBAA6B;EAC/B,OAAO,KAAK;CACd;CACA,IAAI,eAAe,gBAA4B;EAC7C,IAAI,KAAK,qBAAqB,GAC5B;EAEF,KAAK,kBAAkB;CACzB;CAEA,IAAI,cAA2C;EAC7C,OAAO,KAAK;CACd;CACA,IAAI,YAAY,aAA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