Skip to content

Commit 3559c67

Browse files
authored
Merge branch 'main' into fix/client-address
2 parents d1a6c52 + 76e614f commit 3559c67

File tree

6 files changed

+94
-21
lines changed
  • instrumentation
    • opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore
    • opentelemetry-instrumentation-confluent-kafka/src/opentelemetry/instrumentation/confluent_kafka
    • opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch
    • opentelemetry-instrumentation-falcon
    • opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging

6 files changed

+94
-21
lines changed

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
.. code:: python
2929
3030
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
31-
import botocore
31+
import botocore.session
3232
3333
3434
# Instrument Botocore
@@ -39,7 +39,7 @@
3939
session.set_credentials(
4040
access_key="access-key", secret_key="secret-key"
4141
)
42-
ec2 = self.session.create_client("ec2", region_name="us-west-2")
42+
ec2 = session.create_client("ec2", region_name="us-west-2")
4343
ec2.describe_instances()
4444
4545
API
@@ -58,13 +58,15 @@
5858
.. code: python
5959
6060
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
61-
import botocore
61+
import botocore.session
6262
6363
def request_hook(span, service_name, operation_name, api_params):
6464
# request hook logic
65+
pass
6566
6667
def response_hook(span, service_name, operation_name, result):
6768
# response hook logic
69+
pass
6870
6971
# Instrument Botocore with hooks
7072
BotocoreInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
@@ -74,7 +76,7 @@ def response_hook(span, service_name, operation_name, result):
7476
session.set_credentials(
7577
access_key="access-key", secret_key="secret-key"
7678
)
77-
ec2 = self.session.create_client("ec2", region_name="us-west-2")
79+
ec2 = session.create_client("ec2", region_name="us-west-2")
7880
ec2.describe_instances()
7981
8082
Extensions

