Skip to content

Commit

Permalink
feat: traceparent header sharness test
Browse files Browse the repository at this point in the history
  • Loading branch information
iand authored and hacdias committed Apr 4, 2023
1 parent e7f8879 commit 8dcf3c5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/sharness/t0310-tracing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test_init_ipfs
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
export OTEL_TRACES_SAMPLER=always_on

cat <<EOF > collector-config.yaml
receivers:
Expand Down
88 changes: 88 additions & 0 deletions test/sharness/t0311-tracing-traceid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash

test_description="Test HTTP Gateway trace context propagation"

. lib/test-lib.sh

test_init_ipfs

export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
export OTEL_TRACES_SAMPLER=always_on

cat <<EOF > collector-config.yaml
receivers:
otlp:
protocols:
grpc:
processors:
batch:
exporters:
file:
path: /traces/traces.json
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [file]
EOF

# touch traces.json and give it 777 perms, in case docker runs as a different user
rm -rf traces.json && touch traces.json && chmod 777 traces.json

test_expect_success "run opentelemetry collector" '
docker run --rm -d -v "$PWD/collector-config.yaml":/config.yaml -v "$PWD":/traces --net=host --name=ipfs-test-otel-collector-t0311 otel/opentelemetry-collector-contrib:0.52.0 --config /config.yaml
'

test_launch_ipfs_daemon

test_expect_success "Make a file to test with" '
echo "Hello Worlds!" >expected &&
HASH=$(ipfs add -q expected) ||
test_fsh cat daemon_err
'

# HTTP GET Request
test_expect_success "GET to Gateway succeeds" '
curl -svX GET "http://127.0.0.1:$GWAY_PORT/ipfs/$HASH" >/dev/null 2>curl_output &&
cat curl_output
'

# GET Response from Gateway should contain no trace context headers
test_expect_success "GET response for request without traceparent contains no trace context headers" '
grep "< traceparent: \*" curl_output && false || true
'

test_expect_success "Trace collector is writing traces" '
until cat traces.json | grep "\"traceId\"" >/dev/null; do sleep 0.1; done
'

version="00"
trace_id="4bf92f3577b34da6a3ce929d0e0e4736"
parent_id="00f067aa0ba902b7"
flags="00"


# HTTP GET Request with traceparent
test_expect_success "GET to Gateway with traceparent header succeeds" '
curl -H "traceparent: $version-$trace_id-$parent_id-$flags" -svX GET "http://127.0.0.1:$GWAY_PORT/ipfs/$HASH" >/dev/null 2>curl_output &&
cat curl_output
'

test_expect_success "GET response for request with traceparent preserves trace id" '
grep "< Traceparent: $version-$trace_id-" curl_output
'

test_expect_success "Trace id is used in traces" '
until cat traces.json | grep "\"traceId\":\"$trace_id\"" >/dev/null; do sleep 0.1; done
'

test_kill_ipfs_daemon

test_expect_success "kill docker container" '
docker kill ipfs-test-otel-collector-t0311
'

test_done

0 comments on commit 8dcf3c5

Please sign in to comment.