Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4055dcf

Browse files
authored
Added metrics export to gunicorn example (open-telemetry#3002)
1 parent 3132a56 commit 4055dcf

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

docs/examples/fork-process-model/flask-gunicorn/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Run application
88
---------------
99
.. code-block:: sh
1010
11-
gunicorn app -c gunicorn.config.py
11+
gunicorn app -c gunicorn.conf.py

docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py renamed to docs/examples/fork-process-model/flask-gunicorn/gunicorn.conf.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from opentelemetry import trace
15+
from opentelemetry import metrics, trace
16+
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
17+
OTLPMetricExporter,
18+
)
1619
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
1720
OTLPSpanExporter,
1821
)
22+
from opentelemetry.sdk.metrics import MeterProvider
23+
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
1924
from opentelemetry.sdk.resources import Resource
2025
from opentelemetry.sdk.trace import TracerProvider
2126
from opentelemetry.sdk.trace.export import BatchSpanProcessor
@@ -41,7 +46,19 @@
4146
def post_fork(server, worker):
4247
server.log.info("Worker spawned (pid: %s)", worker.pid)
4348

44-
resource = Resource.create(attributes={"service.name": "api-service"})
49+
resource = Resource.create(
50+
attributes={
51+
"service.name": "api-service",
52+
# If workers are not distinguished within attributes, traces and
53+
# metrics exported from each worker will be indistinguishable. While
54+
# not necessarily an issue for traces, it is confusing for almost
55+
# all metric types. A built-in way to identify a worker is by PID
56+
# but this may lead to high label cardinality. An alternative
57+
# workaround and additional discussion are available here:
58+
# https://github.com/benoitc/gunicorn/issues/1352
59+
"worker": worker.pid,
60+
}
61+
)
4562

4663
trace.set_tracer_provider(TracerProvider(resource=resource))
4764
# This uses insecure connection for the purpose of example. Please see the
@@ -50,3 +67,13 @@ def post_fork(server, worker):
5067
OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
5168
)
5269
trace.get_tracer_provider().add_span_processor(span_processor)
70+
71+
reader = PeriodicExportingMetricReader(
72+
OTLPMetricExporter(endpoint="http://localhost:4317")
73+
)
74+
metrics.set_meter_provider(
75+
MeterProvider(
76+
resource=resource,
77+
metric_readers=[reader],
78+
)
79+
)

0 commit comments

Comments
 (0)