Skip to content
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

Instrumentation sample #309

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
2 changes: 2 additions & 0 deletions docs/examples/instrumentation/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.egg_info/
6 changes: 6 additions & 0 deletions docs/examples/instrumentation/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.12
WORKDIR /usr/src/app
COPY . .
RUN python -m venv /venv && \
/venv/bin/pip install .
CMD bash -c "source /venv/bin/activate && opentelemetry-instrument flask run --host 0.0.0.0 2>&1 | tee /var/log/app.log"
17 changes: 17 additions & 0 deletions docs/examples/instrumentation/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import flask
import random
import requests
import time

app = flask.Flask(__name__, static_folder=None)

@app.route('/multi')
def multi():
for _ in range(random.randint(3, 7)):
requests.get(flask.url_for('single', _external=True))
return 'ok'

@app.route('/single')
def single():
time.sleep(random.uniform(0.1, 0.3))
return ''
13 changes: 13 additions & 0 deletions docs/examples/instrumentation/app/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "otel-quickstart-python"
version = "0.0.1"
dependencies = [
"flask",
"requests",
"opentelemetry-instrumentation-flask",
"opentelemetry-instrumentation-requests",
]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
21 changes: 21 additions & 0 deletions docs/examples/instrumentation/docker-compose.creds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use this compose file along with docker-compose.yaml to pass a credentials file from the host
# into the collector container:
#
# ```
# GOOGLE_CLOUD_PROJECT="otel-quickstart-demos" \
# GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/gcloud/application_default_credentials.json" \
# USERID="$(id -u)" \
# docker compose -f docker-compose.yaml -f docker-compose.creds.yaml up --abort-on-container-exit
# ```

version: "3"

services:
otelcol:
# If the collector does not have permission to read the mounted volumes, set
# USERID=$(id -u) to run the container as the current user
user: $USERID
volumes:
- ${GOOGLE_APPLICATION_CREDENTIALS?}:${GOOGLE_APPLICATION_CREDENTIALS}:ro
environment:
- GOOGLE_APPLICATION_CREDENTIALS
27 changes: 27 additions & 0 deletions docs/examples/instrumentation/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'

services:
app:
build: ./app
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otelcol:4318
- OTEL_SERVICE_NAME=otel-quickstart-python
volumes:
- logs:/var/log:rw
depends_on:
- "otelcol"
otelcol:
image: otel/opentelemetry-collector-contrib:0.92.0
volumes:
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml:ro
- logs:/var/log:ro
environment:
- GOOGLE_CLOUD_QUOTA_PROJECT
- GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT?}
loadgen:
image: golang:1.21
command: ["go", "run", "github.com/rakyll/hey@latest", "-c=2", "-q=1", "http://app:8080/multi"]
depends_on:
- "app"
volumes:
logs:
93 changes: 93 additions & 0 deletions docs/examples/instrumentation/otel-collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
receivers:
# Receive OTLP from our application
otlp:
protocols:
http:
endpoint: "0.0.0.0:4318"
# Use the filelog receiver to read our log from its log file.
filelog:
start_at: beginning
include:
- "/var/log/app.log"
operators:
- type: json_parser
parse_to: body
timestamp:
parse_from: body.timestamp
layout: '%Y-%m-%dT%H:%M:%S.%fZ'
severity:
parse_from: body.severity

# set trace_flags to SAMPLED if GCP attribute is set to true
- type: add
field: body.trace_flags
value: "01"
if: body["logging.googleapis.com/trace_sampled"] == true

# parse the trace context fields from GCP attributes
- type: regex_parser
parse_from: body["logging.googleapis.com/trace"]
parse_to: body
regex: (?P<trace_id>.*)
trace:
span_id:
parse_from: body["logging.googleapis.com/spanId"]

# Remove fields that are redundant from translation above
- type: remove
field: body.timestamp
- type: remove
field: body.trace_id
- type: remove
field: body.trace_flags
- type: remove
field: body.severity
- type: remove
field: body["logging.googleapis.com/trace"]
- type: remove
field: body["logging.googleapis.com/spanId"]
- type: remove
field: body["logging.googleapis.com/trace_sampled"]

exporters:
# Export logs and traces using the standard googelcloud exporter
googlecloud:
project: $GOOGLE_CLOUD_PROJECT
log:
default_log_name: "opentelemetry.io/collector-exported-log"
# Export metrics to Google Managed service for Prometheus
googlemanagedprometheus:
project: $GOOGLE_CLOUD_PROJECT

processors:
# Batch telemetry together to more efficiently send to GCP
batch:
send_batch_max_size: 500
send_batch_size: 500
timeout: 1s
# Make sure Google Managed service for Prometheus required labels are set
resource:
attributes:
- { key: "location", value: "us-central1", action: "upsert" }
- { key: "cluster", value: "no-cluster", action: "upsert" }
- { key: "namespace", value: "no-namespace", action: "upsert" }
- { key: "job", value: "us-job", action: "upsert" }
- { key: "instance", value: "us-instance", action: "upsert" }
# If running on GCP (e.g. on GKE), detect resource attributes from the environment.
resourcedetection:
detectors: ["env", "gcp"]

service:
pipelines:
traces:
receivers: ["otlp"]
processors: ["batch", "resourcedetection"]
exporters: ["googlecloud"]
metrics:
receivers: ["otlp"]
processors: ["batch", "resourcedetection", "resource"]
exporters: ["googlemanagedprometheus"]
logs:
receivers: ["filelog"]
processors: ["batch", "resourcedetection"]
exporters: ["googlecloud"]
Loading