Skip to content

Instantly share code, notes, and snippets.

@ashok-an
Last active November 9, 2022 14:19
Show Gist options
  • Save ashok-an/39c155a15fc6b6a5eb1a742405efc85f to your computer and use it in GitHub Desktop.
Save ashok-an/39c155a15fc6b6a5eb1a742405efc85f to your computer and use it in GitHub Desktop.
OTel collector example

How to test

 $ mkdir /tmp/outputs
 $ docker run -d -v ${PWD}/config.yaml:/etc/otelcol/config.yaml -v /tmp/outputs:/outputs -p 4317:4317 -p 4318:4318 -p 13133:13133 otel/opentelemetry-collector
 $ python trace.py
 $ cat /tmp/outputs/*.json
Sample output

{"resourceSpans":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"otel-collector-example"}}]},"scopeSpans":[{"scope":{"name":"__main__"},"spans":[{"traceId":"efdc9db3df8d42fc2ec2776e88dda227","spanId":"9845104eb9e7e64a","parentSpanId":"56592f1d9265bcfd","name":"dad","kind":1,"startTimeUnixNano":"1668003368355962000","endTimeUnixNano":"1668003368355999000","status":{}},{"traceId":"efdc9db3df8d42fc2ec2776e88dda227","spanId":"56592f1d9265bcfd","parentSpanId":"","name":"grand-dad","kind":1,"startTimeUnixNano":"1668003368355922000","endTimeUnixNano":"1668003368356014000","status":{}}]}]}]}

receivers:
otlp:
protocols:
grpc:
http:
processors:
batch:
resource:
attributes:
- key: environment
value: "stage"
action: upsert
exporters:
file:
path: /outputs/output.json
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [file]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [file]
backoff==2.2.1
certifi==2022.9.24
charset-normalizer==2.1.1
Deprecated==1.2.13
googleapis-common-protos==1.56.4
grpcio==1.50.0
idna==3.4
opentelemetry-api==1.14.0
opentelemetry-exporter-otlp==1.14.0
opentelemetry-exporter-otlp-proto-grpc==1.14.0
opentelemetry-exporter-otlp-proto-http==1.14.0
opentelemetry-exporter-prometheus==1.12.0rc1
opentelemetry-proto==1.14.0
opentelemetry-sdk==1.14.0
opentelemetry-semantic-conventions==0.35b0
prometheus-client==0.15.0
protobuf==3.20.3
requests==2.28.1
six==1.16.0
typing_extensions==4.4.0
urllib3==1.26.12
wrapt==1.14.1
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Resource can be required for some backends, e.g. Jaeger
# If resource wouldn't be set - traces wouldn't appears in Jaeger
resource = Resource(attributes={
"service.name": "otel-collector-example"
})
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)
otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
span_processor = BatchSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
with tracer.start_as_current_span("grand-dad"):
with tracer.start_as_current_span("dad"):
print("Hello world!")
@ashok-an
Copy link
Author

ashok-an commented Nov 9, 2022

Metrics exporter does not seem to work and there are no sufficient examples as of now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment