コード例 #1
0
def configure_tracing(configuration: dict):
    # OTLP Exporter configuration
    if configuration['exporter'] == 'otlp':
        service_name = {'service.name': configuration['service_name']}
        resource = Resource(service_name)
        trace.set_tracer_provider(TracerProvider(resource=resource))
        exporter = OTLPSpanExporter(
            endpoint=configuration['exporter_endpoint'],
            insecure=configuration['exporter_insecure'])
        trace.get_tracer(__name__)
        span_processor = BatchExportSpanProcessor(exporter)
        trace.get_tracer_provider().add_span_processor(span_processor)

    # Jaeger HTTP Exporter configuration
    elif configuration['exporter'] == 'jaeger_http':
        exporter = JaegerSpanExporter(
            service_name=configuration['service_name'],
            collector_endpoint=configuration['exporter_endpoint'],
        )
        trace.set_tracer_provider(TracerProvider())
        trace.get_tracer(__name__)
        span_processor = BatchExportSpanProcessor(exporter)
        trace.get_tracer_provider().add_span_processor(span_processor)

    # Jaeger Thrifth Compact Exporter configuration
    elif configuration['exporter'] == 'jaeger_thrift':
        exporter = JaegerSpanExporter(
            service_name=configuration['service_name'],
            agent_host_name=configuration['exporter_host'],
            agent_port=configuration['exporter_port'],
        )
        trace.set_tracer_provider(TracerProvider())
        trace.get_tracer(__name__)
        span_processor = BatchExportSpanProcessor(exporter)
        trace.get_tracer_provider().add_span_processor(span_processor)

    # Zipkin Exporter configuration
    elif configuration['exporter'] == 'zipkin':
        exporter = ZipkinSpanExporter(
            service_name=configuration['service_name'],
            url=configuration['exporter_endpoint'])
        trace.set_tracer_provider(TracerProvider())
        trace.get_tracer(__name__)
        span_processor = BatchExportSpanProcessor(exporter)
        trace.get_tracer_provider().add_span_processor(span_processor)

    # Console Exporter configuration
    elif configuration['exporter'] == 'console':
        trace.set_tracer_provider(TracerProvider())
        trace.get_tracer_provider().add_span_processor(
            SimpleExportSpanProcessor(ConsoleSpanExporter()))
    else:
        raise ValueError(
            'Only Otlp, Jaeger Thrift/HTTP and Zipkin exporters are supported. '
            'Please check your configuration.')
    def test_constructor_by_environment_variables(self):
        """Test using Environment Variables."""
        # pylint: disable=protected-access
        Configuration._reset()
        service = "my-opentelemetry-jaeger"

        collector_endpoint = "localhost:14250"

        env_patch = patch.dict(
            "os.environ",
            {
                "OTEL_EXPORTER_JAEGER_ENDPOINT": collector_endpoint,
                "OTEL_EXPORTER_JAEGER_CERTIFICATE": os.path.dirname(__file__)
                + "/certs/cred.cert",
            },
        )

        env_patch.start()

        exporter = JaegerSpanExporter(
            service_name=service, transport_format="protobuf"
        )

        self.assertEqual(exporter.service_name, service)
        self.assertIsNotNone(exporter._collector_grpc_client)
        self.assertEqual(exporter.collector_endpoint, collector_endpoint)
        self.assertIsNotNone(exporter.credentials)

        env_patch.stop()
コード例 #3
0
def get_application() -> FastAPI:

    trace.set_tracer_provider(TracerProvider())
    jaeger_exporter = JaegerSpanExporter(
        service_name="Jaeger Demo App",
        agent_host_name="jaeger-agent",
        agent_port=6831
        #service_name="Jaeger Demo App", collector_endpoint="http://localhost:31298/api/traces"
    )
    trace.get_tracer_provider().add_span_processor(
        BatchExportSpanProcessor(jaeger_exporter, max_export_batch_size=10))

    application = FastAPI(title="Jaeger Demo App",
                          description="Jaeger Demo App Entrypoiny",
                          openapi_url="/openapi.json",
                          docs_url="/docs")

    # Add your routers
    FastAPIInstrumentor.instrument_app(application)
    return application
コード例 #4
0
from rediscache import RedisCache

