|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { LogRecord, BufferConfig, BatchLogRecordProcessor } from '@opentelemetry/sdk-logs'; |
| 4 | +import { AnyValue } from '@opentelemetry/api-logs'; |
| 5 | +import { callWithTimeout } from '@opentelemetry/core'; |
| 6 | +import { OTLPAwsLogExporter } from './otlp-aws-log-exporter'; |
| 7 | + |
| 8 | +/* |
| 9 | + * OTel log events include fixed metadata attributes so the estimated metadata size |
| 10 | + * possibly be calculated as this with best efforts: |
| 11 | + * service.name (255 chars) + cloud.resource_id (max ARN length) + telemetry.xxx (~20 chars) + |
| 12 | + * common attributes (255 chars) + |
| 13 | + * scope + flags + traceId + spanId + numeric/timestamp fields + ... |
| 14 | + * Example log structure: |
| 15 | + * { |
| 16 | + * "resource": { |
| 17 | + * "attributes": { |
| 18 | + * "aws.local.service": "example-service123", |
| 19 | + * "telemetry.sdk.language": "python", |
| 20 | + * "service.name": "my-application", |
| 21 | + * "cloud.resource_id": "example-resource", |
| 22 | + * "aws.log.group.names": "example-log-group", |
| 23 | + * "aws.ai.agent.type": "default", |
| 24 | + * "telemetry.sdk.version": "1.x.x", |
| 25 | + * "telemetry.auto.version": "0.x.x", |
| 26 | + * "telemetry.sdk.name": "opentelemetry" |
| 27 | + * } |
| 28 | + * }, |
| 29 | + * "scope": {"name": "example.instrumentation.library"}, |
| 30 | + * "timeUnixNano": 1234567890123456789, |
| 31 | + * "observedTimeUnixNano": 1234567890987654321, |
| 32 | + * "severityNumber": 9, |
| 33 | + * "body": {...}, |
| 34 | + * "attributes": {...}, |
| 35 | + * "flags": 1, |
| 36 | + * "traceId": "abcd1234efgh5678ijkl9012mnop3456", |
| 37 | + * "spanId": "1234abcd5678efgh" |
| 38 | + * } |
| 39 | + * 2000 might be a bit of an overestimate but it's better to overestimate the size of the log |
| 40 | + * and suffer a small performance impact with batching than it is to underestimate and risk |
| 41 | + * a large log being dropped when sent to the AWS otlp endpoint. |
| 42 | + */ |
| 43 | +export const BASE_LOG_BUFFER_BYTE_SIZE: number = 2000; |
| 44 | + |
| 45 | +// Maximum uncompressed/unserialized bytes / request - |
| 46 | +// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html |
| 47 | +export const MAX_LOG_REQUEST_BYTE_SIZE: number = 1048576; |
| 48 | + |
| 49 | +/** |
| 50 | + * Custom implementation of BatchLogRecordProcessor that manages log record batching |
| 51 | + * with size-based constraints to prevent exceeding AWS CloudWatch Logs OTLP endpoint request size limits. |
| 52 | + * |
| 53 | + * This processor still exports all logs up to _MAX_LOG_REQUEST_BYTE_SIZE but rather than doing exactly |
| 54 | + * one export, we will estimate log sizes and do multiple batch exports |
| 55 | + * where each exported batch will have an additional constraint: |
| 56 | + * |
| 57 | + * If the batch to be exported will have a data size of > 1 MB: |
| 58 | + * The batch will be split into multiple exports of sub-batches of data size <= 1 MB. |
| 59 | + * |
| 60 | + * A unique case is if the sub-batch is of data size > 1 MB, then the sub-batch will have exactly 1 log in it. |
| 61 | + * |
| 62 | + */ |
| 63 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 64 | +// @ts-ignore |
| 65 | +export class AwsCloudWatchOtlpBatchLogRecordProcessor extends BatchLogRecordProcessor { |
| 66 | + constructor(exporter: OTLPAwsLogExporter, config?: BufferConfig) { |
| 67 | + super(exporter, config); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Explicitly overrides upstream _flushOneBatch method to add AWS CloudWatch size-based batching. |
| 72 | + * Returns a list of promise export requests where each promise will be estimated to be at or under |
| 73 | + * the 1 MB limit for CloudWatch Logs OTLP endpoint. |
| 74 | + * |
| 75 | + * Estimated data size of exported batches will typically be <= 1 MB except for the case below: |
| 76 | + * If the estimated data size of an exported batch is ever > 1 MB then the batch size is guaranteed to be 1 |
| 77 | + */ |
| 78 | + override _flushOneBatch(): Promise<void> { |
| 79 | + this['_clearTimer'](); |
| 80 | + |
| 81 | + if (this['_finishedLogRecords'].length === 0) { |
| 82 | + return Promise.resolve(); |
| 83 | + } |
| 84 | + |
| 85 | + const logsToExport: LogRecord[] = this['_finishedLogRecords'].splice(0, this['_maxExportBatchSize']); |
| 86 | + let batch: LogRecord[] = []; |
| 87 | + let batchDataSize = 0; |
| 88 | + const exportPromises: Promise<void>[] = []; |
| 89 | + |
| 90 | + for (const logData of logsToExport) { |
| 91 | + const logSize = AwsCloudWatchOtlpBatchLogRecordProcessor.estimateLogSize(logData); |
| 92 | + |
| 93 | + if (batch.length > 0 && batchDataSize + logSize > MAX_LOG_REQUEST_BYTE_SIZE) { |
| 94 | + exportPromises.push(callWithTimeout(this['_export'](batch), this['_exportTimeoutMillis'])); |
| 95 | + batchDataSize = 0; |
| 96 | + batch = []; |
| 97 | + } |
| 98 | + |
| 99 | + batchDataSize += logSize; |
| 100 | + batch.push(logData); |
| 101 | + } |
| 102 | + |
| 103 | + if (batch.length > 0) { |
| 104 | + exportPromises.push(callWithTimeout(this['_export'](batch), this['_exportTimeoutMillis'])); |
| 105 | + } |
| 106 | + // Explicitly returns Promise<void> because of upstream's method signature for this function |
| 107 | + return Promise.all(exportPromises) |
| 108 | + .then(() => {}) |
| 109 | + .catch(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Estimates the size in bytes of a log by calculating the size of its body and its attributes |
| 114 | + * and adding a buffer amount to account for other log metadata information. |
| 115 | + * Will process complex log structures up to the specified depth limit. |
| 116 | + * Includes cycle detection to prevent processing the log content more than once. |
| 117 | + * If the depth limit of the log structure is exceeded, returns the truncated calculation |
| 118 | + * to everything up to that point. |
| 119 | + * |
| 120 | + * We set depth to 3 as this is the minimum required depth to estimate our consolidated Gen AI log events: |
| 121 | + * |
| 122 | + * Example structure: |
| 123 | + * { |
| 124 | + * "output": { |
| 125 | + * "messages": [ |
| 126 | + * { |
| 127 | + * "content": "Hello, World!", |
| 128 | + * "role": "assistant" |
| 129 | + * } |
| 130 | + * ] |
| 131 | + * }, |
| 132 | + * "input": { |
| 133 | + * "messages": [ |
| 134 | + * { |
| 135 | + * "content": "Say Hello, World!", |
| 136 | + * "role": "user" |
| 137 | + * } |
| 138 | + * ] |
| 139 | + * } |
| 140 | + * } |
| 141 | + * |
| 142 | + * @param log - The Log object to calculate size for |
| 143 | + * @param depth - Maximum depth to traverse in nested structures (default: 3) |
| 144 | + * @returns The estimated size of the log object in bytes |
| 145 | + */ |
| 146 | + private static estimateLogSize(log: LogRecord, maxDepth: number = 3): number { |
| 147 | + // Queue contains tuples of [log_content, depth] where: |
| 148 | + // - log_content is the current piece of log data being processed |
| 149 | + // - depth tracks how many levels deep we've traversed to reach this content |
| 150 | + // - body starts at depth 0 since it's an AnyValue object |
| 151 | + // - Attributes start at depth -1 since it's a Mapping[str, AnyValue] - when traversed, we will |
| 152 | + // start processing its keys at depth 0 |
| 153 | + let queue: Array<[AnyValue, number]> = [ |
| 154 | + [log.body, 0], |
| 155 | + [log.attributes, -1], |
| 156 | + ]; |
| 157 | + |
| 158 | + // Track visited complex log contents to avoid calculating the same one more than once |
| 159 | + const visited = new Set<object>(); |
| 160 | + |
| 161 | + let size: number = BASE_LOG_BUFFER_BYTE_SIZE; |
| 162 | + |
| 163 | + while (queue.length > 0) { |
| 164 | + const newQueue: Array<[AnyValue, number]> = []; |
| 165 | + |
| 166 | + for (const [nextVal, currentDepth] of queue) { |
| 167 | + // Small optimization, can stop calculating the size once it reaches the 1 MB limit |
| 168 | + if (size >= MAX_LOG_REQUEST_BYTE_SIZE) { |
| 169 | + return size; |
| 170 | + } |
| 171 | + |
| 172 | + if (nextVal == null) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + if (typeof nextVal === 'number' || typeof nextVal === 'boolean' || typeof nextVal === 'string') { |
| 177 | + size += this.estimateUtf8Size(nextVal.toString()); |
| 178 | + continue; |
| 179 | + } |
| 180 | + |
| 181 | + if (nextVal instanceof Uint8Array) { |
| 182 | + size += nextVal.byteLength; |
| 183 | + continue; |
| 184 | + } |
| 185 | + |
| 186 | + // nextVal must be Array or AnyValueMap |
| 187 | + if (currentDepth <= maxDepth && !visited.has(nextVal)) { |
| 188 | + visited.add(nextVal); |
| 189 | + |
| 190 | + if (Array.isArray(nextVal)) { |
| 191 | + for (const content of nextVal) { |
| 192 | + newQueue.push([content, currentDepth + 1]); |
| 193 | + } |
| 194 | + continue; |
| 195 | + } |
| 196 | + if (typeof nextVal === 'object') { |
| 197 | + for (const key in nextVal) { |
| 198 | + size += AwsCloudWatchOtlpBatchLogRecordProcessor.estimateUtf8Size(key); |
| 199 | + newQueue.push([nextVal[key], currentDepth + 1]); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + queue = newQueue; |
| 205 | + } |
| 206 | + return size; |
| 207 | + } |
| 208 | + |
| 209 | + private static estimateUtf8Size(s: string): number { |
| 210 | + let asciiCount = 0; |
| 211 | + let nonAsciiCount = 0; |
| 212 | + |
| 213 | + for (const char of s) { |
| 214 | + if (char.charCodeAt(0) < 128) { |
| 215 | + asciiCount += 1; |
| 216 | + } else { |
| 217 | + nonAsciiCount += 1; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + return asciiCount + nonAsciiCount * 4; |
| 222 | + } |
| 223 | +} |
0 commit comments