|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// We access through node_modules to allow it to be patched. |
| 18 | +/* eslint-disable node/no-extraneous-require */ |
| 19 | + |
| 20 | +import * as path from 'path'; |
| 21 | + |
| 22 | +import { AwsLambdaInstrumentation } from '../../src'; |
| 23 | +import { |
| 24 | + BatchSpanProcessor, |
| 25 | + InMemorySpanExporter, |
| 26 | +} from '@opentelemetry/tracing'; |
| 27 | +import { NodeTracerProvider } from '@opentelemetry/node'; |
| 28 | +import { Context } from 'aws-lambda'; |
| 29 | +import * as assert from 'assert'; |
| 30 | +import { ProxyTracerProvider, TracerProvider } from '@opentelemetry/api'; |
| 31 | + |
| 32 | +const memoryExporter = new InMemorySpanExporter(); |
| 33 | + |
| 34 | +describe('force flush', () => { |
| 35 | + let instrumentation: AwsLambdaInstrumentation; |
| 36 | + |
| 37 | + let oldEnv: NodeJS.ProcessEnv; |
| 38 | + |
| 39 | + const ctx = { |
| 40 | + functionName: 'my_function', |
| 41 | + invokedFunctionArn: 'my_arn', |
| 42 | + awsRequestId: 'aws_request_id', |
| 43 | + } as Context; |
| 44 | + |
| 45 | + const initializeHandler = (handler: string, provider: TracerProvider) => { |
| 46 | + process.env._HANDLER = handler; |
| 47 | + |
| 48 | + instrumentation = new AwsLambdaInstrumentation(); |
| 49 | + instrumentation.setTracerProvider(provider); |
| 50 | + }; |
| 51 | + |
| 52 | + const lambdaRequire = (module: string) => |
| 53 | + require(path.resolve(__dirname, '..', module)); |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + oldEnv = { ...process.env }; |
| 57 | + process.env.LAMBDA_TASK_ROOT = path.resolve(__dirname, '..'); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + process.env = oldEnv; |
| 62 | + instrumentation.disable(); |
| 63 | + |
| 64 | + memoryExporter.reset(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should force flush NodeTracerProvider', async () => { |
| 68 | + const provider = new NodeTracerProvider(); |
| 69 | + provider.addSpanProcessor(new BatchSpanProcessor(memoryExporter)); |
| 70 | + provider.register(); |
| 71 | + let forceFlushed = false; |
| 72 | + const forceFlush = () => |
| 73 | + new Promise<void>(resolve => { |
| 74 | + forceFlushed = true; |
| 75 | + resolve(); |
| 76 | + }); |
| 77 | + provider.activeSpanProcessor.forceFlush = forceFlush; |
| 78 | + initializeHandler('lambda-test/sync.handler', provider); |
| 79 | + |
| 80 | + await new Promise((resolve, reject) => { |
| 81 | + lambdaRequire('lambda-test/sync').handler( |
| 82 | + 'arg', |
| 83 | + ctx, |
| 84 | + (err: Error, res: any) => { |
| 85 | + if (err) { |
| 86 | + reject(err); |
| 87 | + } else { |
| 88 | + resolve(res); |
| 89 | + } |
| 90 | + } |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + assert.strictEqual(forceFlushed, true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should force flush ProxyTracerProvider with NodeTracerProvider', async () => { |
| 98 | + const nodeTracerProvider = new NodeTracerProvider(); |
| 99 | + nodeTracerProvider.addSpanProcessor(new BatchSpanProcessor(memoryExporter)); |
| 100 | + nodeTracerProvider.register(); |
| 101 | + const provider = new ProxyTracerProvider(); |
| 102 | + provider.setDelegate(nodeTracerProvider); |
| 103 | + let forceFlushed = false; |
| 104 | + const forceFlush = () => |
| 105 | + new Promise<void>(resolve => { |
| 106 | + forceFlushed = true; |
| 107 | + resolve(); |
| 108 | + }); |
| 109 | + nodeTracerProvider.activeSpanProcessor.forceFlush = forceFlush; |
| 110 | + initializeHandler('lambda-test/sync.handler', provider); |
| 111 | + |
| 112 | + await new Promise((resolve, reject) => { |
| 113 | + lambdaRequire('lambda-test/sync').handler( |
| 114 | + 'arg', |
| 115 | + ctx, |
| 116 | + (err: Error, res: any) => { |
| 117 | + if (err) { |
| 118 | + reject(err); |
| 119 | + } else { |
| 120 | + resolve(res); |
| 121 | + } |
| 122 | + } |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + assert.strictEqual(forceFlushed, true); |
| 127 | + }); |
| 128 | +}); |
0 commit comments