Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SDK providers in example code #488

Merged
merged 1 commit into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/examples/metrics/observer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import psutil

from opentelemetry import metrics
from opentelemetry.sdk.metrics import LabelSet
from opentelemetry.sdk.metrics import LabelSet, MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

# Configure a stateful batcher
batcher = UngroupedBatcher(stateful=True)

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)

# Exporter to export metrics to the console
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/metrics/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

from opentelemetry import metrics
from opentelemetry.ext.prometheus import PrometheusMetricsExporter
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController

# Start Prometheus client
start_http_server(port=8000, addr="localhost")

# Meter is responsible for creating and recording metrics
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
# exporter to export metrics to Prometheus
prefix = "MyAppPrefix"
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/metrics/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

# Use the meter type provided by the SDK package
metrics.set_meter_provider(MeterProvider())
# Meter is responsible for creating and recording metrics
meter = metrics.get_meter(__name__)
# exporter to export metrics to the console
Expand Down
14 changes: 7 additions & 7 deletions docs/examples/metrics/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Measure
from opentelemetry.sdk.metrics import Counter, Measure, MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

Expand All @@ -43,13 +43,13 @@ def usage(argv):
usage(sys.argv)
sys.exit(1)

# Meter is responsible for creating and recording metrics

# Meter's namespace corresponds to the string passed as the first argument Pass
# in True/False to indicate whether the batcher is stateful. True indicates the
# batcher computes checkpoints from over the process lifetime. False indicates
# the batcher computes checkpoints which describe the updates of a single
# collection period (deltas)
# The Meter is responsible for creating and recording metrics. Each meter has a
# unique name, which we set as the module's name here. The second argument
# determines whether how metrics are collected: if true, metrics accumulate
# over the process lifetime. If false, metrics are reset at the beginning of
# each collection interval.
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__, batcher_mode == "stateful")

# Exporter to export metrics to the console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import opentelemetry.ext.http_requests
from opentelemetry import trace
from opentelemetry.ext.flask import instrument_app
from opentelemetry.sdk.trace import TracerProvider


def configure_opentelemetry(flask_app: flask.Flask):
Expand All @@ -33,21 +34,16 @@ def configure_opentelemetry(flask_app: flask.Flask):
* sets tracer to the SDK's Tracer
* enables requests integration on the Tracer
* uses a WSGI middleware to enable configuration

TODO:

* processors?
* exporters?
"""
# Start by configuring all objects required to ensure
# a complete end to end workflow.
# Start by configuring all objects required to ensure a complete end to end
# workflow.
trace.set_tracer_provider(TracerProvider())

# Next, we need to configure how the values that are used by
# traces and metrics are propagated (such as what specific headers
# carry this value).
# Integrations are the glue that binds the OpenTelemetry API
# and the frameworks and libraries that are used together, automatically
# creating Spans and propagating context as appropriate.
# Next, we need to configure how the values that are used by traces and
# metrics are propagated (such as what specific headers carry this value).
# Integrations are the glue that binds the OpenTelemetry API and the
# frameworks and libraries that are used together, automatically creating
# Spans and propagating context as appropriate.
opentelemetry.ext.http_requests.enable(trace.get_tracer_provider())
instrument_app(flask_app)

Expand All @@ -57,8 +53,7 @@ def configure_opentelemetry(flask_app: flask.Flask):

@app.route("/")
def hello():
# emit a trace that measures how long the
# sleep takes
# Emit a trace that measures how long the sleep takes
version = pkg_resources.get_distribution(
"opentelemetry-example-app"
).version
Expand Down
3 changes: 3 additions & 0 deletions docs/examples/opentracing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from opentelemetry import trace
from opentelemetry.ext import opentracing_shim
from opentelemetry.ext.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
from rediscache import RedisCache

# Configure the tracer using the default implementation
trace.set_tracer_provider(TracerProvider())
tracer_provider = trace.get_tracer_provider()

# Configure the tracer to export traces to Jaeger
Expand Down
3 changes: 2 additions & 1 deletion examples/metrics/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
from opentelemetry.ext.otcollector.metrics_exporter import (
CollectorMetricsExporter,
)
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController

# Meter is responsible for creating and recording metrics
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
# exporter to export metrics to OT Collector
exporter = CollectorMetricsExporter(
Expand Down
5 changes: 4 additions & 1 deletion ext/opentelemetry-ext-dbapi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ Usage

import mysql.connector
import pyodbc
from opentelemetry.trace import tracer_provider

from opentelemetry.ext.dbapi import trace_integration
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.trace import tracer_provider

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
# Ex: mysql.connector
trace_integration(tracer_provider(), mysql.connector, "connect", "mysql", "sql")
Expand Down
2 changes: 2 additions & 0 deletions ext/opentelemetry-ext-jaeger/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ gRPC is still not supported by this implementation.

from opentelemetry import trace
from opentelemetry.ext import jaeger
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# create a JaegerSpanExporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from opentelemetry import trace
from opentelemetry.ext import jaeger
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# create a JaegerSpanExporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
import time

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.ext.opentracing_shim import create_tracer

# Tell OpenTelemetry which Tracer implementation to use.
trace.set_tracer_provider(TracerProvider())

# Create an OpenTelemetry Tracer.
otel_tracer = trace.get_tracer(__name__)

Expand Down
3 changes: 2 additions & 1 deletion ext/opentelemetry-ext-prometheus/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ The **OpenTelemetry Prometheus Exporter** allows to export `OpenTelemetry`_ metr

from opentelemetry import metrics
from opentelemetry.ext.prometheus import PrometheusMetricsExporter
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController
from prometheus_client import start_http_server

# Start Prometheus client
start_http_server(port=8000, addr="localhost")

# Meter is responsible for creating and recording metrics
metrics.set_meter_provider(MeterProvider())
meter = metrics.meter()
# exporter to export metrics to Prometheus
prefix = "MyAppPrefix"
Expand Down
2 changes: 2 additions & 0 deletions ext/opentelemetry-ext-psycopg2/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ Usage

import psycopg2
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.trace.ext.psycopg2 import trace_integration

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace_integration(tracer)
cnx = psycopg2.connect(database='Database')
Expand Down
2 changes: 2 additions & 0 deletions ext/opentelemetry-ext-zipkin/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ This exporter always send traces to the configured Zipkin collector using HTTP.

from opentelemetry import trace
from opentelemetry.ext import zipkin
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

# create a ZipkinSpanExporter
Expand Down