-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: e2e test enabling profiling (#514)
* test: create a script for npm installing in all examples * feat: add debug logging for profiling * test: implement a basic e2e test for profiling * test: turn profiling test on in CI * feat: explicitly start and stop profiling in the example * feat: debug log exporter sends * test: improve the example * style: intent with spaces * docs: add readme for the example * docs: better wording in the readme
- Loading branch information
Showing
15 changed files
with
205 additions
and
11 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,2 @@ | ||
node_modules | ||
package-lock.json |
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,27 @@ | ||
# Profiling Example | ||
|
||
> :warning: Profiling is still an experimental feature | ||
This example showcases enabling profiling for Splunk APM. There's no official support for profiling in OTel, so profiling requires working with some Splunk-specific components. | ||
By default, the example requires the OTel Collector to run with the OTLP receiver listening for logs on `localhost:4317`. To export profiling data to APM, you must set up `splunk_hec` exporter in the Collector. See [the example collector config](./collector-config.yml). | ||
|
||
```shell | ||
# Replace <...> with the correct values | ||
export SPLUNK_REALM="<realm>" | ||
export SPLUNK_ACCESS_TOKEN="<your access token>" | ||
# Docker Compose configuration has been provided for convenience: | ||
docker compose up | ||
``` | ||
|
||
Then run the script in a separate terminal: | ||
|
||
```shell | ||
# Optional. To set the environment: | ||
export OTEL_SERVICE_NAME='profiling-example' | ||
export OTEL_RESOURCE_ATTRIBUTES='deployment.environment=dev' | ||
export OTEL_LOG_LEVEL='DEBUG' | ||
# Run the example: | ||
npm start | ||
``` | ||
|
||
The script will then export to collector on shutdown and the collector will take care of transforming the payload into `splunk_hec` format. |
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,33 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
signalfx: | ||
|
||
exporters: | ||
otlphttp: | ||
traces_endpoint: "https://ingest.${SPLUNK_REALM}.signalfx.com/v2/trace/otlp" | ||
headers: | ||
X-SF-TOKEN: "${SPLUNK_ACCESS_TOKEN}" | ||
splunk_hec: | ||
token: "${SPLUNK_ACCESS_TOKEN}" | ||
endpoint: "https://ingest.${SPLUNK_REALM}.signalfx.com/v1/log" | ||
logging/debug: | ||
loglevel: debug | ||
|
||
processors: | ||
batch: | ||
|
||
service: | ||
telemetry: | ||
logs: | ||
level: "debug" | ||
pipelines: | ||
traces: | ||
receivers: [otlp] | ||
processors: [batch] | ||
exporters: [logging/debug, otlphttp] | ||
logs/profiling: | ||
receivers: [otlp] | ||
processors: [batch] | ||
exporters: [logging/debug, splunk_hec] |
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,13 @@ | ||
version: "3" | ||
services: | ||
otel-collector: | ||
image: otel/opentelemetry-collector-contrib:0.50.0 | ||
environment: | ||
- SPLUNK_ACCESS_TOKEN | ||
- SPLUNK_REALM | ||
command: ["--config=/etc/otel-collector-config.yml"] | ||
volumes: | ||
- ./collector-config.yml:/etc/otel-collector-config.yml | ||
ports: | ||
- "9943:9943" # signalfx | ||
- "4317:4317" # otlp |
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,39 @@ | ||
const { start, stop } = require('@splunk/otel'); | ||
const { diag, DiagConsoleLogger, DiagLogLevel, trace, SpanStatusCode, context } = require('@opentelemetry/api'); | ||
|
||
// If OTEL_LOG_LEVEL env var is set, configure logger | ||
if (process.env.OTEL_LOG_LEVEL) { | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel[process.env.OTEL_LOG_LEVEL]); | ||
} | ||
|
||
// Profiling is still experimental and has to be enabled explicitly | ||
start({ | ||
// Tracing is enabled by default and is required for profiling | ||
profiling: { | ||
callstackInterval: 100, | ||
collectionDuration: 1_000, | ||
}, | ||
}); | ||
|
||
const doWork = () => { | ||
const start = Date.now(); | ||
while (Date.now() - start < 2000) {} | ||
}; | ||
|
||
// setTimeout has to be here because profiling is currently started asyncronously to avoid blocking the runtime. | ||
// If we didn't we'd run stop before the profiling has started in the background. | ||
setTimeout(() => { | ||
const tracer = trace.getTracer('splunk-otel-example-profiling'); | ||
const span = tracer.startSpan('main'); | ||
const spanContext = trace.setSpan(context.active(), span); | ||
|
||
console.log('starting spinning'); | ||
// Span and Trace IDs are attached to the profiling samples | ||
context.with(spanContext, doWork); | ||
|
||
console.log('done!'); | ||
span.end(); | ||
|
||
// Stop profiling to flush the collected samples | ||
stop(); | ||
}, 10); |
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,13 @@ | ||
{ | ||
"name": "splunk-otel-example-profiling", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/api": "^1.1.0", | ||
"@splunk/otel": "1.1.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,12 @@ | ||
#!/usr/bin/env sh | ||
|
||
PKG=$1 | ||
echo "Installing in all examples: $PKG" | ||
|
||
for d in ./examples/*/ ; do | ||
echo "$d" | ||
( | ||
cd "$d"; | ||
npm install $PKG | ||
) | ||
done |
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
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,8 @@ | ||
services: | ||
app: | ||
working_dir: /home/node/app/examples/profiling | ||
env_file: ./profiling/app.env | ||
test: | ||
command: node ./profiling | ||
environment: | ||
COLLECTOR_URL: http://collector:8378 |
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,4 @@ | ||
OTEL_SERVICE_NAME='profiling-example' | ||
OTEL_RESOURCE_ATTRIBUTES='deployment.environment=dev' | ||
OTEL_LOG_LEVEL='DEBUG' | ||
OTEL_EXPORTER_OTLP_ENDPOINT=collector:4317 |
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,14 @@ | ||
const { | ||
assertSpans, | ||
logSpanTable, | ||
request, | ||
waitSpans, | ||
} = require('../utils.js'); | ||
const snapshot = require('./snapshot.js'); | ||
|
||
waitSpans(snapshot.length).then((data) => { | ||
logSpanTable(data); | ||
return assertSpans(data, snapshot); | ||
}).then((validatedSpans) => { | ||
console.log(`${validatedSpans} spans validated.`); | ||
}); |
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,18 @@ | ||
// a console.log from a previous run | ||
module.exports = [ | ||
{ | ||
traceId: 'SugyaoARxWUXXGW7OE6nXg==', | ||
id: 'B4g4YngKBnM=', | ||
startTime: '2022-06-03T13:04:28.087011840Z', | ||
name: 'main', | ||
kind: 'internal', | ||
parentSpanId: undefined, | ||
parent: undefined, | ||
references: undefined, | ||
status: { code: undefined }, | ||
attributes: { | ||
'otel.library.name': 'splunk-otel-example-profiling', | ||
'span.kind': 'internal' | ||
} | ||
} | ||
]; |