Skip to content

Update sample apps to work with OTel Python v1.3 #12

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

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions integration-test-apps/auto-instrumentation/flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.6
FROM python:3.9

WORKDIR /app

Expand All @@ -10,4 +10,6 @@ RUN opentelemetry-bootstrap --action=install

ENV HOME=/

CMD opentelemetry-instrument -e otlp --ids-generator aws_xray python application.py
ENV OTEL_RESOURCE_ATTRIBUTES='service.name=aws-sample-auto-app'

CMD opentelemetry-instrument --trace-exporter otlp --id-generator aws_xray python3 application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# OTel Imports

from opentelemetry import propagators
from opentelemetry import propagate

# AWS X-Ray SDK Extension Components

Expand All @@ -18,7 +18,7 @@
# Setup AWS X-Ray Propagator

# Propagators can be set using environment variable: OTEL_PROPAGATORS = aws_xray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why not use the environment variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason! It's just that the 2 main components you need to get OTel Python to work with X-Ray is that:

  1. The IdsGenerator
  2. The Propagator

So I wanted those to be obvious from the source code here in the manual instrumentation instead of hiding them in environment variables in the script.

You can do the ID Generator from an environment variables too as of recent I think!

propagators.set_global_textmap(AwsXRayFormat())
propagate.set_global_textmap(AwsXRayFormat())

if __name__ == "__main__":
app.run(**get_flask_app_run_args())
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
TRACE_ID_VERSION,
)

from setup_metrics import apiBytesSentCounter, apiLatencyRecorder
# NOTE: (NathanielRN) Metrics is on hold until 1.50 release
# from setup_metrics import apiBytesSentCounter, apiLatencyRecorder

# Constants

Expand Down Expand Up @@ -52,22 +53,22 @@ def mimicPayloadSize():

@app.after_request
def after_request_func(response):
if request.path == "/outgoing-http-call":
apiBytesSentCounter.add(
response.calculate_content_length() + mimicPayloadSize(),
{
DIMENSION_API_NAME: request.path,
DIMENSION_STATUS_CODE: response.status_code,
},
)

apiLatencyRecorder.record(
int(time.time() * 1_000) - session[REQUEST_START_TIME],
{
DIMENSION_API_NAME: request.path,
DIMENSION_STATUS_CODE: response.status_code,
},
)
# if request.path == "/outgoing-http-call":
# apiBytesSentCounter.add(
# response.calculate_content_length() + mimicPayloadSize(),
# {
# DIMENSION_API_NAME: request.path,
# DIMENSION_STATUS_CODE: response.status_code,
# },
# )

# apiLatencyRecorder.record(
# int(time.time() * 1_000) - session[REQUEST_START_TIME],
# {
# DIMENSION_API_NAME: request.path,
# DIMENSION_STATUS_CODE: response.status_code,
# },
# )

return response

Expand Down Expand Up @@ -105,4 +106,7 @@ def root_endpoint():

def get_flask_app_run_args():
host, port = os.environ["LISTEN_ADDRESS"].split(":")
return {"host": host, "port": int(port), "debug": True}
# NOTE: (NathanielRN) The auto-reloader of the Flask app in debug=True mode
# will remove automatically-introduced instrumentation! Filing issue
# upstream #TBD.
return {"host": host, "port": int(port), "debug": False}
19 changes: 11 additions & 8 deletions integration-test-apps/auto-instrumentation/flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
boto3==1.16.30
requests==2.23.0
Flask==1.1.2
boto3
requests
# OTel Python instrumentation package needs to indicate support for Flask~=2.0
# https://github.com/open-telemetry/opentelemetry-python-contrib/pull/545
flask~=1.0

opentelemetry-api==0.16b1
opentelemetry-sdk==0.16b1
opentelemetry-instrumentation==0.16b1
opentelemetry-exporter-otlp==0.16b1
opentelemetry-sdk-extension-aws==0.16b1
opentelemetry-api
opentelemetry-sdk
opentelemetry-instrumentation
opentelemetry-distro
opentelemetry-exporter-otlp
opentelemetry-sdk-extension-aws
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.6
FROM python:3.9

WORKDIR /app

Expand All @@ -8,4 +8,6 @@ RUN pip install -r requirements.txt

ENV HOME=/

ENV OTEL_RESOURCE_ATTRIBUTES='service.name=aws-sample-manual-app'

CMD python application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# OTel Imports

from opentelemetry import propagators, trace
from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter
from opentelemetry import propagate, trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

# Instrumentation Libraries

Expand All @@ -15,11 +15,11 @@
# OpenTelemetry SDK Components

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# AWS X-Ray SDK Extension Components

