Data and monitoring · Advanced · 30 min
Run the OpenTelemetry Collector as a telemetry pipeline
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 LTS · OpenTelemetry Collector contrib 0.166.0 · Go 1.27.4 (for telemetrygen)
You will run OpenTelemetry Collector contrib 0.166 on Ubuntu as one pipeline for traces, metrics and logs that deletes email and authorisation attributes, keeps every error trace and 10% of the rest, and reports on its own health through internal metrics.
On this page
Every service you instrument with OpenTelemetry needs somewhere to send its telemetry. Pointing each one straight at a vendor or a database works for one service, but it spreads decisions across every codebase: which back end, which credentials, what to drop, what to keep. The OpenTelemetry Collector pulls those decisions into one process. Applications send OTLP to it on the same machine, and the Collector cleans, samples, batches and forwards the data.
A Collector configuration has four kinds of parts. Receivers take data in. Processors change or drop it, in the order you list them. Exporters send it on. Pipelines join them, one per signal: traces, metrics and logs. Extensions such as a health check sit alongside.
In this tutorial you build one gateway with three pipelines. It refuses data before it runs out of memory, deletes email addresses and authorisation headers that applications should never have sent, keeps every trace that contains an error plus a 10% sample of the rest, and forwards the result over OTLP. A second, throwaway Collector stands in for your back end so you can read exactly what arrived.
You use the contrib distribution, otelcol-contrib. The core otelcol build has no tail sampling or attributes processor.
Step 1 · Install the Collector package
Install curl and jq (used to read the results later), set the version and your CPU architecture, then download the package and its checksum from the official release:
sudo apt update
sudo apt install -y curl jq
OTELCOL_VERSION=0.166.0
ARCH="$(dpkg --print-architecture)"
mkdir -p ~/otelcol-install
cd ~/otelcol-install
DEB="otelcol-contrib_${OTELCOL_VERSION}_linux_${ARCH}.deb"
BASE="https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v${OTELCOL_VERSION}"
curl -fsSLO "${BASE}/${DEB}"
curl -fsSLO "${BASE}/${DEB}.sha256"
echo "$(cat "${DEB}.sha256") ${DEB}" | sha256sum -cThe .sha256 file holds only the hash, so the echo adds the file name before sha256sum checks it. The last line prints the file name followed by OK. Later steps use ARCH, so stay in this terminal.
The package's install script enables the service and starts it at once with a sample configuration that listens on every network interface, on the OTLP ports 4317 and 4318 and on Jaeger and Zipkin ports. Stop it as soon as the install finishes, and leave it stopped until your own configuration is in place:
sudo apt install -y "./${DEB}"sudo systemctl stop otelcol-contribThe package installs /usr/bin/otelcol-contrib, the configuration /etc/otelcol-contrib/config.yaml, an environment file /etc/otelcol-contrib/otelcol-contrib.conf and a systemd unit that runs as the otelcol-contrib system user.
A package installed from a GitHub release is not in any apt repository, so apt upgrade never updates it. To upgrade, download the newer .deb the same way and run sudo apt install ./<file>.deb again. Read the release notes first: the Collector is still at version 0.x and renames or removes settings between releases.
Check it worked
otelcol-contrib --version
otelcol-contrib components | grep -E 'name: (memory_limiter|attributes|tail_sampling|batch|otlp_http|debug)'The first line is something like otelcol-contrib version 0.166.0. The second command lists six of the components this tutorial uses; if one is missing, you have installed the core otelcol package instead of otelcol-contrib.
Step 2 · Start a stand-in back end
The gateway needs somewhere to export to. Until you connect your real back end, a second Collector does the job: it accepts OTLP over HTTP on port 5318 and writes each signal to a JSON file you can inspect. The heredoc is unquoted on purpose, so the shell writes your real home folder into the paths:
mkdir -p ~/otel-demo/sink
cat > ~/otel-demo/sink.yaml <<EOF
# Stand-in back end: OTLP over HTTP in, one JSON file per signal out.
receivers:
otlp:
protocols:
http:
endpoint: 127.0.0.1:5318
exporters:
file/traces:
path: $HOME/otel-demo/sink/traces.json
file/metrics:
path: $HOME/otel-demo/sink/metrics.json
file/logs:
path: $HOME/otel-demo/sink/logs.json
service:
telemetry:
metrics:
readers:
- pull:
exporter:
prometheus:
host: 127.0.0.1
port: 8889
pipelines:
traces:
receivers: [otlp]
exporters: [file/traces]
metrics:
receivers: [otlp]
exporters: [file/metrics]
logs:
receivers: [otlp]
exporters: [file/logs]
EOF
otelcol-contrib validate --config="$HOME/otel-demo/sink.yaml"Every Collector serves its own metrics, on port 8888 unless told otherwise. The telemetry block moves this one to 8889 so it does not clash with the gateway. validate returns to the prompt without an error.
In your second terminal, start the stand-in in the foreground and leave it running:
otelcol-contrib --config="$HOME/otel-demo/sink.yaml"It logs a few start-up lines ending with something like Everything is ready. Begin running and processing data.
Check it worked
Back in the first terminal:
ss -tln | grep '127.0.0.1:5318'One line shows a socket listening on 127.0.0.1:5318.
Step 3 · Write the pipeline configuration
The next command replaces the package's sample configuration, so keep a copy of the original first:
sudo cp /etc/otelcol-contrib/config.yaml /etc/otelcol-contrib/config.yaml.sampleNow write the gateway configuration:
sudo tee /etc/otelcol-contrib/config.yaml > /dev/null <<'YAML'
extensions:
health_check:
endpoint: 127.0.0.1:13133
receivers:
otlp:
protocols:
grpc:
endpoint: 127.0.0.1:4317
http:
endpoint: 127.0.0.1:4318
processors:
# Refuse new data before the process runs out of memory.
memory_limiter:
check_interval: 1s
limit_mib: 400
spike_limit_mib: 80
# Delete personal data and credentials that should never leave the server.
attributes/scrub:
actions:
- action: delete
pattern: '^(.+\.)?email$'
- action: delete
pattern: '^http\.request\.header\.(authorization|cookie)$'
- action: delete
key: authorization
# Keep every trace with an error, plus 10% of all the others.
tail_sampling:
decision_wait: 10s
num_traces: 50000
expected_new_traces_per_sec: 100
policies:
- name: keep-every-error
type: status_code
status_code:
status_codes: [ERROR]
- name: keep-ten-percent
type: probabilistic
probabilistic:
sampling_percentage: 10
batch:
send_batch_size: 2048
timeout: 2s
exporters:
# Replace this endpoint with your back end's OTLP/HTTP address.
otlp_http/backend:
endpoint: http://127.0.0.1:5318
debug:
verbosity: basic
service:
extensions: [health_check]
telemetry:
metrics:
readers:
- pull:
exporter:
prometheus:
host: 127.0.0.1
port: 8888
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, attributes/scrub, tail_sampling, batch]
exporters: [otlp_http/backend, debug]
metrics:
receivers: [otlp]
processors: [memory_limiter, attributes/scrub, batch]
exporters: [otlp_http/backend, debug]
logs:
receivers: [otlp]
processors: [memory_limiter, attributes/scrub, batch]
exporters: [otlp_http/backend, debug]
YAMLA component is only defined in the top sections. It does nothing until a pipeline under service names it, and the order inside processors: [...] is the order the data flows through.
Receiver. Both OTLP ports listen on 127.0.0.1, so only programs on this server can send. To run this as a gateway for other machines, change the address to the server's private IP and allow the ports in your firewall only from your own network. The Collector has no login on these ports unless you add an authentication extension.
memory_limiter comes first. Once heap use passes the soft limit (limit_mib minus spike_limit_mib, here 320 MiB), it refuses new data with an error that tells OTLP senders to retry later, instead of letting the process be killed. You set the matching Go memory limit in a later step.
attributes/scrub deletes any attribute whose name is email or ends in .email, the Authorization and Cookie request headers in OpenTelemetry's naming, and a bare authorization key. It works on span, log record and metric data point attributes. It does not touch resource attributes, span events or log bodies; an email written into a log message stays there. Fix that in the application, and see the redaction tutorial linked at the end for patterns. Scrubbing runs before sampling, so the spans held in memory are already clean.
tail_sampling is in the traces pipeline only. It holds each trace's spans for decision_wait after the first span arrives, then runs the policies. A trace is kept if any policy says keep, so every error trace survives and other traces have a 10% chance. num_traces caps how many traces wait in memory at once.
Tail sampling needs every span of a trace to reach the same Collector. With one gateway that is automatic. With several, put a layer in front that routes by trace ID (the contrib loadbalancing exporter does this) or the policies see partial traces.
batch comes last, after anything that drops data, and groups items into larger requests.
Exporters. otlp_http/backend sends all three signals to one OTLP/HTTP address with gzip compression on by default. debug writes one summary line per batch to the Collector's log, which is handy while you build the pipeline and cheap enough to leave on at basic. Remove it once you trust the pipeline.
Sampling, batching and compression together cut the volume that leaves the server, often by most of it for healthy traffic. That matters wherever outbound data costs money, from cloud egress fees to a site on a metered or prepaid connection.
Most hosted back ends need a token. Never write it into config.yaml. Put it in the service's environment file (which you rewrite in the step after next, created private as shown there) and refer to it by name in the exporter:
otlp_http/backend:
endpoint: https://otlp.example.com
headers:
authorization: "Bearer ${env:OTLP_BACKEND_TOKEN}"The Collector replaces ${env:NAME} with the variable's value when it loads the file.
Check it worked
grep -c 'processors: \[memory_limiter' /etc/otelcol-contrib/config.yaml
ls /etc/otelcol-contrib/The count is 3, one for each pipeline, and the folder lists config.yaml, config.yaml.sample and otelcol-contrib.conf.
Step 4 · Validate the configuration
validate loads the file, builds every component and checks the pipelines without opening any ports or sending anything. Run it after every change, before you restart the service:
otelcol-contrib validate --config=/etc/otelcol-contrib/config.yaml
echo "exit code $?"See what a mistake looks like. This small file names a processor in its pipeline that it never defines:
cat > ~/otel-demo/broken.yaml <<'YAML'
receivers:
otlp:
protocols:
grpc:
endpoint: 127.0.0.1:4317
exporters:
debug:
service:
pipelines:
traces:
receivers: [otlp]
processors: [tail_sampling]
exporters: [debug]
YAML
otelcol-contrib validate --config="$HOME/otel-demo/broken.yaml" || echo "validate found a problem"Check it worked
The real configuration prints exit code 0 with no error. The broken one prints an error that names tail_sampling as a processor the pipeline references but the file does not configure, followed by validate found a problem. Misspelt keys, such as sampling_percent, are reported with the component and the key name.
Step 5 · Set a memory limit and start the service
The systemd unit reads /etc/otelcol-contrib/otelcol-contrib.conf for its environment. Rewrite it with the original start-up option plus GOMEMLIMIT, which tells the Go runtime to collect garbage harder as it nears 320 MiB, 80% of the 400 MiB limit_mib. The file is created readable by root only, so a token you add later stays private:
sudo install -m 600 -o root -g root /dev/null /etc/otelcol-contrib/otelcol-contrib.conf
sudo tee /etc/otelcol-contrib/otelcol-contrib.conf > /dev/null <<'CONF'
# Environment for the otelcol-contrib systemd service.
OTELCOL_OPTIONS="--config=/etc/otelcol-contrib/config.yaml"
# About 80% of memory_limiter's limit_mib (400).
GOMEMLIMIT=320MiB
CONFSet limit_mib to what this server can spare, then keep GOMEMLIMIT at about 80% of it and spike_limit_mib at about 20%. On a 1 GB VPS shared with an application, 400 MiB is a reasonable ceiling.
Start the service with the new configuration:
sudo systemctl restart otelcol-contrib
sudo systemctl status otelcol-contrib --no-pagerThe status shows active (running). If it shows failed, read why with sudo journalctl -u otelcol-contrib -n 30 --no-pager; a configuration that passed validate usually fails here only because a port is already taken.
Check it worked
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:13133/
ss -tln | grep -E '127\.0\.0\.1:(4317|4318|8888|13133)'The health check prints 200. The ss output has four lines, all on 127.0.0.1; none shows 0.0.0.0 or * for these ports.
Step 6 · Send sample telemetry with telemetrygen
telemetrygen is the Collector project's own load generator. It sends synthetic traces, metrics and logs over OTLP, with the attributes and status you choose. There is no prebuilt binary in the release downloads, so build it with Go. (If you already run Docker, the project also publishes it as the container image ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen.) Everything goes under ~/otel-demo, which you delete at the end. The Go archive is about 70 MB and the modules it fetches add more, so on a metered connection you may prefer to build it on a machine with cheaper bandwidth and copy the single telemetrygen binary across.
Download and check Go 1.27.4 (still in the first terminal, where ARCH is set):
GO_VERSION=1.27.4
case "$ARCH" in
amd64) GO_SHA256=7226736c2339719cda28fbf3a54d71aa63bcefe376e98cef00034268dab24840 ;;
arm64) GO_SHA256=3392582988a273a87ab5043ee1b2c9c17a2c3c00580bf9326c47a2e0aaaeac92 ;;
esac
cd ~/otel-demo
curl -fsSLO "https://go.dev/dl/go${GO_VERSION}.linux-${ARCH}.tar.gz"
echo "${GO_SHA256} go${GO_VERSION}.linux-${ARCH}.tar.gz" | sha256sum -c
tar -xzf "go${GO_VERSION}.linux-${ARCH}.tar.gz"Build telemetrygen at the version that matches the Collector. The variables keep Go's caches inside the demo folder; building takes a few minutes on a small server:
GOPATH="$HOME/otel-demo/gopath" GOCACHE="$HOME/otel-demo/gocache" GOBIN="$HOME/otel-demo/bin" \
GOTOOLCHAIN=local CGO_ENABLED=0 \
~/otel-demo/go/bin/go install github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen@v0.166.0Now send four batches to the gateway. Every item carries a user.email attribute, as a careless application might. The error traces also carry an Authorization header value; Bearer demo-only is a made-up string, not a credential. --rate 0 turns off telemetrygen's default throttle of one item per second:
TG=~/otel-demo/bin/telemetrygen
COMMON=(--otlp-endpoint 127.0.0.1:4317 --otlp-insecure --service checkout --rate 0)
"$TG" traces "${COMMON[@]}" --traces 5 --status-code Error \
--telemetry-attributes 'user.email="amara@example.com"' \
--telemetry-attributes 'http.request.header.authorization="Bearer demo-only"'
"$TG" traces "${COMMON[@]}" --traces 200 --status-code Ok \
--telemetry-attributes 'user.email="amara@example.com"'
"$TG" logs "${COMMON[@]}" --logs 3 --body 'card payment declined' \
--severity-text Error --severity-number 17 \
--telemetry-attributes 'user.email="amara@example.com"'
"$TG" metrics "${COMMON[@]}" --metrics 3 --otlp-metric-name checkout.requests \
--telemetry-attributes 'user.email="amara@example.com"'Each trace has two spans, a parent and one child, so the gateway receives 410 spans. String values keep their double quotes inside the single quotes; telemetrygen rejects user.email=amara@example.com without them.
Check it worked
Each command logs its progress and exits without an error. The two trace runs end with a line containing traces generated and the count, 5 and then 200.
Step 7 · Check what reached the back end
Wait about 15 seconds. The tail sampler holds each trace for 10 seconds before it decides, then the batch processor and the stand-in's file writer each add a moment.
Count the error traces and the successful traces that arrived, list the span attribute keys, and look for the email address and the header anywhere in the three files:
cd ~/otel-demo/sink
jq -r '.resourceSpans[].scopeSpans[].spans[] | select(.status.code == 2) | .traceId' traces.json | sort -u | wc -l
jq -r '.resourceSpans[].scopeSpans[].spans[] | select(.status.code == 1) | .traceId' traces.json | sort -u | wc -l
jq -r '.resourceSpans[].scopeSpans[].spans[].attributes[]?.key' traces.json | sort -u
grep -l -e 'amara@example.com' -e 'authorization' traces.json metrics.json logs.json || echo "no email or authorization found"In OTLP JSON a span's status code is a number: 2 is error and 1 is OK.
Then the logs and metrics:
jq -r '.resourceLogs[].scopeLogs[].logRecords[] | "\(.severityText) \(.body.stringValue)"' logs.json
jq -r '.resourceMetrics[].scopeMetrics[].metrics[].name' metrics.json | sort -uThe gateway's debug exporter logged the same traffic to the journal. Each line summarises one batch, with counts such as "spans": 12 or "log records": 3:
sudo journalctl -u otelcol-contrib --since "10 minutes ago" --no-pager | grep -E '"(spans|log records|data points)"'Check it worked
- The error count is
5: every error trace survived. - The OK count is somewhere near
20, 10% of 200. It changes from run to run because the sampler hashes random trace IDs; anything from about 10 to 30 is normal. - The attribute keys are telemetrygen's own, such as its peer address and service name keys.
user.emailandhttp.request.header.authorizationare not among them. - The
grepprintsno email or authorization found. If it prints a file name instead, an attribute got through: compare its exact key with the patterns inattributes/scrub. - The logs show
Error card payment declinedthree times, and the metrics listcheckout.requests.
Step 8 · Watch the Collector's own metrics
The debug exporter shows that data moves. The internal metrics on port 8888 show how much, where it stops and why. Compare what the receiver accepted with what the exporter sent:
curl -s http://127.0.0.1:8888/metrics | grep -E '^otelcol_(receiver_accepted|receiver_refused|exporter_sent|exporter_send_failed)_spans'
curl -s http://127.0.0.1:8888/metrics | grep -E '^otelcol_(processor_tail_sampling_global_count_traces_sampled|exporter_queue_size|exporter_queue_capacity)'Each line is a counter with labels naming the component, for example exporter="otlp_http/backend". What to watch on a real gateway:
otelcol_receiver_accepted_spansagainstotelcol_exporter_sent_spans: the gap is your sampling. A gap far larger than your policies explain means data is being lost somewhere.otelcol_receiver_refused_spansabove zero: the memory limiter is pushing back. Give the Collector more memory or send less.otelcol_exporter_send_failed_spansabove zero: the back end rejected data or could not be reached after retries.otelcol_exporter_queue_sizeclose tootelcol_exporter_queue_capacity: the back end is slower than your traffic, and new data will be dropped once the queue is full. The queue lives in memory and is lost on restart.otelcol_processor_tail_sampling_global_count_traces_sampledwithsampled="true"and"false": the share of traces you keep.
The same families exist for metric points and log records (otelcol_receiver_accepted_log_records, otelcol_exporter_sent_metric_points and so on). If Prometheus runs on this server, add a scrape job so these become graphs and alerts:
scrape_configs:
- job_name: otelcol
static_configs:
- targets: ['127.0.0.1:8888']Check it worked
otelcol_receiver_accepted_spans adds up to 410 across its lines. otelcol_exporter_sent_spans for otlp_http/backend is about 50: 10 error spans plus two spans for each sampled OK trace. otelcol_exporter_send_failed_spans is 0 or absent, and the queue size is 0 now that the traffic has stopped.
Step 9 · Clean up the demo
Stop the stand-in back end with Ctrl+C in the second terminal. From now on the gateway cannot deliver anything. Either change endpoint under otlp_http/backend to your real back end, validate and restart, or stop the service until you are ready:
sudo systemctl stop otelcol-contribDelete the demo folder, the Go toolchain and the downloads. Go marks its module cache read-only, so make it writable first:
cd ~
chmod -R u+w ~/otel-demo
rm -rf ~/otel-demo ~/otelcol-installIf you were only trying the Collector out, remove it too. This deletes /etc/otelcol-contrib, including your configuration and the saved sample, and the service user:
sudo apt purge -y otelcol-contrib
sudo rm -rf /etc/otelcol-contrib
sudo userdel otelcol-contribCheck it worked
test -e ~/otel-demo && echo "demo folder still there" || echo "demo folder removed"
command -v otelcol-contrib || echo "collector removed"It prints demo folder removed, and collector removed if you purged the package. ss -tln no longer lists ports 4317, 4318, 5318, 8888 or 13133.
Next steps
- Send real traces into this gateway from an application in /tutorials/trace-a-python-web-app-with-opentelemetry.
- Point the metrics pipeline at Prometheus's native OTLP endpoint in /tutorials/send-opentelemetry-metrics-to-prometheus.
- Scrape the gateway's port 8888 alongside your server metrics with /tutorials/monitor-a-server-with-prometheus-and-node-exporter.
Related tutorials
- Trace a Python web app with OpenTelemetryYou will add OpenTelemetry tracing to a FastAPI app that calls a second service with httpx, with named resources, your own spans, events and recorded errors, exported first to the terminal and then to a Collector, with sampling set from environment variables.
- Send OpenTelemetry metrics to Prometheus with native OTLPYou will push OpenTelemetry metrics from a Python app into Prometheus 3.16's built-in OTLP receiver, directly and through the Collector, and query them back with the right names, labels and temporality.
- Understand metrics, logs and traces and when to use eachYou will have followed one failing checkout request through a metric, a log line and a trace, and written a one-page plan for which signal your own system should reach for first.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials