|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2019, OpenTelemetry Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +""" |
| 17 | +This server is intended to be used with the W3C tracecontext validation |
| 18 | +Service. It implements the APIs needed to be exercised by the test bed. |
| 19 | +""" |
| 20 | + |
| 21 | +import json |
| 22 | + |
| 23 | +import flask |
| 24 | +import requests |
| 25 | + |
| 26 | +from opentelemetry import trace |
| 27 | +from opentelemetry.ext import http_requests |
| 28 | +from opentelemetry.ext.wsgi import OpenTelemetryMiddleware |
| 29 | +from opentelemetry.sdk.trace import Tracer |
| 30 | +from opentelemetry.sdk.trace.export import ( |
| 31 | + ConsoleSpanExporter, |
| 32 | + SimpleExportSpanProcessor, |
| 33 | +) |
| 34 | + |
| 35 | +# The preferred tracer implementation must be set, as the opentelemetry-api |
| 36 | +# defines the interface with a no-op implementation. |
| 37 | +trace.set_preferred_tracer_implementation(lambda T: Tracer()) |
| 38 | + |
| 39 | +# Integrations are the glue that binds the OpenTelemetry API and the |
| 40 | +# frameworks and libraries that are used together, automatically creating |
| 41 | +# Spans and propagating context as appropriate. |
| 42 | +http_requests.enable(trace.tracer()) |
| 43 | + |
| 44 | +# SpanExporter receives the spans and send them to the target location. |
| 45 | +span_processor = SimpleExportSpanProcessor(ConsoleSpanExporter()) |
| 46 | +trace.tracer().add_span_processor(span_processor) |
| 47 | + |
| 48 | +app = flask.Flask(__name__) |
| 49 | +app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app) |
| 50 | + |
| 51 | + |
| 52 | +@app.route("/verify-tracecontext", methods=["POST"]) |
| 53 | +def verify_tracecontext(): |
| 54 | + """Upon reception of some payload, sends a request back to the designated url. |
| 55 | +
|
| 56 | + This route is designed to be testable with the w3c tracecontext server / client test. |
| 57 | + """ |
| 58 | + for action in flask.request.json: |
| 59 | + requests.post( |
| 60 | + url=action["url"], |
| 61 | + data=json.dumps(action["arguments"]), |
| 62 | + headers={ |
| 63 | + "Accept": "application/json", |
| 64 | + "Content-Type": "application/json; charset=utf-8", |
| 65 | + }, |
| 66 | + timeout=5.0, |
| 67 | + ) |
| 68 | + return "hello" |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + try: |
| 73 | + app.run(debug=True) |
| 74 | + finally: |
| 75 | + span_processor.shutdown() |
0 commit comments