from opentelemetry.sdk.extension.aws.trace import AwsXRayIdsGenerator
from opentelemetry.sdk.extension.aws.trace import AwsXRayIdGenerator
from opentelemetry.sdk.extension.aws.trace.propagation.aws_xray_format import (
AwsXRayFormat,
)
Expand All @@ -31,18 +31,18 @@
# Setup AWS X-Ray Propagator

# Propagators can be set using environment variable: OTEL_PROPAGATORS = aws_xray
propagators.set_global_textmap(AwsXRayFormat())
propagate.set_global_textmap(AwsXRayFormat())

# Setup Tracer

# OTLP Exporter is configured through environment variables:
# - OTEL_EXPORTER_OTLP_ENDPOINT
# - OTEL_EXPORTER_OTLP_CERTIFICATE
otlp_exporter = OTLPSpanExporter()
span_processor = BatchExportSpanProcessor(otlp_exporter)
span_processor = BatchSpanProcessor(otlp_exporter)
trace.set_tracer_provider(
TracerProvider(
active_span_processor=span_processor, ids_generator=AwsXRayIdsGenerator()
active_span_processor=span_processor, id_generator=AwsXRayIdGenerator()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
TRACE_ID_VERSION,
)

from setup_metrics import apiBytesSentCounter, apiLatencyRecorder
# NOTE: (NathanielRN) Metrics is on hold until 1.50 release
# from setup_metrics import apiBytesSentCounter, apiLatencyRecorder

# Constants

Expand Down Expand Up @@ -52,22 +53,22 @@ def mimicPayloadSize():

@app.after_request
def after_request_func(response):
if request.path == "/outgoing-http-call":
apiBytesSentCounter.add(
response.calculate_content_length() + mimicPayloadSize(),
{
DIMENSION_API_NAME: request.path,
DIMENSION_STATUS_CODE: response.status_code,
},
)

apiLatencyRecorder.record(
int(time.time() * 1_000) - session[REQUEST_START_TIME],
{
DIMENSION_API_NAME: request.path,
DIMENSION_STATUS_CODE: response.status_code,
},
)
# if request.path == "/outgoing-http-call":
# apiBytesSentCounter.add(
# response.calculate_content_length() + mimicPayloadSize(),
# {
# DIMENSION_API_NAME: request.path,
# DIMENSION_STATUS_CODE: response.status_code,
# },
# )

# apiLatencyRecorder.record(
# int(time.time() * 1_000) - session[REQUEST_START_TIME],
# {
# DIMENSION_API_NAME: request.path,
# DIMENSION_STATUS_CODE: response.status_code,
# },
# )

return response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ Flask==1.1.2
./opentelemetry-python-core/opentelemetry-api
./opentelemetry-python-contrib/sdk-extension/opentelemetry-sdk-extension-aws
./opentelemetry-python-core/opentelemetry-sdk
./opentelemetry-python-core/opentelemetry-instrumentation
./opentelemetry-python-contrib/opentelemetry-instrumentation
./opentelemetry-python-core/opentelemetry-semantic-conventions
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-botocore
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-requests
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-wsgi
./opentelemetry-python-contrib/util/opentelemetry-util-http
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-flask
./opentelemetry-python-core/opentelemetry-proto
./opentelemetry-python-core/exporter/opentelemetry-exporter-otlp-proto-grpc
./opentelemetry-python-core/exporter/opentelemetry-exporter-otlp
4 changes: 3 additions & 1 deletion integration-test-apps/run_integration_test_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ OTEL_EXPORTER_ENV_VARS_CONFIG=''

if [[ $OTEL_EXPORTER_OTLP_ENDPOINT ]]; then
OTEL_EXPORTER_ENV_VARS_CONFIG="$OTEL_EXPORTER_ENV_VARS_CONFIG -e OTEL_EXPORTER_OTLP_ENDPOINT=$OTEL_EXPORTER_OTLP_ENDPOINT"
else
OTEL_EXPORTER_ENV_VARS_CONFIG="$OTEL_EXPORTER_ENV_VARS_CONFIG -e OTEL_EXPORTER_OTLP_ENDPOINT=172.17.0.1:4317"
fi

if [[ $OTEL_EXPORTER_OTLP_INSECURE ]]; then
Expand Down Expand Up @@ -71,7 +73,7 @@ if ! [[ $INSTRUMENTATION_TYPES =~ (^|[[:space:]])"$INSTRUMENTATION_TYPE"($|[[:sp
fi

SCRIPT_PATH=$(dirname $0)
APP_PATH=$SCRIPT_PATH/$INSTRUMENTATION_TYPE-instrumentation/$APP_PLATFORM/
APP_PATH=$SCRIPT_PATH/$INSTRUMENTATION_TYPE-instrumentation/$APP_PLATFORM

# AWS OTel Python Specific Commands

Expand Down