Skip to content

Commit

Permalink
Merge pull request #288 from humivo/EditPythonManual
Browse files Browse the repository at this point in the history
Update python-manual-instrumentation/javascript sample apps and add to workflow
  • Loading branch information
humivo committed Mar 23, 2023
2 parents ebceb97 + db210c0 commit 421535c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/collector/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_SESSION_TOKEN
- OTEL_RESOURCE_ATTRIBUTES=service.namespace=aws-otel,service.name=aws-otel-integ-test
- OTEL_RESOURCE_ATTRIBUTES=service.namespace=aws-otel,service.name=${LANGUAGE}-sample-app
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otel:4317
- AWS_REGION=us-west-2
ports:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
app-language: [ javascript ]
app-language: [ javascript, python-manual-instrumentation ]
env:
APP_PATH: sample-apps/${{ matrix.app-language }}-sample-app
steps:
Expand All @@ -33,4 +33,5 @@ jobs:
env:
INSTANCE_ID: ${{ github.run_id }}-${{ github.run_number }}
LISTEN_ADDRESS: 0.0.0.0:8080
VALIDATOR_COMMAND: -c standard-otel-trace-metric-validation.yml --testcase standard_otlp_metric_trace --endpoint http://app:8080 --metric-namespace aws-otel/aws-otel-integ-test -t ${{ github.run_id }}-${{ github.run_number }} --language ${{ matrix.app-language }}
LANGUAGE: ${{ matrix.app-language }}
VALIDATOR_COMMAND: -c standard-otel-trace-metric-validation.yml --testcase standard_otlp_metric_trace --endpoint http://app:8080 --metric-namespace aws-otel/${{ matrix.app-language }}-sample-app -t ${{ github.run_id }}-${{ github.run_number }} --language ${{ matrix.app-language }}
8 changes: 4 additions & 4 deletions sample-apps/javascript-sample-app/random-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const metricsApi = require('@opentelemetry/api-metrics');
const create_cfg = require('./config');
const cfg = create_cfg.create_config('./config.yaml');

const TIME_ALIVE_METRIC = 'timeAlive';
const CPU_USAGE_METRIC = 'cpuUsage';
const THREADS_ACTIVE_METRIC = 'threadsActive';
const HEAP_SIZE_METRIC = 'totalHeapSize';
const TIME_ALIVE_METRIC = 'time_alive';
const CPU_USAGE_METRIC = 'cpu_usage';
const THREADS_ACTIVE_METRIC = 'threads_active';
const HEAP_SIZE_METRIC = 'total_heap_size';

const common_attributes = { signal: 'metric', language: 'javascript', metricType: 'random' };

Expand Down
6 changes: 3 additions & 3 deletions sample-apps/javascript-sample-app/request-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
const { SemanticAttributes } = require("@opentelemetry/semantic-conventions");
const metricsApi = require('@opentelemetry/api-metrics');

const TOTAL_BYTES_SENT_METRIC = 'totalBytesSent';
const TOTAL_API_REQUESTS = 'totalApiRequests';
const LATENCY_TIME = 'latencyTime';
const TOTAL_BYTES_SENT_METRIC = 'total_bytes_sent';
const TOTAL_API_REQUESTS = 'total_api_requests';
const LATENCY_TIME = 'latency_time';

let totalApiRequests = 0;

Expand Down
2 changes: 1 addition & 1 deletion sample-apps/javascript-sample-app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function handleRequest(req, res) {
}