from opentelemetry import trace
from opentelemetry.exporter.jaeger import JaegerSpanExporter
from opentelemetry.instrumentation import opentracing_shim
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

# 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
jaeger_exporter = JaegerSpanExporter(
    service_name="OpenTracing Shim Example",
    agent_host_name="localhost",
    agent_port=6831,
)
span_processor = SimpleExportSpanProcessor(jaeger_exporter)
tracer_provider.add_span_processor(span_processor)

# Create an OpenTracing shim. This implements the OpenTracing tracer API, but
# forwards calls to the underlying OpenTelemetry tracer.
opentracing_tracer = opentracing_shim.create_tracer(tracer_provider)

# Our example caching library expects an OpenTracing-compliant tracer.
redis_cache = RedisCache(opentracing_tracer)

# Appication code uses an OpenTelemetry Tracer as usual.
tracer = trace.get_tracer(__name__)
コード例 #5
0
from opentelemetry import trace
from opentelemetry.exporter.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
import opentelemetry.instrumentation.requests
#from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware

from opentelemetry.sdk.trace.propagation.b3_format import B3Format

from opentelemetry import propagators

#setting propagators
#propagators.set_global_textmap(B3Format())

# SpanExporter receives the spans and send them to the target location.
exporter = JaegerSpanExporter(
    service_name="Veamer-manage",
    agent_host_name="jaeger",
    agent_port=6831,
)
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

#exec(open("./manage.py").read())

import manage
#manage.app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)
manage.main()
コード例 #6
0
db = SQLAlchemy(app)


class WFHAdvice(db.Model):
    __tablename__ = "wfh_advice"
    # Here we define columns for the table restaurant
    # Notice that each column is also a normal Python instance attribute.
    id = Column(Integer, primary_key=True)
    advice = Column(String(512), nullable=False)


trace.get_tracer_provider().add_span_processor(
    BatchExportSpanProcessor(
        JaegerSpanExporter(
            "flask-server",
            agent_host_name=os.getenv("OTEL_EXPORTER_JAEGER_AGENT_HOST",
                                      "localhost"),
        ), ))


@app.route("/populate")
def populate():
    advice = []
    try:
        advice = WFHAdvice.query.all()
    except Exception:
        # ignore any errors here
        pass
    if len(advice) <= 0:
        db.create_all()
        advice_strings = {
コード例 #7
0
# limitations under the License.

import os
import time

import backoff
from opentelemetry import trace
from opentelemetry.exporter.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
import requests


trace.get_tracer_provider().add_span_processor(
    BatchExportSpanProcessor(
        JaegerSpanExporter(
            "requests-client",
            agent_host_name=os.getenv("OTEL_EXPORTER_JAEGER_AGENT_HOST", "localhost"),
        ),
    )
)


def send_requests(destination):
    url = f"{destination}/help"
    try:
        res = requests.get(url)
        print(f"Working from home tip: {res.text}")
    except Exception as e:
        print(f"Request to {url} failed {e}")


@backoff.on_exception(
コード例 #8
0
import mysql.connector

from opentelemetry.instrumentation.mysql import MySQLInstrumentor
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

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

# SpanExporter receives the spans and send them to the target location.
exporter = JaegerSpanExporter(
    service_name=os.getenv('OTEL_JAEGER_SERVICE_NAME', "python-service"),
    agent_host_name=os.getenv('OTEL_JAEGER_AGENT_HOST', "localhost"),
    agent_port=6831)

span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

FlaskInstrumentor().instrument_app(app)
MySQLInstrumentor().instrument()

# configuration used to connect to MariaDB
config = {
    'host': os.getenv('MYSQL_HOST', "localhost"),
    'port': 3306,
    'user': os.getenv('MYSQL_USER', "admin"),
    'password': os.getenv('MYSQL_PASSWORD', "password"),
    'database': 'salary_amount'
コード例 #9
0
from opentelemetry import trace
from opentelemetry.exporter.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

# SpanExporter receives the spans and send them to the target location.
exporter = JaegerSpanExporter(
    service_name="auto-instrument-example",
    agent_host_name="jaeger",
    agent_port=6831,
)
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
print('Tracing Initialized')
import recommendation_server as r
r.main()