-
Notifications
You must be signed in to change notification settings - Fork 1.1k
docs(configuration): add declarative config example for startNodeSDK() #6834
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
Open
MikeGoldsmith
wants to merge
7
commits into
open-telemetry:main
Choose a base branch
from
MikeGoldsmith:mike/6807-declarative-config-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
52e22af
add declarative config example for startNodeSDK()
MikeGoldsmith 2f6e3b6
Merge remote-tracking branch 'upstream/main' into mike/6807-declarati…
MikeGoldsmith f7818fa
Merge remote-tracking branch 'upstream/main' into mike/6807-declarati…
MikeGoldsmith e3ed382
merge main + address Marylia's feedback on the docs example
MikeGoldsmith d5806d7
Merge remote-tracking branch 'upstream/main' into mike/6807-declarati…
MikeGoldsmith 500af3e
address Trent's PR feedback on the declarative config example
MikeGoldsmith d1c2c5b
bump declarative-config example deps to 0.220.0 to match monorepo
MikeGoldsmith 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Declarative Configuration Example | ||
|
|
||
| End-to-end example of configuring the Node SDK from a YAML file via | ||
| `OTEL_CONFIG_FILE` and `startNodeSDK()`. No programmatic provider construction: | ||
| the YAML drives traces, metrics, logs, resource attributes, propagators, and | ||
| the sampler. | ||
|
|
||
| ## What this demonstrates | ||
|
|
||
| - A single `otel-config.yaml` covering traces, metrics, and logs over OTLP HTTP. | ||
| - Environment variable substitution (`${OTEL_EXPORTER_OTLP_ENDPOINT:-...}`, | ||
| `${EXAMPLE_API_KEY:-}`) so secrets and per-environment values stay out of the | ||
| YAML. | ||
| - A `parent_based` sampler config. | ||
|
|
||
| See [`../../packages/configuration/README.md`](../../packages/configuration/README.md) | ||
| for the full list of supported fields and current limitations. | ||
|
|
||
| ## Run it | ||
|
|
||
| The example exports to any OTLP HTTP endpoint. The simplest path is the bundled | ||
| collector that prints what it receives. | ||
|
|
||
| ```sh | ||
| # 1. Start a collector locally (OTLP HTTP receiver on :4318, debug exporter) | ||
| docker compose up -d | ||
|
|
||
| # 2. Install deps + start the example | ||
| npm install | ||
| npm start | ||
| ``` | ||
|
|
||
| You should see span, metric, and log entries in the collector's container logs: | ||
|
|
||
| ```sh | ||
| docker compose logs -f otel-collector | ||
| ``` | ||
|
|
||
| To export to your own backend instead, set `OTEL_EXPORTER_OTLP_ENDPOINT` and | ||
| (optionally) `EXAMPLE_API_KEY`: | ||
|
|
||
| ```sh | ||
| OTEL_EXPORTER_OTLP_ENDPOINT=https://vendor.endpoint \ | ||
| EXAMPLE_API_KEY=$VENDOR_API_KEY \ | ||
| npm start | ||
| ``` | ||
|
|
||
| Tear down the collector with `docker compose down`. |
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,9 @@ | ||
| services: | ||
| otel-collector: | ||
| image: otel/opentelemetry-collector-contrib:latest | ||
| command: ["--config=/etc/otel-collector-config.yaml"] | ||
| volumes: | ||
| - "./otel-collector-config.yaml:/etc/otel-collector-config.yaml" | ||
| ports: | ||
| - "4318:4318" # OTLP HTTP | ||
| restart: unless-stopped |
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,41 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { trace, metrics } from '@opentelemetry/api'; | ||
| import { logs, SeverityNumber } from '@opentelemetry/api-logs'; | ||
| import { startNodeSDK } from '@opentelemetry/sdk-node'; | ||
|
|
||
| // `startNodeSDK()` reads OTEL_CONFIG_FILE (set in package.json's start script) | ||
| // and wires up trace, metric, and log pipelines from the YAML. No programmatic | ||
| // provider construction needed. | ||
| const sdk = startNodeSDK(); | ||
|
|
||
| const tracer = trace.getTracer('example'); | ||
| const meter = metrics.getMeter('example'); | ||
| const logger = logs.getLogger('example'); | ||
|
|
||
| const counter = meter.createCounter('example.requests', { | ||
| description: 'Demo counter incremented per request', | ||
| }); | ||
|
|
||
| async function main(): Promise<void> { | ||
| await tracer.startActiveSpan('example.request', async span => { | ||
| span.setAttribute('example.kind', 'demo'); | ||
| counter.add(1, { route: '/hello' }); | ||
| logger.emit({ | ||
| severityNumber: SeverityNumber.INFO, | ||
| body: 'Handled example request', | ||
| attributes: { route: '/hello' }, | ||
| }); | ||
| span.end(); | ||
| }); | ||
|
|
||
| await sdk.shutdown(); | ||
| } | ||
|
|
||
| main().catch(err => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
21 changes: 21 additions & 0 deletions
21
experimental/examples/declarative-config/otel-collector-config.yaml
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,21 @@ | ||
| receivers: | ||
| otlp: | ||
| protocols: | ||
| http: | ||
| endpoint: 0.0.0.0:4318 | ||
|
|
||
| exporters: | ||
| debug: | ||
| verbosity: detailed | ||
|
|
||
| service: | ||
| pipelines: | ||
| traces: | ||
| receivers: [otlp] | ||
| exporters: [debug] | ||
| metrics: | ||
| receivers: [otlp] | ||
| exporters: [debug] | ||
| logs: | ||
| receivers: [otlp] | ||
| exporters: [debug] |
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,58 @@ | ||
| file_format: "1.1" | ||
|
|
||
| disabled: ${OTEL_SDK_DISABLED:-false} | ||
| log_level: info | ||
|
|
||
| resource: | ||
| attributes: | ||
| - name: service.name | ||
| value: declarative-config-example | ||
| - name: service.version | ||
| value: "0.1.0" | ||
| - name: deployment.environment.name | ||
| value: ${DEPLOYMENT_ENV:-development} | ||
|
|
||
| # Configure the W3C propagators explicitly. tracecontext and baggage match the | ||
| # spec default, but the propagator block must be present for any to be applied. | ||
| propagator: | ||
| composite: | ||
| - tracecontext: | ||
| - baggage: | ||
|
|
||
| tracer_provider: | ||
| processors: | ||
| - batch: | ||
| exporter: | ||
| otlp_http: | ||
| endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://localhost:4318}/v1/traces | ||
| # Headers support env var substitution so secrets don't live in the YAML. | ||
| headers: | ||
| - name: x-example-api-key | ||
| value: ${EXAMPLE_API_KEY:-} | ||
| compression: gzip | ||
| timeout: 10000 | ||
| sampler: | ||
| parent_based: | ||
| root: | ||
| trace_id_ratio_based: | ||
| ratio: 1.0 | ||
|
|
||
| meter_provider: | ||
| readers: | ||
| - periodic: | ||
| interval: 10000 | ||
| timeout: 5000 | ||
| exporter: | ||
| otlp_http: | ||
| endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://localhost:4318}/v1/metrics | ||
| compression: gzip | ||
| timeout: 10000 | ||
|
|
||
| logger_provider: | ||
| processors: | ||
| - batch: | ||
| exporter: | ||
| otlp_http: | ||
| endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://localhost:4318}/v1/logs | ||
| compression: gzip | ||
| timeout: 10000 |
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,23 @@ | ||
| { | ||
| "name": "declarative-config-example", | ||
| "version": "0.220.0", | ||
| "private": true, | ||
| "description": "Example of configuring the Node SDK from a YAML file via OTEL_CONFIG_FILE + startNodeSDK()", | ||
| "main": "index.ts", | ||
| "scripts": { | ||
| "start": "OTEL_CONFIG_FILE=./otel-config.yaml ts-node index.ts", | ||
| "align-api-deps": "node ../../../scripts/align-api-deps.js" | ||
| }, | ||
| "author": "OpenTelemetry Authors", | ||
| "license": "Apache-2.0", | ||
| "dependencies": { | ||
| "@opentelemetry/api": "^1.7.0", | ||
| "@opentelemetry/api-logs": "0.220.0", | ||
| "@opentelemetry/configuration": "0.220.0", | ||
| "@opentelemetry/sdk-node": "0.220.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "18.19.130", | ||
| "ts-node": "^10.9.1" | ||
| } | ||
| } |
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,22 @@ | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "build", | ||
| "rootDir": "." | ||
| }, | ||
| "include": ["./index.ts"], | ||
| "references": [ | ||
| { | ||
| "path": "../../../api" | ||
| }, | ||
| { | ||
| "path": "../../../experimental/packages/api-logs" | ||
| }, | ||
| { | ||
| "path": "../../../experimental/packages/configuration" | ||
| }, | ||
| { | ||
| "path": "../../../experimental/packages/opentelemetry-sdk-node" | ||
| } | ||
| ] | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.