|
| 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 | +// Use dataloader from an ES module: |
| 18 | +// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-dataloader-default-import.mjs |
| 19 | + |
| 20 | +import { trace } from '@opentelemetry/api'; |
| 21 | +import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; |
| 22 | +import * as crypto from 'crypto'; |
| 23 | + |
| 24 | +import { DataloaderInstrumentation } from '../../build/src/index.js'; |
| 25 | + |
| 26 | +const sdk = createTestNodeSdk({ |
| 27 | + serviceName: 'use-dataloader', |
| 28 | + instrumentations: [ |
| 29 | + new DataloaderInstrumentation() |
| 30 | + ] |
| 31 | +}) |
| 32 | +sdk.start(); |
| 33 | + |
| 34 | +import Dataloader from 'dataloader'; |
| 35 | + |
| 36 | +function getMd5HashFromIdx(idx ) { |
| 37 | + return crypto.createHash('md5').update(String(idx)).digest('hex'); |
| 38 | +} |
| 39 | +const dataloader = new Dataloader( |
| 40 | + async keys => |
| 41 | + keys.map((_, idx) => { |
| 42 | + return getMd5HashFromIdx(idx); |
| 43 | + }), |
| 44 | + { cache: true } |
| 45 | +); |
| 46 | + |
| 47 | +const tracer = trace.getTracer(); |
| 48 | +await tracer.startActiveSpan('manual', async (span) => { |
| 49 | + await dataloader.load(1); |
| 50 | + span.end(); |
| 51 | +}); |
| 52 | + |
| 53 | +await sdk.shutdown(); |
0 commit comments