Open xwgao opened 11 months ago
@Badrmoh I've already imported the sitecustomize
, my code is as below. But still got the same result. I also tried removing the other lines from post_fork
(only reserve the first line which imports sitecustomize
), the result is the same.
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
def post_fork(server, worker):
from opentelemetry.instrumentation.auto_instrumentation import sitecustomize
server.log.info("Worker spawned (pid: %s)", worker.pid)
service_name = os.getenv("OTEL_SERVICE_NAME")
resource = Resource.create(attributes={
"service.name": service_name
})
trace.set_tracer_provider(TracerProvider(resource=resource))
otlp_endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
span_processor = BatchSpanProcessor(
OTLPSpanExporter(endpoint=otlp_endpoint)
)
trace.get_tracer_provider().add_span_processor(span_processor)
@Badrmoh Is there any update? Thanks a lot.
Sorry, I am not a maintainer. Hope you solve it.
Find some related configuration instructions https://grafana.com/docs/grafana-cloud/monitor-applications/application-observability/setup/instrument/python/ https://grafana.com/docs/grafana-cloud/monitor-applications/application-observability/setup/instrument/python/gunicorn/ py code in above doc
import logging
from uuid import uuid4
from opentelemetry import metrics, trace
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
OTLPMetricExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter,
)
# support for logs is currently experimental
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.resources import SERVICE_INSTANCE_ID
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# your gunicorn config here
# bind = "127.0.0.1:8000"
collector_endpoint = "http://localhost:4317"
def post_fork(server, worker):
server.log.info("Worker spawned (pid: %s)", worker.pid)
resource = Resource.create(
attributes={
# each worker needs a unique service.instance.id to distinguish the created metrics in prometheus
SERVICE_INSTANCE_ID: str(uuid4()),
"worker": worker.pid,
}
)
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(endpoint=collector_endpoint)))
trace.set_tracer_provider(tracer_provider)
metrics.set_meter_provider(
MeterProvider(
resource=resource,
metric_readers=[(PeriodicExportingMetricReader(
OTLPMetricExporter(endpoint=collector_endpoint)
))],
)
)
logger_provider = LoggerProvider(resource=resource)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(OTLPLogExporter(endpoint=collector_endpoint)))
logging.getLogger().addHandler(LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider))
@lzpfmh Thanks a lot for your comments. I tried with your code, the only difference is that I set the value collector_endpoint
to be http://otel-collector-headless:4317
which is the address of my OTEL collector service. But there is no traces exported to OTEL collector. Thanks.
Describe your environment Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main.
Environment:
Steps to reproduce Describe exactly how to reproduce the error. Include a code sample if applicable.
I created an OpenTelemetry collector as below.
gunicorn.conf.py
of my Python application.def post_fork(server, worker): from opentelemetry.instrumentation.auto_instrumentation import sitecustomize server.log.info("Worker spawned (pid: %s)", worker.pid) service_name = os.getenv("OTEL_SERVICE_NAME") resource = Resource.create(attributes={ "service.name": service_name })
[2023-12-08 06:58:08 +0000] [57] [ERROR] Exception in worker process Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker worker.init_process() File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/geventlet.py", line 143, in init_process super().init_process() File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process self.load_wsgi() File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi self.wsgi = self.app.wsgi() File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load return self.load_wsgiapp() File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp return util.import_app(self.app_uri) File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app mod = importlib.import_module(module) File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/opt/ibm/internalapi/internalapi.py", line 22, in
from api import usersAPI, workspacesAPI, applicationsAPI, metadataAPI, openidAPI, messageAPI, groupsAPI, datadictionaryAPI, manageworkspaceAPI, bindingsAPI, passwordPolicyAPI, idpsAPI
File "/opt/ibm/internalapi/api/init.py", line 15, in
from api.metadata import metadataAPI
File "/opt/ibm/internalapi/api/metadata.py", line 17, in
from managers import MetadataMgr
File "/opt/ibm/internalapi/managers/init.py", line 15, in
from managers.datadictionary import DataDictionaryMgr
File "/opt/ibm/internalapi/managers/datadictionary.py", line 41, in
k8sClient = k8sUtil.dynClient
File "/usr/local/lib/python3.9/site-packages/mas/utils/k8s/k8sUtil.py", line 105, in dynClient
self._dynClient = DynamicClient(k8s_client)
File "/usr/local/lib/python3.9/site-packages/openshift/dynamic/client.py", line 40, in init
K8sDynamicClient.init(self, client, cache_file=cache_file, discoverer=discoverer)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/client.py", line 84, in init
self.discoverer = discoverer(self, cache_file)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/discovery.py", line 228, in init
Discoverer.init(self, client, cache_file)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/discovery.py", line 54, in init
self.init_cache()
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/discovery.py", line 70, in init_cache
self._load_server_info()
File "/usr/local/lib/python3.9/site-packages/openshift/dynamic/discovery.py", line 98, in _load_server_info
'kubernetes': self.client.request('get', '/version', serializer=just_json)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/client.py", line 55, in inner
resp = func(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/client.py", line 270, in request
api_response = self.client.call_api(
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return self.call_api(resource_path, method,
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/api_client.py", line 180, in call_api
response_data = self.request(
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/api_client.py", line 373, in request
return self.rest_client.GET(url,
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/rest.py", line 241, in GET
return self.request("GET", url,
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/rest.py", line 214, in request
r = self.pool_manager.request(method, url,
File "/otel-auto-instrumentation-python/urllib3/_request_methods.py", line 110, in request
return self.request_encode_url(
File "/otel-auto-instrumentation-python/urllib3/_request_methods.py", line 143, in request_encode_url
return self.urlopen(method, url, extra_kw)
File "/otel-auto-instrumentation-python/urllib3/poolmanager.py", line 443, in urlopen
response = conn.urlopen(method, u.request_uri, kw)
File "/otel-auto-instrumentation-python/wrapt/wrappers.py", line 669, in call
return self._self_wrapper(self.wrapped, self._self_instance,
File "/otel-auto-instrumentation-python/opentelemetry/instrumentation/urllib3/init.py", line 243, in instrumented_urlopen
response = wrapped(*args, **kwargs)
File "/otel-auto-instrumentation-python/urllib3/connectionpool.py", line 790, in urlopen
response = self._make_request(
File "/otel-auto-instrumentation-python/urllib3/connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "/otel-auto-instrumentation-python/urllib3/connectionpool.py", line 1092, in _validate_conn
conn.connect()
File "/otel-auto-instrumentation-python/urllib3/connection.py", line 642, in connect
sock_and_verified = _ssl_wrap_socket_and_match_hostname(
File "/otel-auto-instrumentation-python/urllib3/connection.py", line 735, in _ssl_wrap_socket_and_match_hostname
context = create_urllib3context(
File "/otel-auto-instrumentation-python/urllib3/util/ssl.py", line 292, in create_urllib3_context
context.minimum_version = TLSVersion.TLSv1_2
File "/usr/local/lib/python3.9/ssl.py", line 587, in minimum_version
super(SSLContext, SSLContext).minimum_version.set(self, value)
File "/usr/local/lib/python3.9/ssl.py", line 587, in minimum_version
super(SSLContext, SSLContext).minimum_version.set(self, value)
File "/usr/local/lib/python3.9/ssl.py", line 587, in minimum_version
super(SSLContext, SSLContext).minimum_version.set(self, value)
[Previous line repeated 457 more times]
RecursionError: maximum recursion depth exceeded
[2023-12-08 06:58:08 +0000] [57] [INFO] Worker exiting (pid: 57)
CPU_LIMITS for INTERNALAPI: 2
[2023-12-08 06:58:08 +0000] [62] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/geventlet.py", line 143, in init_process
super().init_process()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/opt/ibm/internalapi/internalapi.py", line 22, in
from api import usersAPI, workspacesAPI, applicationsAPI, metadataAPI, openidAPI, messageAPI, groupsAPI, datadictionaryAPI, manageworkspaceAPI, bindingsAPI, passwordPolicyAPI, idpsAPI
File "/opt/ibm/internalapi/api/ init.py", line 15, in
from api.metadata import metadataAPI
File "/opt/ibm/internalapi/api/metadata.py", line 17, in
from managers import MetadataMgr
File "/opt/ibm/internalapi/managers/ init.py", line 15, in
from managers.datadictionary import DataDictionaryMgr
File "/opt/ibm/internalapi/managers/datadictionary.py", line 41, in
k8sClient = k8sUtil.dynClient
File "/usr/local/lib/python3.9/site-packages/mas/utils/k8s/k8sUtil.py", line 105, in dynClient
self._dynClient = DynamicClient(k8s_client)
File "/usr/local/lib/python3.9/site-packages/openshift/dynamic/client.py", line 40, in init
K8sDynamicClient.init(self, client, cache_file=cache_file, discoverer=discoverer)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/client.py", line 84, in init
self.discoverer = discoverer(self, cache_file)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/discovery.py", line 228, in init
Discoverer.init(self, client, cache_file)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/discovery.py", line 54, in init
self.init_cache()
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/discovery.py", line 70, in init_cache
self._load_server_info()
File "/usr/local/lib/python3.9/site-packages/openshift/dynamic/discovery.py", line 98, in _load_server_info
'kubernetes': self.client.request('get', '/version', serializer=just_json)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/client.py", line 55, in inner
resp = func(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/kubernetes/dynamic/client.py", line 270, in request
api_response = self.client.call_api(
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return self.call_api(resource_path, method,
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/api_client.py", line 180, in call_api
response_data = self.request(
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/api_client.py", line 373, in request
return self.rest_client.GET(url,
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/rest.py", line 241, in GET
return self.request("GET", url,
File "/usr/local/lib/python3.9/site-packages/kubernetes/client/rest.py", line 214, in request
r = self.pool_manager.request(method, url,
File "/otel-auto-instrumentation-python/urllib3/_request_methods.py", line 110, in request
return self.request_encode_url(
File "/otel-auto-instrumentation-python/urllib3/_request_methods.py", line 143, in request_encode_url
return self.urlopen(method, url, extra_kw)
File "/otel-auto-instrumentation-python/urllib3/poolmanager.py", line 443, in urlopen
response = conn.urlopen(method, u.request_uri, kw)
File "/otel-auto-instrumentation-python/wrapt/wrappers.py", line 669, in call
return self._self_wrapper(self.wrapped, self._self_instance,
File "/otel-auto-instrumentation-python/opentelemetry/instrumentation/urllib3/init.py", line 243, in instrumented_urlopen
response = wrapped(*args, **kwargs)
File "/otel-auto-instrumentation-python/urllib3/connectionpool.py", line 790, in urlopen
response = self._make_request(
File "/otel-auto-instrumentation-python/urllib3/connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "/otel-auto-instrumentation-python/urllib3/connectionpool.py", line 1092, in _validate_conn
conn.connect()
File "/otel-auto-instrumentation-python/urllib3/connection.py", line 642, in connect
sock_and_verified = _ssl_wrap_socket_and_match_hostname(
File "/otel-auto-instrumentation-python/urllib3/connection.py", line 735, in _ssl_wrap_socket_and_match_hostname
context = create_urllib3context(
File "/otel-auto-instrumentation-python/urllib3/util/ssl.py", line 292, in create_urllib3_context
context.minimum_version = TLSVersion.TLSv1_2
File "/usr/local/lib/python3.9/ssl.py", line 587, in minimum_version
super(SSLContext, SSLContext).minimum_version.set(self, value)
File "/usr/local/lib/python3.9/ssl.py", line 587, in minimum_version
super(SSLContext, SSLContext).minimum_version.set(self, value)
File "/usr/local/lib/python3.9/ssl.py", line 587, in minimum_version
super(SSLContext, SSLContext).minimum_version.set(self, value)
[Previous line repeated 457 more times]
RecursionError: maximum recursion depth exceeded