instrumentation/opentelemetry-instrumentation-confluent-kafka/src/opentelemetry/instrumentation/confluent_kafka/__init__.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
# report a span of type consumer with the default settings
3535
consumer = Consumer(conf2)
3636
37+
def msg_process(msg):
38+
print(msg)
39+
3740
def basic_consume_loop(consumer, topics):
3841
try:
3942
consumer.subscribe(topics)
@@ -44,7 +47,7 @@ def basic_consume_loop(consumer, topics):
4447
if msg.error():
4548
if msg.error().code() == KafkaError._PARTITION_EOF:
4649
# End of partition event
47-
sys.stderr.write(f"{msg.topic() [{msg.partition()}] reached end at offset {msg.offset()}}")
50+
sys.stderr.write(f"{msg.topic()} [{msg.partition()}] reached end at offset {msg.offset()}")
4851
elif msg.error():
4952
raise KafkaException(msg.error())
5053
else:
@@ -53,7 +56,7 @@ def basic_consume_loop(consumer, topics):
5356
# Close down consumer to commit final offsets.
5457
consumer.close()
5558
56-
basic_consume_loop(consumer, "my-topic")
59+
basic_consume_loop(consumer, ["my-topic"])
5760
5861
The _instrument method accepts the following keyword args:
5962
tracer_provider (TracerProvider) - an optional tracer provider
@@ -72,14 +75,16 @@ def instrument_consumer(consumer: Consumer, tracer_provider=None)
7275
.. code:: python
7376
7477
from opentelemetry.instrumentation.confluent_kafka import ConfluentKafkaInstrumentor
78+
from opentelemetry.trace import get_tracer_provider
7579
7680
from confluent_kafka import Producer, Consumer
7781
7882
inst = ConfluentKafkaInstrumentor()
83+
tracer_provider = get_tracer_provider()
7984
80-
p = confluent_kafka.Producer({'bootstrap.servers': 'localhost:29092'})
81-
c = confluent_kafka.Consumer({
82-
'bootstrap.servers': 'localhost:29092',
85+
p = Producer({'bootstrap.servers': 'localhost:9092'})
86+
c = Consumer({
87+
'bootstrap.servers': 'localhost:9092',
8388
'group.id': 'mygroup',
8489
'auto.offset.reset': 'earliest'
8590
})

instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/__init__.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
3333
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
3434
import elasticsearch
35-
35+
from datetime import datetime
3636
3737
# instrument elasticsearch
3838
ElasticsearchInstrumentor().instrument()
3939
4040
# Using elasticsearch as normal now will automatically generate spans
4141
es = elasticsearch.Elasticsearch()
42-
es.index(index='my-index', doc_type='my-type', id=1, body={'my': 'data', 'timestamp': datetime.now()})
43-
es.get(index='my-index', doc_type='my-type', id=1)
42+
es.index(index='my-index', doc_type='_doc', id=1, body={'my': 'data', 'timestamp': datetime.now()})
43+
es.get(index='my-index', doc_type='_doc', id=1)
4444
4545
Elasticsearch instrumentation prefixes operation names with the string "Elasticsearch". This
4646
can be changed to a different string by either setting the OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX
@@ -49,6 +49,8 @@
4949
5050
.. code-block:: python
5151
52+
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
53+
5254
ElasticsearchInstrumentor("my-custom-prefix").instrument()
5355
5456
The instrument() method accepts the following keyword args:
@@ -67,6 +69,7 @@ def response_hook(span: Span, response: dict)
6769
6870
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
6971
import elasticsearch
72+
from datetime import datetime
7073
7174
def request_hook(span, method, url, kwargs):
7275
if span and span.is_recording():
@@ -82,8 +85,8 @@ def response_hook(span, response):
8285
# Using elasticsearch as normal now will automatically generate spans,
8386
# including user custom attributes added from the hooks
8487
es = elasticsearch.Elasticsearch()
85-
es.index(index='my-index', doc_type='my-type', id=1, body={'my': 'data', 'timestamp': datetime.now()})
86-
es.get(index='my-index', doc_type='my-type', id=1)
88+
es.index(index='my-index', doc_type='_doc', id=1, body={'my': 'data', 'timestamp': datetime.now()})
89+
es.get(index='my-index', doc_type='_doc', id=1)
8790
8891
API
8992
---

instrumentation/opentelemetry-instrumentation-falcon/README.rst

+22-2
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,41 @@ will extract path_info and content_type attributes from every traced request and
4747

4848
Falcon Request object reference: https://falcon.readthedocs.io/en/stable/api/request_and_response.html#id1
4949

50+
Usage
51+
-----
52+
53+
.. code-block:: python
54+
55+
import falcon
56+
from opentelemetry.instrumentation.falcon import FalconInstrumentor
57+
58+
FalconInstrumentor().instrument()
59+
60+
app = falcon.App()
61+
62+
class HelloWorldResource(object):
63+
def on_get(self, req, resp):
64+
resp.text = 'Hello World'
65+
66+
app.add_route('/hello', HelloWorldResource())
67+
5068
5169
Request/Response hooks
5270
**********************
5371
The instrumentation supports specifying request and response hooks. These are functions that get called back by the instrumentation right after a Span is created for a request
5472
and right before the span is finished while processing a response. The hooks can be configured as follows:
5573

56-
::
74+
.. code-block:: python
75+
76+
from opentelemetry.instrumentation.falcon import FalconInstrumentor
5777
5878
def request_hook(span, req):
5979
pass
6080
6181
def response_hook(span, req, resp):
6282
pass
6383
64-
FalconInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)
84+
FalconInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
6585
6686
References
6787
----------

instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@
5858
5959
.. code-block:: python
6060
61-
from falcon import API
61+
import falcon
6262
from opentelemetry.instrumentation.falcon import FalconInstrumentor
6363
6464
FalconInstrumentor().instrument()
6565
66-
app = falcon.API()
66+
app = falcon.App()
6767
6868
class HelloWorldResource(object):
6969
def on_get(self, req, resp):
70-
resp.body = 'Hello World'
70+
resp.text = 'Hello World'
7171
7272
app.add_route('/hello', HelloWorldResource())
7373
@@ -78,15 +78,17 @@ def on_get(self, req, resp):
7878
right after a span is created for a request and right before the span is finished for the response.
7979
The hooks can be configured as follows:
8080
81-
::
81+
.. code-block:: python
82+
83+
from opentelemetry.instrumentation.falcon import FalconInstrumentor
8284
8385
def request_hook(span, req):
8486
pass
8587
8688
def response_hook(span, req, resp):
8789
pass
8890
89-
FalconInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)
91+
FalconInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
9092
9193
Capture HTTP request and response headers
9294
*****************************************

instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py

+41
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,47 @@
1414

1515
# pylint: disable=empty-docstring,no-value-for-parameter,no-member,no-name-in-module
1616

17+
"""
18+
The OpenTelemetry `logging` integration automatically injects tracing context into
19+
log statements, though it is opt-in and must be enabled explicitly by setting the
20+
environment variable `OTEL_PYTHON_LOG_CORRELATION` to `true`.
21+
22+
.. code-block:: python
23+
24+
import logging
25+
26+
from opentelemetry.instrumentation.logging import LoggingInstrumentor
27+
28+
LoggingInstrumentor().instrument()
29+
30+
logging.warning('OTel test')
31+
32+
When running the above example you will see the following output:
33+
34+
::
35+
36+
2025-03-05 09:40:04,398 WARNING [root] [example.py:7] [trace_id=0 span_id=0 resource.service.name= trace_sampled=False] - OTel test
37+
38+
The environment variable `OTEL_PYTHON_LOG_CORRELATION` must be set to `true`
39+
in order to enable trace context injection into logs by calling
40+
`logging.basicConfig()` and setting a logging format that makes use of the
41+
injected tracing variables.
42+
43+
Alternatively, `set_logging_format` argument can be set to `True` when
44+
initializing the `LoggingInstrumentor` class to achieve the same effect:
45+
46+
.. code-block:: python
47+
48+
import logging
49+
50+
from opentelemetry.instrumentation.logging import LoggingInstrumentor
51+
52+
LoggingInstrumentor().instrument(set_logging_format=True)
53+
54+
logging.warning('OTel test')
55+
56+
"""
57+
1758
import logging # pylint: disable=import-self
1859
from os import environ
1960
from typing import Collection

0 commit comments

Comments
 (0)