async function sdkCall (req, res) {
const traceid = await instrumentRequest('/aws-sdk-call', () => {
const traceid = await instrumentRequest('aws-sdk-call', () => {
const s3 = new AWS.S3();
s3.listBuckets();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ WORKDIR /app

COPY . .

RUN pip install --upgrade pip

RUN pip install --no-cache-dir -r requirements.txt

ENTRYPOINT python app.py
52 changes: 31 additions & 21 deletions sample-apps/python-manual-instrumentation-sample-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
import random
import boto3
import os

from opentelemetry import propagate, trace, metrics

Expand Down Expand Up @@ -63,9 +64,10 @@ def setup_opentelemetry(tracer, meter):
# Set up AWS X-Ray Propagator
propagate.set_global_textmap(AwsXRayPropagator())
# Service name is required for most backends
resource = Resource(attributes={
SERVICE_NAME: 'python-manual-instrumentation-sampleapp'
})
resource_attributes = { 'service.name': 'python-manual-instrumentation-sample-app' }
if (os.environ.get("OTEL_RESOURCE_ATTRIBUTES")):
resource_attributes = None
resource = Resource.create(attributes=resource_attributes)

# Setting up Traces
processor = BatchSpanProcessor(OTLPSpanExporter())
Expand All @@ -78,29 +80,30 @@ def setup_opentelemetry(tracer, meter):
tracer = trace.get_tracer(__name__)

# Setting up Metrics
metric_reader = PeriodicExportingMetricReader(OTLPMetricExporter())
metric_reader = PeriodicExportingMetricReader(exporter=OTLPMetricExporter(), export_interval_millis=1000)
metric_provider = MeterProvider(resource=resource, metric_readers=[metric_reader])

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

common_attributes = { 'signal': 'metric', 'language': 'python-manual-instrumentation', 'metricType': 'request' }

# update_total_bytes_sent updates the metric with a value between 0 and 1024
def update_total_bytes_sent():
min = 0
max = 1024
total_bytes_sent.add(random.randint(min,max))
total_bytes_sent.add(random.randint(min,max), attributes=common_attributes)

# update latency time updates the metric with a value between 0 and 512
def update_latency_time():
min = 0
max = 512
latency_time.record(random.randint(min, max))
latency_time.record(random.randint(min, max), attributes=common_attributes)

def api_requests_callback(options: CallbackOptions):
global n
n += 1
add_api_request = Observation(value=n)
add_api_request = Observation(value=n, attributes=common_attributes)
print("api_requests called by SDK")
yield add_api_request

Expand All @@ -116,24 +119,28 @@ def convert_otel_trace_id_to_xray(otel_trace_id_decimal):
)
return '{{"traceId": "{}"}}'.format(x_ray_trace_id)

testingId = ""
if (os.environ.get("INSTANCE_ID")):
testingId = "_" + os.environ["INSTANCE_ID"]

# register total bytes sent counter
total_bytes_sent=meter.create_counter(
name="totalBytesSent",
name="total_bytes_sent" + testingId,
description="Keeps a sum of the total amount of bytes sent while application is alive",
unit='By'
)

# register api requests observable counter
total_api_requests=meter.create_observable_counter(
name="apiRequests",
name="total_api_requests" + testingId,
callbacks=[api_requests_callback],
description="Increments by one every time a sampleapp endpoint is used",
unit='1'
)

# registers latency time histogram
latency_time=meter.create_histogram(
name="latencyTime",
name="latency_time" + testingId,
description="Measures latency time in buckets of 100, 300 and 500",
unit='ms'
)
Expand All @@ -144,29 +151,32 @@ def call_http():
with tracer.start_as_current_span("outgoing-http-call") as span:

# Demonstrates setting an attribute, a k/v pairing.
span.set_attribute("operation.name", "outgoing-http-call")
span.set_attribute("language", "python-manual-instrumentation")
span.set_attribute("signal", "trace")

# Demonstrating adding events to the span. Think of events as a primitive log.
span.add_event("Making a request to https://aws.amazon.com/")
with tracer.start_as_current_span("amazon-request") as child:
requests.get("https://aws.amazon.com/")
requests.get("https://aws.amazon.com/")

print("updating bytes sent & latency time...")
update_total_bytes_sent()
update_latency_time()
print("updating bytes sent & latency time...")
update_total_bytes_sent()
update_latency_time()

return app.make_response(
convert_otel_trace_id_to_xray(
trace.get_current_span().get_span_context().trace_id
)
return app.make_response(
convert_otel_trace_id_to_xray(
trace.get_current_span().get_span_context().trace_id
)
)

# Test AWS SDK instrumentation
@app.route("/aws-sdk-call")
def call_aws_sdk():

with tracer.start_as_current_span("aws-sdk-call") as span:
span.set_attribute("operation.name", "aws-sdk-call")

span.set_attribute("language", "python-manual-instrumentation")
span.set_attribute("signal", "trace")

print("updating bytes sent & latency time...")
update_total_bytes_sent()
update_latency_time()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
import threading
import time
import os

from config import *
from opentelemetry.metrics import CallbackOptions, Observation
Expand All @@ -10,19 +11,21 @@

meter = metrics.get_meter(__name__)

common_attributes = { 'signal': 'metric', 'language': 'python-manual-instrumentation', 'metricType': 'random' }

# Starts the callback for cpu usage
def cpu_usage_callback(options: CallbackOptions):
min = 0
max = cfg['RandomCpuUsageUpperBound']
cpu_usage = Observation(value=random.randint(min, max))
cpu_usage = Observation(value=random.randint(min, max), attributes=common_attributes)
print('CPU Usage asked by SDK')
yield cpu_usage

# Starts the callback for heap size
def heap_size_callback(options: CallbackOptions):
min = 0
max = cfg['RandomTotalHeapSizeUpperBound']
total_heap_size = Observation(value=random.randint(min, max))
total_heap_size = Observation(value=random.randint(min, max), attributes=common_attributes)
print("Heapsize asked by SDK")
yield total_heap_size

Expand All @@ -31,24 +34,28 @@ class RandomMetricCollector():

# Init registers 4 different metrics
def __init__(self):
testingId = ""
if (os.environ.get("INSTANCE_ID")):
testingId = "_" + os.environ["INSTANCE_ID"]

self.time_alive=meter.create_counter(
name="timeAlive",
name="time_alive" + testingId,
description="Total amount of time that the application has been alive",
unit='ms'
)
self.cpu_usage=meter.create_observable_gauge(
name="cpuUsage",
name="cpu_usage" + testingId,
callbacks=[cpu_usage_callback],
description="Cpu usage percent",
unit='1'
)
self.threads_active=meter.create_up_down_counter(
name="threadsActive",
name="threads_active" + testingId,
description="The total number of threads active",
unit='1'
)
self.total_heap_size=meter.create_observable_gauge(
name="TotalHeapSize",
name="total_heap_size" + testingId,
callbacks=[heap_size_callback],
description="The current total heap size",
unit='By'
Expand All @@ -58,21 +65,21 @@ def __init__(self):

# Adds one to the time alive counter
def update_time_alive(self, cfg=None):
self.time_alive.add(cfg['RandomTimeAliveIncrementer'])
self.time_alive.add(cfg['RandomTimeAliveIncrementer'], attributes=common_attributes)

# Updates the currently active threads based on its current bounds.
def update_threads_active(self, cfg=None):
if self.thread_bool:
if self.thread_count < cfg['RandomThreadsActiveUpperBound']:
self.threads_active.add(1)
self.threads_active.add(1, attributes=common_attributes)
self.thread_count += 1
else:
self.thread_bool = False
self.thread_count -= 1

else:
if self.thread_count > 0 :
self.threads_active.add(-1)
self.threads_active.add(-1, attributes=common_attributes)
self.thread_count -= 1
else:
self.thread_bool = True
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
boto3==1.26.45
boto3==1.26.84
Flask==2.2.2
opentelemetry-distro==0.36b0
opentelemetry-exporter-otlp==1.12.0rc2
opentelemetry-api==1.15.0
opentelemetry-instrumentation-flask==0.36b0
opentelemetry-instrumentation-requests==0.36b0
opentelemetry-instrumentation-botocore==0.36b0
opentelemetry-distro==0.37b0
opentelemetry-exporter-otlp==1.16.0
opentelemetry-api==1.16.0
opentelemetry-instrumentation-flask==0.37b0
opentelemetry-instrumentation-requests==0.37b0
opentelemetry-instrumentation-botocore==0.37b0
opentelemetry-sdk-extension-aws==2.0.1
opentelemetry-propagator-aws-xray==1.0.1
protobuf==3.20.2
PyYAML==6.0
requests==2.28.1
requests==2.28.2

0 comments on commit 421535c

Please sign in to comment.