-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ESM support for instrumentation (#3698)
Co-authored-by: Purvi Kanal <[email protected]> Co-authored-by: Jamie Danielson <[email protected]> Co-authored-by: Liz Fong-Jones <[email protected]> Co-authored-by: Purvi Kanal <[email protected]>
- Loading branch information
1 parent
bba09c0
commit 422a36a
Showing
17 changed files
with
432 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
# Overview | ||
|
||
This is a simple example that demonstrates tracing HTTP request, with an app written in TypeScript and transpiled to ES Modules. | ||
|
||
## Installation | ||
|
||
```sh | ||
# from this directory | ||
npm install | ||
npm run build | ||
npm start | ||
``` | ||
|
||
In a separate terminal, `curl localhost:3000`. | ||
|
||
See two spans in the console (one manual, one for http instrumentation) | ||
|
||
## Useful links | ||
|
||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node> | ||
|
||
## LICENSE | ||
|
||
Apache License 2.0 |
This file contains 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,43 @@ | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
import { trace, DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'; | ||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
import { | ||
ConsoleSpanExporter, | ||
SimpleSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; | ||
import http from 'http'; | ||
|
||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); | ||
const tracerProvider = new NodeTracerProvider({ | ||
resource: new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: 'esm-http-ts-example', | ||
}), | ||
}); | ||
const exporter = new ConsoleSpanExporter(); | ||
const processor = new SimpleSpanProcessor(exporter); | ||
tracerProvider.addSpanProcessor(processor); | ||
tracerProvider.register(); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [new HttpInstrumentation()], | ||
}); | ||
|
||
const hostname = '0.0.0.0'; | ||
const port = 3000; | ||
|
||
const server = http.createServer((req, res) => { | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/plain'); | ||
const tracer = trace.getTracer('esm-tracer'); | ||
tracer.startActiveSpan('manual', span => { | ||
span.end(); | ||
}); | ||
res.end('Hello, World!\n'); | ||
}); | ||
|
||
server.listen(port, hostname, () => { | ||
console.log(`Server running at http://${hostname}:${port}/`); | ||
}); |
This file contains 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,42 @@ | ||
{ | ||
"name": "esm-http-ts", | ||
"private": true, | ||
"version": "0.38.0", | ||
"description": "Example of HTTP integration with OpenTelemetry using ESM and TypeScript", | ||
"main": "build/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"start": "node --experimental-loader=@opentelemetry/instrumentation/hook.mjs ./build/index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js.git" | ||
}, | ||
"keywords": [ | ||
"opentelemetry", | ||
"http", | ||
"tracing", | ||
"esm", | ||
"typescript" | ||
], | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"author": "OpenTelemetry Authors", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/open-telemetry/opentelemetry-js/issues" | ||
}, | ||
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/", | ||
"dependencies": { | ||
"@opentelemetry/api": "1.4.0", | ||
"@opentelemetry/exporter-trace-otlp-proto": "0.38.0", | ||
"@opentelemetry/instrumentation": "0.38.0", | ||
"@opentelemetry/instrumentation-http": "0.38.0", | ||
"@opentelemetry/resources": "1.9.1", | ||
"@opentelemetry/sdk-trace-base": "1.9.1", | ||
"@opentelemetry/sdk-trace-node": "1.9.1", | ||
"@opentelemetry/semantic-conventions": "1.9.1" | ||
} | ||
} |
This file contains 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,25 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Language and Environment */ | ||
"target": "ES2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, | ||
|
||
/* Modules */ | ||
"module": "ESNext" /* Specify what module code is generated. */, | ||
"rootDir": "." /* Specify the root folder within your source files. */, | ||
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, | ||
"resolveJsonModule": true /* Enable importing .json files. */, | ||
|
||
/* Emit */ | ||
"outDir": "build" /* Specify an output folder for all emitted files. */, | ||
|
||
/* Interop Constraints */ | ||
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */, | ||
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, | ||
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, | ||
|
||
/* Completeness */ | ||
"skipLibCheck": true /* Skip type checking all .d.ts files. */ | ||
}, | ||
"include": ["**/*.ts", "**/*.js", "*.config.js"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains 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 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 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
28 changes: 28 additions & 0 deletions
28
experimental/packages/opentelemetry-instrumentation/hook.mjs
This file contains 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,28 @@ | ||
/*! | ||
* Copyright 2021 Datadog, Inc. | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License. | ||
// | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. | ||
|
||
import { | ||
load, | ||
resolve, | ||
getFormat, | ||
getSource, | ||
} from 'import-in-the-middle/hook.mjs'; | ||
export { load, resolve, getFormat, getSource }; |
This file contains 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
Oops, something went wrong.