From bb89831cc1dcf27e6d93208770f6765df35b2300 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Mon, 30 Sep 2019 15:46:46 -0700 Subject: [PATCH 1/5] add simple client/server examples --- examples/trace/client.py | 30 +++++++++++++++++++++++++++ examples/trace/server.py | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 examples/trace/client.py create mode 100644 examples/trace/server.py diff --git a/examples/trace/client.py b/examples/trace/client.py new file mode 100644 index 00000000000..c1013c5fec4 --- /dev/null +++ b/examples/trace/client.py @@ -0,0 +1,30 @@ +# Copyright 2019, OpenCensus 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 requests + +from opentelemetry import trace +from opentelemetry.ext import http_requests +from opentelemetry.sdk.trace import Tracer +from opentelemetry.sdk.trace.export import ConsoleSpanExporter +from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor + +trace.set_preferred_tracer_implementation(lambda T: Tracer()) +tracer = trace.tracer() +http_requests.enable(tracer) +span_processor = SimpleExportSpanProcessor(ConsoleSpanExporter()) +tracer.add_span_processor(span_processor) + +response = requests.get(url="http://127.0.0.1:5000/") +span_processor.shutdown() diff --git a/examples/trace/server.py b/examples/trace/server.py new file mode 100644 index 00000000000..c162f49d2c6 --- /dev/null +++ b/examples/trace/server.py @@ -0,0 +1,44 @@ +# Copyright 2019, OpenCensus 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 +import requests + +from opentelemetry import trace +from opentelemetry.ext import http_requests +from opentelemetry.ext.wsgi import OpenTelemetryMiddleware +from opentelemetry.sdk.trace import Tracer +from opentelemetry.sdk.trace.export import ConsoleSpanExporter +from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor + +trace.set_preferred_tracer_implementation(lambda T: Tracer()) + +http_requests.enable(trace.tracer()) +span_processor = SimpleExportSpanProcessor(ConsoleSpanExporter()) +trace.tracer().add_span_processor(span_processor) + +app = flask.Flask(__name__) +app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app) + + +@app.route("/") +def hello(): + with trace.tracer().start_span("parent"): + requests.get("https://www.wikipedia.org/wiki/Rabbit") + return "hello" + + +if __name__ == "__main__": + app.run(debug=True) + span_processor.shutdown() From 610e5ba6f354ff5066281369b455d3e385e16522 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Mon, 30 Sep 2019 16:04:53 -0700 Subject: [PATCH 2/5] lint --- examples/trace/client.py | 6 ++++-- examples/trace/server.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/trace/client.py b/examples/trace/client.py index c1013c5fec4..63480757e8a 100644 --- a/examples/trace/client.py +++ b/examples/trace/client.py @@ -17,8 +17,10 @@ from opentelemetry import trace from opentelemetry.ext import http_requests from opentelemetry.sdk.trace import Tracer -from opentelemetry.sdk.trace.export import ConsoleSpanExporter -from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor +from opentelemetry.sdk.trace.export import ( + ConsoleSpanExporter, + SimpleExportSpanProcessor, +) trace.set_preferred_tracer_implementation(lambda T: Tracer()) tracer = trace.tracer() diff --git a/examples/trace/server.py b/examples/trace/server.py index c162f49d2c6..82095bd6c68 100644 --- a/examples/trace/server.py +++ b/examples/trace/server.py @@ -19,8 +19,10 @@ from opentelemetry.ext import http_requests from opentelemetry.ext.wsgi import OpenTelemetryMiddleware from opentelemetry.sdk.trace import Tracer -from opentelemetry.sdk.trace.export import ConsoleSpanExporter -from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor +from opentelemetry.sdk.trace.export import ( + ConsoleSpanExporter, + SimpleExportSpanProcessor, +) trace.set_preferred_tracer_implementation(lambda T: Tracer()) From d7d35c7739b136c9c6c4d5b24f86f6e9ec1ff329 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Tue, 1 Oct 2019 22:05:36 -0700 Subject: [PATCH 3/5] chmod and add shebang --- examples/trace/client.py | 2 ++ examples/trace/server.py | 2 ++ 2 files changed, 4 insertions(+) mode change 100644 => 100755 examples/trace/client.py mode change 100644 => 100755 examples/trace/server.py diff --git a/examples/trace/client.py b/examples/trace/client.py old mode 100644 new mode 100755 index 63480757e8a..3e9cabe045f --- a/examples/trace/client.py +++ b/examples/trace/client.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# # Copyright 2019, OpenCensus Authors # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/examples/trace/server.py b/examples/trace/server.py old mode 100644 new mode 100755 index 82095bd6c68..2c9057e52cd --- a/examples/trace/server.py +++ b/examples/trace/server.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# # Copyright 2019, OpenCensus Authors # # Licensed under the Apache License, Version 2.0 (the "License"); From c70360b8422e48d8b21674e1d935e0ca5d3e0652 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Tue, 1 Oct 2019 22:13:30 -0700 Subject: [PATCH 4/5] add some comments --- examples/trace/client.py | 8 ++++++++ examples/trace/server.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/examples/trace/client.py b/examples/trace/client.py index 3e9cabe045f..0842e6d778c 100755 --- a/examples/trace/client.py +++ b/examples/trace/client.py @@ -24,9 +24,17 @@ SimpleExportSpanProcessor, ) +# The preferred tracer implementation must be set, as the opentelemetry-api +# defines the interface with a no-op implementation. trace.set_preferred_tracer_implementation(lambda T: Tracer()) tracer = trace.tracer() + +# 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. http_requests.enable(tracer) + +# SpanExporter receives the spans and send them to the target location. span_processor = SimpleExportSpanProcessor(ConsoleSpanExporter()) tracer.add_span_processor(span_processor) diff --git a/examples/trace/server.py b/examples/trace/server.py index 2c9057e52cd..e639df0bb79 100755 --- a/examples/trace/server.py +++ b/examples/trace/server.py @@ -26,9 +26,16 @@ SimpleExportSpanProcessor, ) +# The preferred tracer implementation must be set, as the opentelemetry-api +# defines the interface with a no-op implementation. trace.set_preferred_tracer_implementation(lambda T: Tracer()) +# 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. http_requests.enable(trace.tracer()) + +# SpanExporter receives the spans and send them to the target location. span_processor = SimpleExportSpanProcessor(ConsoleSpanExporter()) trace.tracer().add_span_processor(span_processor) From 6ff60e497e641246c9315b13286fcd5333c82aed Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Tue, 1 Oct 2019 22:15:59 -0700 Subject: [PATCH 5/5] fix typo --- examples/trace/client.py | 2 +- examples/trace/server.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/trace/client.py b/examples/trace/client.py index 0842e6d778c..662cea8d969 100755 --- a/examples/trace/client.py +++ b/examples/trace/client.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -# Copyright 2019, OpenCensus Authors +# Copyright 2019, OpenTelemetry Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/examples/trace/server.py b/examples/trace/server.py index e639df0bb79..878898593c4 100755 --- a/examples/trace/server.py +++ b/examples/trace/server.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -# Copyright 2019, OpenCensus Authors +# Copyright 2019, OpenTelemetry Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.