- 
                Notifications
    You must be signed in to change notification settings 
- Fork 351
Add support for OpenTelemetry Logs API #6465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 65 commits
      Commits
    
    
            Show all changes
          
          
            66 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      343c772
              
                first attempt at otel logs support
              
              
                mabdinur b3a2973
              
                lint files and centralize configs
              
              
                mabdinur 2c0d528
              
                add tests
              
              
                mabdinur 6c359ce
              
                clean up yarn.lock
              
              
                mabdinur 7dc1480
              
                clean up logs
              
              
                mabdinur 2c60dfc
              
                add back log exporter logic
              
              
                mabdinur dabe98f
              
                clean up docs
              
              
                mabdinur b7ae33d
              
                clean up docs and fix otlp protocol
              
              
                mabdinur f73d425
              
                revert readme docs
              
              
                mabdinur 543805a
              
                add new otel files
              
              
                mabdinur c63e20e
              
                working version
              
              
                mabdinur d4d8aac
              
                fmy
              
              
                mabdinur 9514e68
              
                fmt
              
              
                mabdinur df7cbca
              
                Update packages/dd-trace/src/config_defaults.js
              
              
                mabdinur 2c0a5a5
              
                fix tests
              
              
                mabdinur f109cf3
              
                allow any version of logs api, let opentelemetry api determine the ve…
              
              
                mabdinur 0c29478
              
                add otlp payload tests
              
              
                mabdinur 8b1ce46
              
                add telemetry metrics
              
              
                mabdinur 432ec89
              
                some other clean ups
              
              
                mabdinur 036776a
              
                simplify tests
              
              
                mabdinur 8abbe95
              
                use agent hostname to resolve otlp endpoints
              
              
                mabdinur e9456e3
              
                clean up initalization
              
              
                mabdinur 9164c6f
              
                Merge branch 'master' into munir/otlp-logs-support
              
              
                mabdinur 4bc66fc
              
                parse additional otlp headers
              
              
                mabdinur d505cab
              
                clean up component args
              
              
                mabdinur 0f2bd21
              
                clean up docs
              
              
                mabdinur 3af5080
              
                clean up component args
              
              
                mabdinur 9578887
              
                remove addLogProcessor, init provider with a processor
              
              
                mabdinur c7c15d8
              
                support trace-log correlation
              
              
                mabdinur f6f58fe
              
                clean up registering provider in tests, and rename exporter arg
              
              
                mabdinur 434fbc4
              
                first round of clean ups from PR review
              
              
                mabdinur 3963ed6
              
                clean ups part 2
              
              
                mabdinur 6d10314
              
                make things private and clean up tests
              
              
                mabdinur 11161f3
              
                clean up tests
              
              
                mabdinur 266c936
              
                clean up yarn file
              
              
                mabdinur d63a4ed
              
                remove unused configs
              
              
                mabdinur b98cd76
              
                fix context issues
              
              
                mabdinur 9955d5e
              
                nother round of clean ups
              
              
                mabdinur a9ddbcc
              
                group payloads by instrumentation scope
              
              
                mabdinur d9d9dfb
              
                fix typing
              
              
                mabdinur 351ca2f
              
                address review comments
              
              
                mabdinur b99ba1d
              
                add better typing, and better support for sending schemaurl
              
              
                mabdinur 07ec605
              
                Merge branch 'master' into munir/otlp-logs-support
              
              
                mabdinur a1f8cf7
              
                revert instrumentationScope change to span
              
              
                mabdinur 09fedbc
              
                Merge branch 'master' into munir/otlp-logs-support
              
              
                mabdinur 9ff13fd
              
                review comments
              
              
                mabdinur 0b8e71d
              
                clean up protobuf loader file
              
              
                mabdinur 46d1385
              
                Merge branch 'master' into munir/otlp-logs-support
              
              
                mabdinur 5410486
              
                lint
              
              
                mabdinur 55e86e9
              
                move protos to same dir, this will set up metrics work
              
              
                mabdinur 311500c
              
                clean up throws
              
              
                mabdinur 07ab61c
              
                update protos
              
              
                mabdinur f25c7a5
              
                disable log injection when otel logs support is enabled
              
              
                mabdinur 9f716d1
              
                update configurations to pass telemetry system tests
              
              
                mabdinur ea532c2
              
                remove useless import
              
              
                mabdinur e695249
              
                add more tests
              
              
                mabdinur 557b6d5
              
                provide fix for failing system test
              
              
                mabdinur 266a239
              
                add test case for noop logger and fix mocking for remote config
              
              
                mabdinur df5d6b4
              
                add test case for noop logger and fix mocking for remote config
              
              
                mabdinur 2763296
              
                Merge branch 'master' into munir/otlp-logs-support
              
              
                mabdinur e995e0c
              
                fix encoding for doubles, remove unused shutdown code, test getLogger…
              
              
                mabdinur e20668e
              
                remove unused timer and shutdown logic, simplify loggerprovider regis…
              
              
                mabdinur 3f8eab1
              
                fix comment
              
              
                mabdinur 148d47b
              
                clean up how otel endpoint configs are loaded
              
              
                mabdinur 9ac86bb
              
                address Ayans comments
              
              
                mabdinur 7f4fbff
              
                add integration tests
              
              
                mabdinur File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -67,6 +67,8 @@ const VALID_PROPAGATION_BEHAVIOR_EXTRACT = new Set(['continue', 'restart', 'igno | |
|  | ||
| const VALID_LOG_LEVELS = new Set(['debug', 'info', 'warn', 'error']) | ||
|  | ||
| const DEFAULT_OTLP_PORT = 4318 | ||
|  | ||
| function getFromOtelSamplerMap (otelTracesSampler, otelTracesSamplerArg) { | ||
| const OTEL_TRACES_SAMPLER_MAPPING = { | ||
| always_on: '1.0', | ||
|  | @@ -554,6 +556,7 @@ class Config { | |
| DD_INSTRUMENTATION_TELEMETRY_ENABLED, | ||
| DD_INSTRUMENTATION_CONFIG_ID, | ||
| DD_LOGS_INJECTION, | ||
| DD_LOGS_OTEL_ENABLED, | ||
| DD_LANGCHAIN_SPAN_CHAR_LIMIT, | ||
| DD_LANGCHAIN_SPAN_PROMPT_COMPLETION_SAMPLE_RATE, | ||
| DD_LLMOBS_AGENTLESS_ENABLED, | ||
|  | @@ -635,7 +638,17 @@ class Config { | |
| OTEL_RESOURCE_ATTRIBUTES, | ||
| OTEL_SERVICE_NAME, | ||
| OTEL_TRACES_SAMPLER, | ||
| OTEL_TRACES_SAMPLER_ARG | ||
| OTEL_TRACES_SAMPLER_ARG, | ||
|         
                  mabdinur marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| OTEL_EXPORTER_OTLP_LOGS_ENDPOINT, | ||
| OTEL_EXPORTER_OTLP_LOGS_HEADERS, | ||
| OTEL_EXPORTER_OTLP_LOGS_PROTOCOL, | ||
| OTEL_EXPORTER_OTLP_LOGS_TIMEOUT, | ||
| OTEL_EXPORTER_OTLP_PROTOCOL, | ||
| OTEL_EXPORTER_OTLP_ENDPOINT, | ||
| OTEL_EXPORTER_OTLP_HEADERS, | ||
| OTEL_EXPORTER_OTLP_TIMEOUT, | ||
| OTEL_BSP_SCHEDULE_DELAY, | ||
| OTEL_BSP_MAX_EXPORT_BATCH_SIZE | ||
| } = getEnvironmentVariables() | ||
|  | ||
| const tags = {} | ||
|  | @@ -649,6 +662,23 @@ class Config { | |
| tagger.add(tags, DD_TRACE_TAGS) | ||
| tagger.add(tags, DD_TRACE_GLOBAL_TAGS) | ||
|  | ||
| this._setBoolean(env, 'otelLogsEnabled', isTrue(DD_LOGS_OTEL_ENABLED)) | ||
| // Set OpenTelemetry logs configuration with specific _LOGS_ vars taking precedence over generic _EXPORTERS_ vars | ||
| if (OTEL_EXPORTER_OTLP_ENDPOINT) { | ||
| // Only set if there's a custom URL, otherwise let calc phase handle the default | ||
| this._setString(env, 'otelUrl', OTEL_EXPORTER_OTLP_ENDPOINT) | ||
| } | ||
| if (OTEL_EXPORTER_OTLP_ENDPOINT || OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) { | ||
| this._setString(env, 'otelLogsUrl', OTEL_EXPORTER_OTLP_LOGS_ENDPOINT || env.otelUrl) | ||
| } | ||
| this._setString(env, 'otelHeaders', OTEL_EXPORTER_OTLP_HEADERS) | ||
| this._setString(env, 'otelLogsHeaders', OTEL_EXPORTER_OTLP_LOGS_HEADERS || env.otelHeaders) | ||
| this._setString(env, 'otelProtocol', OTEL_EXPORTER_OTLP_PROTOCOL) | ||
|         
                  BridgeAR marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| this._setString(env, 'otelLogsProtocol', OTEL_EXPORTER_OTLP_LOGS_PROTOCOL || env.otelProtocol) | ||
| env.otelTimeout = maybeInt(OTEL_EXPORTER_OTLP_TIMEOUT) | ||
| env.otelLogsTimeout = maybeInt(OTEL_EXPORTER_OTLP_LOGS_TIMEOUT) || env.otelTimeout | ||
| env.otelLogsBatchTimeout = maybeInt(OTEL_BSP_SCHEDULE_DELAY) | ||
| env.otelLogsMaxExportBatchSize = maybeInt(OTEL_BSP_MAX_EXPORT_BATCH_SIZE) | ||
| this._setBoolean( | ||
| env, | ||
| 'apmTracingEnabled', | ||
|  | @@ -1154,7 +1184,20 @@ class Config { | |
| calc.testManagementAttemptToFixRetries = maybeInt(DD_TEST_MANAGEMENT_ATTEMPT_TO_FIX_RETRIES) ?? 20 | ||
| this._setBoolean(calc, 'isImpactedTestsEnabled', !isFalse(DD_CIVISIBILITY_IMPACTED_TESTS_DETECTION_ENABLED)) | ||
| } | ||
|  | ||
| // Disable log injection when OTEL logs are enabled | ||
| // OTEL logs and DD log injection are mutually exclusive | ||
| if (this._env.otelLogsEnabled) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a unit test for this scenario? | ||
| this._setBoolean(calc, 'logInjection', false) | ||
| } | ||
|  | ||
| calc['dogstatsd.hostname'] = this._getHostname() | ||
|  | ||
| // Compute OTLP logs URL to send payloads to the active Datadog Agent | ||
| const agentHostname = this._getHostname() | ||
| calc.otelLogsUrl = `http://${agentHostname}:${DEFAULT_OTLP_PORT}` | ||
| calc.otelUrl = `http://${agentHostname}:${DEFAULT_OTLP_PORT}` | ||
|  | ||
| this._setBoolean(calc, 'isGitUploadEnabled', | ||
| calc.isIntelligentTestRunnerEnabled && !isFalse(this._isCiVisibilityGitUploadEnabled())) | ||
| this._setBoolean(calc, 'spanComputePeerService', this._getSpanComputePeerService()) | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            100 changes: 100 additions & 0 deletions
          
          100 
        
  packages/dd-trace/src/opentelemetry/logs/batch_log_processor.js
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| 'use strict' | ||
|  | ||
| /** | ||
| * @typedef {import('@opentelemetry/api-logs').LogRecord} LogRecord | ||
| * @typedef {import('@opentelemetry/core').InstrumentationScope} InstrumentationScope | ||
| */ | ||
|  | ||
| /** | ||
| * BatchLogRecordProcessor processes log records in batches for efficient export to Datadog Agent. | ||
| * | ||
| * This implementation follows the OpenTelemetry JavaScript SDK BatchLogRecordProcessor: | ||
| * https://open-telemetry.github.io/opentelemetry-js/classes/_opentelemetry_sdk-logs.BatchLogRecordProcessor.html | ||
|         
                  mabdinur marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| * | ||
| * @class BatchLogRecordProcessor | ||
| */ | ||
| class BatchLogRecordProcessor { | ||
| #logRecords | ||
| #timer | ||
| #batchTimeout | ||
| #maxExportBatchSize | ||
|  | ||
| /** | ||
| * Creates a new BatchLogRecordProcessor instance. | ||
| * | ||
| * @param {OtlpHttpLogExporter} exporter - Log processor for exporting batches to Datadog Agent | ||
| * @param {number} batchTimeout - Timeout in milliseconds for batch processing | ||
| * @param {number} maxExportBatchSize - Maximum number of log records per batch | ||
| */ | ||
| constructor (exporter, batchTimeout, maxExportBatchSize) { | ||
| this.exporter = exporter | ||
| this.#batchTimeout = batchTimeout | ||
| this.#maxExportBatchSize = maxExportBatchSize | ||
| this.#logRecords = [] | ||
| this.#timer = null | ||
| } | ||
|  | ||
| /** | ||
| * Processes a single log record. | ||
| * | ||
| * @param {LogRecord} logRecord - The enriched log record with trace correlation and metadata | ||
| * @param {InstrumentationScope} instrumentationScope - The instrumentation library | ||
| */ | ||
| onEmit (logRecord, instrumentationScope) { | ||
| // Store the log record (already enriched by Logger.emit) | ||
| this.#logRecords.push({ ...logRecord, instrumentationScope }) | ||
|  | ||
| if (this.#logRecords.length >= this.#maxExportBatchSize) { | ||
| this.#export() | ||
| } else if (this.#logRecords.length === 1) { | ||
| this.#startTimer() | ||
| } | ||
| } | ||
|  | ||
| /** | ||
| * Forces an immediate flush of all pending log records. | ||
| * @returns {undefined} Promise that resolves when flush is complete | ||
| */ | ||
| forceFlush () { | ||
| this.#export() | ||
| } | ||
|  | ||
| /** | ||
| * Starts the batch timeout timer. | ||
| * @private | ||
| */ | ||
| #startTimer () { | ||
| if (this.#timer) { | ||
| return | ||
| } | ||
|  | ||
| this.#timer = setTimeout(() => { | ||
| this.#export() | ||
| }, this.#batchTimeout) | ||
| } | ||
|  | ||
| /** | ||
| * Exports the current batch of log records. | ||
| * @private | ||
| */ | ||
| #export () { | ||
| const logRecords = this.#logRecords.slice(0, this.#maxExportBatchSize) | ||
| this.#logRecords = this.#logRecords.slice(this.#maxExportBatchSize) | ||
|  | ||
| this.#clearTimer() | ||
| this.exporter.export(logRecords, () => {}) | ||
| } | ||
|  | ||
| /** | ||
| * Clears the batch timeout timer. | ||
| * @private | ||
| */ | ||
| #clearTimer () { | ||
| if (this.#timer) { | ||
| clearTimeout(this.#timer) | ||
| this.#timer = null | ||
| } | ||
| } | ||
| } | ||
|  | ||
| module.exports = BatchLogRecordProcessor | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use strict' | ||
|  | ||
| const os = require('os') | ||
|  | ||
| /** | ||
| * @typedef {import('../../config')} Config | ||
| */ | ||
|  | ||
| /** | ||
| * @fileoverview OpenTelemetry Logs Implementation for dd-trace-js | ||
| * | ||
| * This package provides a custom OpenTelemetry Logs implementation that integrates | ||
| * with the Datadog tracing library. It includes all necessary components for | ||
| * emitting, processing, and exporting log records via OTLP (OpenTelemetry Protocol). | ||
| * | ||
| * Key Components: | ||
| * - LoggerProvider: Main entry point for creating loggers | ||
| * - Logger: Provides methods to emit log records | ||
| * - BatchLogRecordProcessor: Processes log records in batches for efficient export | ||
| * - OtlpHttpLogExporter: Exports log records via OTLP over HTTP | ||
| * - OtlpTransformer: Transforms log records to OTLP format | ||
| * | ||
| * This is a custom implementation to avoid pulling in the full OpenTelemetry SDK, | ||
| * based on OTLP Protocol v1.7.0. It supports both protobuf and JSON serialization | ||
| * formats and integrates with Datadog's configuration system. | ||
| * | ||
| * @package | ||
| */ | ||
|  | ||
| const LoggerProvider = require('./logger_provider') | ||
| const BatchLogRecordProcessor = require('./batch_log_processor') | ||
| const OtlpHttpLogExporter = require('./otlp_http_log_exporter') | ||
|  | ||
| /** | ||
| * Initializes OpenTelemetry Logs support | ||
| * @param {Config} config - Tracer configuration instance | ||
| */ | ||
| function initializeOpenTelemetryLogs (config) { | ||
| // Build resource attributes | ||
| const resourceAttributes = { | ||
| 'service.name': config.service, | ||
| 'service.version': config.version, | ||
| 'deployment.environment': config.env | ||
| } | ||
|  | ||
| // Add all tracer tags (includes DD_TAGS, OTEL_RESOURCE_ATTRIBUTES, DD_TRACE_TAGS, etc.) | ||
| // Exclude Datadog-style keys that duplicate OpenTelemetry standard keys | ||
| if (config.tags) { | ||
| const filteredTags = { ...config.tags } | ||
| delete filteredTags.service | ||
| delete filteredTags.version | ||
| delete filteredTags.env | ||
| Object.assign(resourceAttributes, filteredTags) | ||
| } | ||
|  | ||
| // Add host.name if reportHostname is enabled | ||
| if (config.reportHostname) { | ||
| resourceAttributes['host.name'] = os.hostname() | ||
| } | ||
|  | ||
| // Create OTLP exporter using resolved config values | ||
| const exporter = new OtlpHttpLogExporter( | ||
| config.otelLogsUrl, | ||
| config.otelLogsHeaders, | ||
| config.otelLogsTimeout, | ||
| config.otelLogsProtocol, | ||
| resourceAttributes | ||
| ) | ||
|  | ||
| // Create batch processor for exporting logs to Datadog Agent | ||
| const processor = new BatchLogRecordProcessor( | ||
| exporter, | ||
| config.otelLogsBatchTimeout, | ||
| config.otelLogsMaxExportBatchSize | ||
| ) | ||
|  | ||
| // Create logger provider with processor for Datadog Agent export | ||
| const loggerProvider = new LoggerProvider({ processor }) | ||
|  | ||
| // Register the logger provider globally with OpenTelemetry API | ||
| loggerProvider.register() | ||
| } | ||
|  | ||
| module.exports = { | ||
| LoggerProvider, | ||
| initializeOpenTelemetryLogs | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.