From 5b37676df81af080e03ddbfecc16216c39988080 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sun, 14 Feb 2021 15:26:14 +0530 Subject: [PATCH 01/10] Add how to work with fork process models --- docs/examples/fork-process-model/README.rst | 48 ++++++++++++++++ .../flask-gunicorn/README.rst | 11 ++++ .../fork-process-model/flask-gunicorn/app.py | 44 +++++++++++++++ .../flask-gunicorn/gunicorn.config.py | 30 ++++++++++ .../flask-gunicorn/requirements.txt | 20 +++++++ .../fork-process-model/flask-uwsgi/README.rst | 12 ++++ .../fork-process-model/flask-uwsgi/app.py | 56 +++++++++++++++++++ .../flask-uwsgi/requirements.txt | 20 +++++++ 8 files changed, 241 insertions(+) create mode 100644 docs/examples/fork-process-model/README.rst create mode 100644 docs/examples/fork-process-model/flask-gunicorn/README.rst create mode 100644 docs/examples/fork-process-model/flask-gunicorn/app.py create mode 100644 docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py create mode 100644 docs/examples/fork-process-model/flask-gunicorn/requirements.txt create mode 100644 docs/examples/fork-process-model/flask-uwsgi/README.rst create mode 100644 docs/examples/fork-process-model/flask-uwsgi/app.py create mode 100644 docs/examples/fork-process-model/flask-uwsgi/requirements.txt diff --git a/docs/examples/fork-process-model/README.rst b/docs/examples/fork-process-model/README.rst new file mode 100644 index 00000000000..ce0f56433ce --- /dev/null +++ b/docs/examples/fork-process-model/README.rst @@ -0,0 +1,48 @@ +Working With Fork Process Models +================================ + +The `BatchExportSpanProcessor` is not fork-safe and doesn't work well with application servers +(Gunicorn, uWSGI) which are based on pre-fork web server model. The `BatchExportSpanProcessor` +spawns thread to run in the background to export spans to telemetry backend. During the fork, child +process inherits the lock which is held by parent process and deadlock occurs. We can use fork hooks to +get around this limitation of span processor. + +Please see the http://bugs.python.org/issue6721 for the problems about Python locks in (multi)threaded +context with fork. + +Gunicorn post_fork hook +----------------------- + +.. code-block:: python + + from opentelemetry import trace + from opentelemetry.exporter.jaeger import JaegerSpanExporter + from opentelemetry.sdk.trace import TracerProvider + from opentelemetry.sdk.trace.export import BatchExportSpanProcessor + + def post_fork(server, worker): + server.log.info("Worker spawned (pid: %s)", worker.pid) + trace.set_tracer_provider(TracerProvider()) + trace.get_tracer_provider().add_span_processor( + BatchExportSpanProcessor(JaegerSpanExporter(service_name='my-service')) + ) + + +uWSGI postfork decorator +------------------------ + +.. code-block:: python + + from opentelemetry import trace + from opentelemetry.exporter.jaeger import JaegerSpanExporter + from opentelemetry.sdk.trace.export import BatchExportSpanProcessor + + + @postfork + def set_span_processor(): + trace.get_tracer_provider().add_span_processor( + BatchExportSpanProcessor(JaegerSpanExporter(service_name='my-service')) + ) + + +The source code for the examples with Flask app are available :scm_web:`here `. diff --git a/docs/examples/fork-process-model/flask-gunicorn/README.rst b/docs/examples/fork-process-model/flask-gunicorn/README.rst new file mode 100644 index 00000000000..ea80905a8b7 --- /dev/null +++ b/docs/examples/fork-process-model/flask-gunicorn/README.rst @@ -0,0 +1,11 @@ +Installation +------------ +.. code-block:: sh + + pip install -rrequirements.txt + +Run application +--------------- +.. code-block:: sh + + gunicorn flask-app -c gunicorn.config.py \ No newline at end of file diff --git a/docs/examples/fork-process-model/flask-gunicorn/app.py b/docs/examples/fork-process-model/flask-gunicorn/app.py new file mode 100644 index 00000000000..b72b0c0d9bf --- /dev/null +++ b/docs/examples/fork-process-model/flask-gunicorn/app.py @@ -0,0 +1,44 @@ +import flask +from flask import request + +from opentelemetry import trace +from opentelemetry.instrumentation.flask import FlaskInstrumentor + +application = flask.Flask(__name__) + +FlaskInstrumentor().instrument_app(application) + + +def fib_slow(n): + if n <= 1: + return n + return fib_slow(n - 1) + fib_fast(n - 2) + + +def fib_fast(n): + nth_fib = [0] * (n + 2) + nth_fib[1] = 1 + for i in range(2, n + 1): + nth_fib[i] = nth_fib[i - 1] + nth_fib[i - 2] + return nth_fib[n] + + +@application.route("/fibonacci") +def fibonacci(): + tracer = trace.get_tracer(__name__) + n = int(request.args.get("n", 1)) + with tracer.start_as_current_span("root"): + with tracer.start_as_current_span("fib_slow") as slow_span: + ans = fib_slow(n) + slow_span.set_attribute("n", n) + slow_span.set_attribute("nth_fibonacci", ans) + with tracer.start_as_current_span("fib_fast") as fast_span: + ans = fib_fast(n) + fast_span.set_attribute("n", n) + fast_span.set_attribute("nth_fibonacci", ans) + + return "Hello" + + +if __name__ == "__main__": + application.run() diff --git a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py new file mode 100644 index 00000000000..2504d02cf2e --- /dev/null +++ b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py @@ -0,0 +1,30 @@ +from opentelemetry import trace +from opentelemetry.exporter.jaeger import JaegerSpanExporter +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchExportSpanProcessor + + +bind = "127.0.0.1:8000" + +# Sample Worker processes +workers = 4 +worker_class = "sync" +worker_connections = 1000 +timeout = 30 +keepalive = 2 + +# Sample logging +errorlog = "-" +loglevel = "info" +accesslog = "-" +access_log_format = ( + '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' +) + + +def post_fork(server, worker): + server.log.info("Worker spawned (pid: %s)", worker.pid) + trace.set_tracer_provider(TracerProvider()) + trace.get_tracer_provider().add_span_processor( + BatchExportSpanProcessor(JaegerSpanExporter(service_name="my-service")) + ) diff --git a/docs/examples/fork-process-model/flask-gunicorn/requirements.txt b/docs/examples/fork-process-model/flask-gunicorn/requirements.txt new file mode 100644 index 00000000000..2b8c147bcce --- /dev/null +++ b/docs/examples/fork-process-model/flask-gunicorn/requirements.txt @@ -0,0 +1,20 @@ +click==7.1.2 +Flask==1.1.2 +googleapis-common-protos==1.52.0 +grpcio==1.35.0 +gunicorn==20.0.4 +itsdangerous==1.1.0 +Jinja2==2.11.3 +MarkupSafe==1.1.1 +opentelemetry-api==0.17b0 +opentelemetry-exporter-jaeger==0.17b0 +opentelemetry-instrumentation==0.17b0 +opentelemetry-instrumentation-flask==0.17b0 +opentelemetry-instrumentation-wsgi==0.17b0 +opentelemetry-sdk==0.17b0 +protobuf==3.14.0 +six==1.15.0 +thrift==0.13.0 +uWSGI==2.0.19.1 +Werkzeug==1.0.1 +wrapt==1.12.1 diff --git a/docs/examples/fork-process-model/flask-uwsgi/README.rst b/docs/examples/fork-process-model/flask-uwsgi/README.rst new file mode 100644 index 00000000000..055b488bd7a --- /dev/null +++ b/docs/examples/fork-process-model/flask-uwsgi/README.rst @@ -0,0 +1,12 @@ +Installation +------------ +.. code-block:: sh + + pip install -rrequirements.txt + +Run application +---------------------- + +.. code-block:: sh + + uwsgi --http :8000 --wsgi-file app.py --callable application --master --enable-threads \ No newline at end of file diff --git a/docs/examples/fork-process-model/flask-uwsgi/app.py b/docs/examples/fork-process-model/flask-uwsgi/app.py new file mode 100644 index 00000000000..f70d27855f2 --- /dev/null +++ b/docs/examples/fork-process-model/flask-uwsgi/app.py @@ -0,0 +1,56 @@ +import flask +from flask import request +from uwsgidecorators import postfork +from opentelemetry import trace +from opentelemetry.exporter.jaeger import JaegerSpanExporter +from opentelemetry.instrumentation.flask import FlaskInstrumentor +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchExportSpanProcessor + +trace.set_tracer_provider(TracerProvider()) + +application = flask.Flask(__name__) + +FlaskInstrumentor().instrument_app(application) +tracer = trace.get_tracer(__name__) + + +@postfork +def set_span_processor(): + trace.get_tracer_provider().add_span_processor( + BatchExportSpanProcessor(JaegerSpanExporter(service_name="my-service")) + ) + + +def fib_slow(n): + if n <= 1: + return n + return fib_slow(n - 1) + fib_fast(n - 2) + + +def fib_fast(n): + nth_fib = [0] * (n + 2) + nth_fib[1] = 1 + for i in range(2, n + 1): + nth_fib[i] = nth_fib[i - 1] + nth_fib[i - 2] + return nth_fib[n] + + +@application.route("/fibonacci") +def fibonacci(): + n = int(request.args.get("n", 1)) + with tracer.start_as_current_span("root"): + with tracer.start_as_current_span("fib_slow") as slow_span: + ans = fib_slow(n) + slow_span.set_attribute("n", n) + slow_span.set_attribute("nth_fibonacci", ans) + with tracer.start_as_current_span("fib_fast") as fast_span: + ans = fib_fast(n) + fast_span.set_attribute("n", n) + fast_span.set_attribute("nth_fibonacci", ans) + + return "Hello" + + +if __name__ == "__main__": + application.run() diff --git a/docs/examples/fork-process-model/flask-uwsgi/requirements.txt b/docs/examples/fork-process-model/flask-uwsgi/requirements.txt new file mode 100644 index 00000000000..2b8c147bcce --- /dev/null +++ b/docs/examples/fork-process-model/flask-uwsgi/requirements.txt @@ -0,0 +1,20 @@ +click==7.1.2 +Flask==1.1.2 +googleapis-common-protos==1.52.0 +grpcio==1.35.0 +gunicorn==20.0.4 +itsdangerous==1.1.0 +Jinja2==2.11.3 +MarkupSafe==1.1.1 +opentelemetry-api==0.17b0 +opentelemetry-exporter-jaeger==0.17b0 +opentelemetry-instrumentation==0.17b0 +opentelemetry-instrumentation-flask==0.17b0 +opentelemetry-instrumentation-wsgi==0.17b0 +opentelemetry-sdk==0.17b0 +protobuf==3.14.0 +six==1.15.0 +thrift==0.13.0 +uWSGI==2.0.19.1 +Werkzeug==1.0.1 +wrapt==1.12.1 From 1aa907baf8418c702f5e093832e5dd282e1c14c2 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sun, 14 Feb 2021 17:05:29 +0530 Subject: [PATCH 02/10] Fix lint --- docs/examples/fork-process-model/README.rst | 1 + .../fork-process-model/flask-gunicorn/gunicorn.config.py | 1 - docs/examples/fork-process-model/flask-uwsgi/README.rst | 2 +- docs/examples/fork-process-model/flask-uwsgi/app.py | 1 + 4 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/examples/fork-process-model/README.rst b/docs/examples/fork-process-model/README.rst index ce0f56433ce..a65189f3924 100644 --- a/docs/examples/fork-process-model/README.rst +++ b/docs/examples/fork-process-model/README.rst @@ -20,6 +20,7 @@ Gunicorn post_fork hook from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor + def post_fork(server, worker): server.log.info("Worker spawned (pid: %s)", worker.pid) trace.set_tracer_provider(TracerProvider()) diff --git a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py index 2504d02cf2e..f147475196f 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py +++ b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py @@ -3,7 +3,6 @@ from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor - bind = "127.0.0.1:8000" # Sample Worker processes diff --git a/docs/examples/fork-process-model/flask-uwsgi/README.rst b/docs/examples/fork-process-model/flask-uwsgi/README.rst index 055b488bd7a..0cf947245f4 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/README.rst +++ b/docs/examples/fork-process-model/flask-uwsgi/README.rst @@ -5,7 +5,7 @@ Installation pip install -rrequirements.txt Run application ----------------------- +--------------- .. code-block:: sh diff --git a/docs/examples/fork-process-model/flask-uwsgi/app.py b/docs/examples/fork-process-model/flask-uwsgi/app.py index f70d27855f2..7261c97b442 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/app.py +++ b/docs/examples/fork-process-model/flask-uwsgi/app.py @@ -1,6 +1,7 @@ import flask from flask import request from uwsgidecorators import postfork + from opentelemetry import trace from opentelemetry.exporter.jaeger import JaegerSpanExporter from opentelemetry.instrumentation.flask import FlaskInstrumentor From 69af51375379b7b200ed98b18b153918ad313c05 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sun, 14 Feb 2021 18:12:28 +0530 Subject: [PATCH 03/10] Update README --- docs/examples/fork-process-model/flask-gunicorn/README.rst | 4 +++- docs/examples/fork-process-model/flask-uwsgi/README.rst | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/examples/fork-process-model/flask-gunicorn/README.rst b/docs/examples/fork-process-model/flask-gunicorn/README.rst index ea80905a8b7..008512e56b1 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/README.rst +++ b/docs/examples/fork-process-model/flask-gunicorn/README.rst @@ -8,4 +8,6 @@ Run application --------------- .. code-block:: sh - gunicorn flask-app -c gunicorn.config.py \ No newline at end of file + gunicorn app -c gunicorn.config.py + +Please make sure Jaeger is running locally and the default agent host and port are exposed. diff --git a/docs/examples/fork-process-model/flask-uwsgi/README.rst b/docs/examples/fork-process-model/flask-uwsgi/README.rst index 0cf947245f4..54a5a68696a 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/README.rst +++ b/docs/examples/fork-process-model/flask-uwsgi/README.rst @@ -9,4 +9,6 @@ Run application .. code-block:: sh - uwsgi --http :8000 --wsgi-file app.py --callable application --master --enable-threads \ No newline at end of file + uwsgi --http :8000 --wsgi-file app.py --callable application --master --enable-threads + +Please make sure Jaeger is running locally and the default agent host and port are exposed. From 88524e3b3029a6aa5816602e280625d28c8a3628 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sun, 14 Feb 2021 18:28:05 +0530 Subject: [PATCH 04/10] Update README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 810e94a8092..918d98a5269 100644 --- a/README.md +++ b/README.md @@ -98,11 +98,15 @@ machine. 1. Install scalene using the following command -`pip install scalene` +```sh +pip install scalene +``` 2. Run the `scalene` tests on any of the example Python programs -`scalene opentelemetry-/tests/performance/resource-usage//profile_resource_usage_.py` +```sh +scalene opentelemetry-/tests/performance/resource-usage//profile_resource_usage_.py +``` ## Documentation From b98898ce82f33af18030b6e29d9792b420ad18bb Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 17 Feb 2021 09:11:20 +0530 Subject: [PATCH 05/10] Add CHANGELOG entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcb8d65d4b8..905e5a36943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.18b0...HEAD) +### Added +- Document how to work with fork process web server models(Gunicorn, uWSGI etc...) + ([#1609](https://github.com/open-telemetry/opentelemetry-python/pull/1609)) + ## [0.18b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.18b0) - 2021-02-16 ### Added From 3aa01aff1f9f82aecfd09ee672d5b9ffc8b072d4 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 18 Feb 2021 07:39:10 +0530 Subject: [PATCH 06/10] Apply suggestions from code review Co-authored-by: Diego Hurtado --- docs/examples/fork-process-model/README.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/examples/fork-process-model/README.rst b/docs/examples/fork-process-model/README.rst index a65189f3924..dc8d93eb8bd 100644 --- a/docs/examples/fork-process-model/README.rst +++ b/docs/examples/fork-process-model/README.rst @@ -2,12 +2,12 @@ Working With Fork Process Models ================================ The `BatchExportSpanProcessor` is not fork-safe and doesn't work well with application servers -(Gunicorn, uWSGI) which are based on pre-fork web server model. The `BatchExportSpanProcessor` -spawns thread to run in the background to export spans to telemetry backend. During the fork, child -process inherits the lock which is held by parent process and deadlock occurs. We can use fork hooks to -get around this limitation of span processor. +(Gunicorn, uWSGI) which are based on the pre-fork web server model. The `BatchExportSpanProcessor` +spawns a thread to run in the background to export spans to the telemetry backend. During the fork, the child +process inherits the lock which is held by the parent process and deadlock occurs. We can use fork hooks to +get around this limitation of the span processor. -Please see the http://bugs.python.org/issue6721 for the problems about Python locks in (multi)threaded +Please see http://bugs.python.org/issue6721 for the problems about Python locks in (multi)threaded context with fork. Gunicorn post_fork hook From 98d1a309e3cb7bd812c8861fcf719aa3f4abd5a9 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 18 Feb 2021 07:48:55 +0530 Subject: [PATCH 07/10] Add License --- .../fork-process-model/flask-gunicorn/app.py | 14 ++++++++++++++ .../flask-gunicorn/gunicorn.config.py | 14 ++++++++++++++ .../examples/fork-process-model/flask-uwsgi/app.py | 14 ++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/docs/examples/fork-process-model/flask-gunicorn/app.py b/docs/examples/fork-process-model/flask-gunicorn/app.py index b72b0c0d9bf..61218a9f622 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/app.py +++ b/docs/examples/fork-process-model/flask-gunicorn/app.py @@ -1,3 +1,17 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import flask from flask import request diff --git a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py index f147475196f..85412ba675c 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py +++ b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py @@ -1,3 +1,17 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from opentelemetry import trace from opentelemetry.exporter.jaeger import JaegerSpanExporter from opentelemetry.sdk.trace import TracerProvider diff --git a/docs/examples/fork-process-model/flask-uwsgi/app.py b/docs/examples/fork-process-model/flask-uwsgi/app.py index 7261c97b442..44bd4893edf 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/app.py +++ b/docs/examples/fork-process-model/flask-uwsgi/app.py @@ -1,3 +1,17 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import flask from flask import request from uwsgidecorators import postfork From 00609b2bff06a4204bbe4ab6700b57709538d416 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 18 Feb 2021 07:49:23 +0530 Subject: [PATCH 08/10] Add @postfork import --- docs/examples/fork-process-model/README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/examples/fork-process-model/README.rst b/docs/examples/fork-process-model/README.rst index a65189f3924..af8546b3eaa 100644 --- a/docs/examples/fork-process-model/README.rst +++ b/docs/examples/fork-process-model/README.rst @@ -34,6 +34,8 @@ uWSGI postfork decorator .. code-block:: python + from uwsgidecorators import postfork + from opentelemetry import trace from opentelemetry.exporter.jaeger import JaegerSpanExporter from opentelemetry.sdk.trace.export import BatchExportSpanProcessor From 89e8f2802faeac1ad102970758b5189137b249c4 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 24 Feb 2021 01:36:28 +0530 Subject: [PATCH 09/10] Use OTLP; Update documentation --- docs/examples/fork-process-model/README.rst | 31 ++++++++++++++----- .../flask-gunicorn/README.rst | 2 -- .../fork-process-model/flask-gunicorn/app.py | 2 +- .../flask-gunicorn/gunicorn.config.py | 15 ++++++--- .../flask-gunicorn/requirements.txt | 2 +- .../fork-process-model/flask-uwsgi/README.rst | 2 -- .../fork-process-model/flask-uwsgi/app.py | 21 ++++++++----- .../flask-uwsgi/requirements.txt | 2 +- 8 files changed, 50 insertions(+), 27 deletions(-) diff --git a/docs/examples/fork-process-model/README.rst b/docs/examples/fork-process-model/README.rst index f13fb6a2278..ae8122dca92 100644 --- a/docs/examples/fork-process-model/README.rst +++ b/docs/examples/fork-process-model/README.rst @@ -16,17 +16,24 @@ Gunicorn post_fork hook .. code-block:: python from opentelemetry import trace - from opentelemetry.exporter.jaeger import JaegerSpanExporter + from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter + from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor def post_fork(server, worker): server.log.info("Worker spawned (pid: %s)", worker.pid) - trace.set_tracer_provider(TracerProvider()) - trace.get_tracer_provider().add_span_processor( - BatchExportSpanProcessor(JaegerSpanExporter(service_name='my-service')) + + resource = Resource.create(attributes={ + "service.name": "api-service" + }) + + trace.set_tracer_provider(TracerProvider(resource=resource)) + span_processor = BatchExportSpanProcessor( + OTLPSpanExporter(endpoint="localhost:4317") ) + trace.get_tracer_provider().add_span_processor(span_processor) uWSGI postfork decorator @@ -37,15 +44,23 @@ uWSGI postfork decorator from uwsgidecorators import postfork from opentelemetry import trace - from opentelemetry.exporter.jaeger import JaegerSpanExporter + from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter + from opentelemetry.sdk.resources import Resource + from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor @postfork - def set_span_processor(): - trace.get_tracer_provider().add_span_processor( - BatchExportSpanProcessor(JaegerSpanExporter(service_name='my-service')) + def init_tracing(): + resource = Resource.create(attributes={ + "service.name": "api-service" + }) + + trace.set_tracer_provider(TracerProvider(resource=resource)) + span_processor = BatchExportSpanProcessor( + OTLPSpanExporter(endpoint="localhost:4317") ) + trace.get_tracer_provider().add_span_processor(span_processor) The source code for the examples with Flask app are available :scm_web:`here `. diff --git a/docs/examples/fork-process-model/flask-gunicorn/README.rst b/docs/examples/fork-process-model/flask-gunicorn/README.rst index 008512e56b1..ec7a2e9e606 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/README.rst +++ b/docs/examples/fork-process-model/flask-gunicorn/README.rst @@ -9,5 +9,3 @@ Run application .. code-block:: sh gunicorn app -c gunicorn.config.py - -Please make sure Jaeger is running locally and the default agent host and port are exposed. diff --git a/docs/examples/fork-process-model/flask-gunicorn/app.py b/docs/examples/fork-process-model/flask-gunicorn/app.py index 61218a9f622..e8ea6a1dea7 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/app.py +++ b/docs/examples/fork-process-model/flask-gunicorn/app.py @@ -51,7 +51,7 @@ def fibonacci(): fast_span.set_attribute("n", n) fast_span.set_attribute("nth_fibonacci", ans) - return "Hello" + return "F({}) is: ({})".format(n, ans) if __name__ == "__main__": diff --git a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py index 85412ba675c..51eff04e02b 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py +++ b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py @@ -13,7 +13,8 @@ # limitations under the License. from opentelemetry import trace -from opentelemetry.exporter.jaeger import JaegerSpanExporter +from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter +from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor @@ -37,7 +38,13 @@ def post_fork(server, worker): server.log.info("Worker spawned (pid: %s)", worker.pid) - trace.set_tracer_provider(TracerProvider()) - trace.get_tracer_provider().add_span_processor( - BatchExportSpanProcessor(JaegerSpanExporter(service_name="my-service")) + + resource = Resource.create(attributes={"service.name": "api-service"}) + + trace.set_tracer_provider(TracerProvider(resource=resource)) + # This uses insecure connection for the purpose of example. Please see the + # OTLP Exporter documentation for other options. + span_processor = BatchExportSpanProcessor( + OTLPSpanExporter(endpoint="localhost:4317", insecure=True) ) + trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/docs/examples/fork-process-model/flask-gunicorn/requirements.txt b/docs/examples/fork-process-model/flask-gunicorn/requirements.txt index 2b8c147bcce..5506e5ab2cd 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/requirements.txt +++ b/docs/examples/fork-process-model/flask-gunicorn/requirements.txt @@ -7,7 +7,7 @@ itsdangerous==1.1.0 Jinja2==2.11.3 MarkupSafe==1.1.1 opentelemetry-api==0.17b0 -opentelemetry-exporter-jaeger==0.17b0 +opentelemetry-exporter-otlp==0.17b0 opentelemetry-instrumentation==0.17b0 opentelemetry-instrumentation-flask==0.17b0 opentelemetry-instrumentation-wsgi==0.17b0 diff --git a/docs/examples/fork-process-model/flask-uwsgi/README.rst b/docs/examples/fork-process-model/flask-uwsgi/README.rst index 54a5a68696a..d9310e03f4c 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/README.rst +++ b/docs/examples/fork-process-model/flask-uwsgi/README.rst @@ -10,5 +10,3 @@ Run application .. code-block:: sh uwsgi --http :8000 --wsgi-file app.py --callable application --master --enable-threads - -Please make sure Jaeger is running locally and the default agent host and port are exposed. diff --git a/docs/examples/fork-process-model/flask-uwsgi/app.py b/docs/examples/fork-process-model/flask-uwsgi/app.py index 44bd4893edf..4a9e631e194 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/app.py +++ b/docs/examples/fork-process-model/flask-uwsgi/app.py @@ -17,24 +17,28 @@ from uwsgidecorators import postfork from opentelemetry import trace -from opentelemetry.exporter.jaeger import JaegerSpanExporter +from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter from opentelemetry.instrumentation.flask import FlaskInstrumentor +from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor -trace.set_tracer_provider(TracerProvider()) - application = flask.Flask(__name__) FlaskInstrumentor().instrument_app(application) -tracer = trace.get_tracer(__name__) @postfork -def set_span_processor(): - trace.get_tracer_provider().add_span_processor( - BatchExportSpanProcessor(JaegerSpanExporter(service_name="my-service")) +def init_tracing(): + resource = Resource.create(attributes={"service.name": "api-service"}) + + trace.set_tracer_provider(TracerProvider(resource=resource)) + # This uses insecure connection for the purpose of example. Please see the + # OTLP Exporter documentation for other options. + span_processor = BatchExportSpanProcessor( + OTLPSpanExporter(endpoint="localhost:4317", insecure=True) ) + trace.get_tracer_provider().add_span_processor(span_processor) def fib_slow(n): @@ -53,6 +57,7 @@ def fib_fast(n): @application.route("/fibonacci") def fibonacci(): + tracer = trace.get_tracer(__name__) n = int(request.args.get("n", 1)) with tracer.start_as_current_span("root"): with tracer.start_as_current_span("fib_slow") as slow_span: @@ -64,7 +69,7 @@ def fibonacci(): fast_span.set_attribute("n", n) fast_span.set_attribute("nth_fibonacci", ans) - return "Hello" + return "F({}) is: ({})".format(n, ans) if __name__ == "__main__": diff --git a/docs/examples/fork-process-model/flask-uwsgi/requirements.txt b/docs/examples/fork-process-model/flask-uwsgi/requirements.txt index 2b8c147bcce..5506e5ab2cd 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/requirements.txt +++ b/docs/examples/fork-process-model/flask-uwsgi/requirements.txt @@ -7,7 +7,7 @@ itsdangerous==1.1.0 Jinja2==2.11.3 MarkupSafe==1.1.1 opentelemetry-api==0.17b0 -opentelemetry-exporter-jaeger==0.17b0 +opentelemetry-exporter-otlp==0.17b0 opentelemetry-instrumentation==0.17b0 opentelemetry-instrumentation-flask==0.17b0 opentelemetry-instrumentation-wsgi==0.17b0 From 7e2a0797a69112696c08f4ce5f405589f751afe6 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 4 Mar 2021 09:06:43 +0530 Subject: [PATCH 10/10] Update deps --- .../fork-process-model/flask-gunicorn/requirements.txt | 4 ++-- docs/examples/fork-process-model/flask-uwsgi/requirements.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/examples/fork-process-model/flask-gunicorn/requirements.txt b/docs/examples/fork-process-model/flask-gunicorn/requirements.txt index 4d83635139d..e36515841e2 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/requirements.txt +++ b/docs/examples/fork-process-model/flask-gunicorn/requirements.txt @@ -9,8 +9,8 @@ MarkupSafe==1.1.1 opentelemetry-api==0.18b0 opentelemetry-exporter-otlp==0.18b0 opentelemetry-instrumentation==0.18b0 -opentelemetry-instrumentation-flask==v0.18b1 -opentelemetry-instrumentation-wsgi==v0.18b1 +opentelemetry-instrumentation-flask==0.18b1 +opentelemetry-instrumentation-wsgi==0.18b1 opentelemetry-sdk==0.18b0 protobuf==3.14.0 six==1.15.0 diff --git a/docs/examples/fork-process-model/flask-uwsgi/requirements.txt b/docs/examples/fork-process-model/flask-uwsgi/requirements.txt index 4d83635139d..e36515841e2 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/requirements.txt +++ b/docs/examples/fork-process-model/flask-uwsgi/requirements.txt @@ -9,8 +9,8 @@ MarkupSafe==1.1.1 opentelemetry-api==0.18b0 opentelemetry-exporter-otlp==0.18b0 opentelemetry-instrumentation==0.18b0 -opentelemetry-instrumentation-flask==v0.18b1 -opentelemetry-instrumentation-wsgi==v0.18b1 +opentelemetry-instrumentation-flask==0.18b1 +opentelemetry-instrumentation-wsgi==0.18b1 opentelemetry-sdk==0.18b0 protobuf==3.14.0 six==1.15.0