Skip to content
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

Add span links #63

Merged
merged 11 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions applications/aws-lambdas/functions/cakes/cakes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json

from opentelemetry import trace

AVAILABLE_CAKES = ['cornetto', 'cannolo_siciliano', 'torta', 'muffin']


def is_avi(cake, context):
span_context = trace.SpanContext(context['trace_id'],
context['span_id'],
context['is_remote'],
context['trace_flags'],
context['trace_state'])
link = trace.Link(context=span_context)

tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("is_cake_available", links=[link]) as span:
span.set_attribute('requested_cake', cake)
if cake in AVAILABLE_CAKES:
span.set_attribute('is_avi', True)
status = 200
else:
span.set_attribute('is_avi', False)
status = 404

return status


def lambda_handler(event, context):
data = json.loads(event)
cake = data['cakes']
link_context = data['link_context']

result = is_avi(cake, link_context)

if result == 200:
print('%s is available' % cake)
return {'statusCode': 200, 'body': {'cakes': cake}}
else:
print('%s is not available' % data['cakes'])
return {'statusCode': 404, 'body': {'cakes': cake}}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
client = boto3.client('lambda')


def invoke(sweets):
print('Invoke %s with data: %s' % (FUNCTION_NAME, sweets))
def invoke(cakes):
print('Invoke %s with data: %s' % (FUNCTION_NAME, cakes))
response = client.invoke(
FunctionName=FUNCTION_NAME,
InvocationType='RequestResponse',
Payload=json.dumps(sweets),
Payload=json.dumps(cakes),
)
return response


def get_sweets(event, context):
def get_cakes(event, context):
body = event['body']

print('Check if %s is available' % str(body))
Expand Down
1 change: 0 additions & 1 deletion applications/aws-lambdas/functions/sweets/requirements.txt

This file was deleted.

15 changes: 0 additions & 15 deletions applications/aws-lambdas/functions/sweets/sweets.py

This file was deleted.

62 changes: 31 additions & 31 deletions applications/aws-lambdas/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: The Coffee Bar App Lambda Functions
Resources:
SweetsApi:
CakesApi:
Type: AWS::Serverless::Api
Properties:
StageName: SweetsApi
StageName: CakesApi
TracingEnabled: true
OpenApiVersion: 3.0.2
CheckSweetsApi:
CheckCakesApi:
Type: AWS::Serverless::Api
Properties:
StageName: CheckSweetsApi
StageName: CheckCakesApi
TracingEnabled: true
OpenApiVersion: 3.0.2
SweetsFunction:
CakesFunction:
Type: AWS::Serverless::Function
Properties:
Handler: sweets.lambda_handler
Handler: cakes.lambda_handler
Runtime: python3.8
CodeUri: ./functions/sweets
CodeUri: ./functions/cakes
Description: Build AWS Observability layer
MemorySize: 512
Timeout: 35
Expand All @@ -42,7 +42,7 @@ Resources:
Variables:
SUMOLOGIC_HTTP_TRACES_ENDPOINT_URL: HTTP_TRACES_URL
AWS_LAMBDA_EXEC_WRAPPER: /opt/otel-instrument
OTEL_SERVICE_NAME: lambda-SweetsFunction
OTEL_SERVICE_NAME: lambda-CakesFunction
OTEL_RESOURCE_ATTRIBUTES: application=the-coffee-bar-app
OTEL_TRACES_SAMPLER: always_on
Layers:
Expand All @@ -51,15 +51,15 @@ Resources:
getEndpoint:
Type: Api
Properties:
RestApiId: !Ref SweetsApi
RestApiId: !Ref CakesApi
Path: /
Method: POST
CheckSweetsFunction:
CheckCakesFunction:
Type: AWS::Serverless::Function
Properties:
Handler: check_sweets.get_sweets
Handler: check_cakes.get_cakes
Runtime: python3.8
CodeUri: ./functions/check_sweets
CodeUri: ./functions/check_cakes
Description: Build AWS Observability layer
MemorySize: 512
Timeout: 35
Expand All @@ -81,54 +81,54 @@ Resources:
Variables:
SUMOLOGIC_HTTP_TRACES_ENDPOINT_URL: HTTP_TRACES_URL
AWS_LAMBDA_EXEC_WRAPPER: /opt/otel-instrument
OTEL_SERVICE_NAME: lambda‑CheckSweetsFunction
OTEL_SERVICE_NAME: lambda‑CheckCakesFunction
OTEL_RESOURCE_ATTRIBUTES: application=the-coffee-bar-app
OTEL_TRACES_SAMPLER: always_on
OTEL_PROPAGATORS: xray
INVOKE_FUNCTION_NAME: !Ref SweetsFunction
INVOKE_FUNCTION_NAME: !Ref CakesFunction
Layers:
- !FindInMap [RegionLayers, !Ref "AWS::Region", layer]
Events:
getEndpoint:
Type: Api
Properties:
RestApiId: !Ref CheckSweetsApi
RestApiId: !Ref CheckCakesApi
Path: /
Method: POST
Mappings:
RegionLayers:
ap-northeast-1:
layer: "arn:aws:lambda:ap-northeast-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:ap-northeast-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
ap-northeast-2:
layer: "arn:aws:lambda:ap-northeast-2:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:ap-northeast-2:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
ap-south-1:
layer: "arn:aws:lambda:ap-south-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:ap-south-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
ap-southeast-1:
layer: "arn:aws:lambda:ap-southeast-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:ap-southeast-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
ap-southeast-2:
layer: "arn:aws:lambda:ap-southeast-2:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:ap-southeast-2:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
ca-central-1:
layer: "arn:aws:lambda:ca-central-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:ca-central-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
eu-central-1:
layer: "arn:aws:lambda:eu-central-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:2"
layer: "arn:aws:lambda:eu-central-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
eu-north-1:
layer: "arn:aws:lambda:eu-north-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:eu-north-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
eu-west-1:
layer: "arn:aws:lambda:eu-west-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:eu-west-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
eu-west-2:
layer: "arn:aws:lambda:eu-west-2:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:eu-west-2:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
eu-west-3:
layer: "arn:aws:lambda:eu-west-3:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:eu-west-3:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
sa-east-1:
layer: " arn:aws:lambda:sa-east-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: " arn:aws:lambda:sa-east-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
us-east-1:
layer: "arn:aws:lambda:us-east-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:us-east-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:1"
us-east-2:
layer: "arn:aws:lambda:us-east-2:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:2"
layer: "arn:aws:lambda:us-east-2:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
us-west-1:
layer: "arn:aws:lambda:us-west-1:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:us-west-1:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
us-west-2:
layer: "arn:aws:lambda:us-west-2:663229565520:layer:sumologic-aws-distro-otel-col-python-lambda-layer:1"
layer: "arn:aws:lambda:us-west-2:663229565520:layer:sumologic-otel-python-x86_64-ver-1-10-0:2"
Metadata:
AWS::ServerlessRepo::Application:
Name: The-Coffee-Bar-App-Functions
Expand Down
20 changes: 10 additions & 10 deletions applications/js-the-coffee-bar-ui-clicker/src/clicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const COFFEE = {
'Cappuccino': [80, 94],
'Americano': [95, 100],
};
const SWEETS = {
const CAKES = {
'Cornetto': [0, 89],
'Tiramisu': [90, 94],
'Muffin': [95, 100],
Expand Down Expand Up @@ -270,17 +270,17 @@ const NAVIGATE_RETRY_SECONDS = 60;
// Add Coffee
await click(coffee_selectors['button'], DELAY);

// Select Sweets to order
let sweets = utils.choose(SWEETS);
let sweet_selectors = {
'input': `input[name="amount${sweets}"]`,
'button': `button[name="add${sweets}"]`,
// Select Cakes to order
let cakes = utils.choose(CAKES);
let cake_selectors = {
'input': `input[name="amount${cakes}"]`,
'button': `button[name="add${cakes}"]`,
}

// Set Sweets amount
await clickAndSetFieldValue(sweet_selectors['input'], utils.getRandomNumber(1, 3), DELAY);
// Add Sweets
await click(sweet_selectors['button'], DELAY);
// Set Cakes amount
await clickAndSetFieldValue(cake_selectors['input'], utils.getRandomNumber(1, 3), DELAY);
// Add Cakes
await click(cake_selectors['button'], DELAY);


// Checkout
Expand Down
2 changes: 1 addition & 1 deletion applications/python-the-coffee-bar-apps/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BASE_TAG = pythonAppsv0.2.0-ot1.10.1-0.28b1
BASE_TAG = pythonAppsv0.2.1-ot1.10.1-0.28b1

ifndef GITHUB_HEAD_REF
TAG = $(BASE_TAG)
Expand Down
56 changes: 33 additions & 23 deletions applications/python-the-coffee-bar-apps/src/bar/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask import make_response
import requests
import datetime
from opentelemetry import trace

from src.bar.coffee_machine import GET_COFFEE_ENDPOINT
from src.common.http_server import HttpServer
Expand All @@ -21,30 +22,30 @@ def set_product_status(response: requests.models.Response, status_key: str):

class Bar(HttpServer):
def __init__(self, coffee_machine_host: str, coffee_machine_port: str, cashdesk_host: str, cashdesk_port: int,
name: str = 'The Coffee Bar', host: str = 'localhost', port: int = 8082, sweets_url: str = None):
name: str = 'The Coffee Bar', host: str = 'localhost', port: int = 8082, cakes_url: str = None):
super().__init__(name, host, port)
self.coffee_machine_host = coffee_machine_host
self.coffee_machine_port = coffee_machine_port
self.cashdesk_host = cashdesk_host
self.cashdesk_port = cashdesk_port
self.sweets_url = sweets_url
self.cakes_url = cakes_url
self.add_all_endpoints()

def add_all_endpoints(self):
self.add_endpoint(endpoint='/order', endpoint_name='order',
handler=self.order)

def order(self, data):
# OPTIONAL: If sweets_url is set, get sweets_status
sweets_status = {'sweets_status': False}
if self.sweets_url:
if data['sweets_amount'] > 0:
sweets_status = self.get_sweets(data=data)
data.update(sweets_status)
# OPTIONAL: If cakes_url is set, get cakes_status
cakes_status = {'cakes_status': False}
if self.cakes_url:
if data['cakes_amount'] > 0:
cakes_status = self.get_cakes(data=data)
data.update(cakes_status)
else:
log.warning('Sweets were not requested.')
log.warning('Cakes were not requested.')
else:
log.warning('Sweets URL not configured. Sweets will be not provided.')
log.warning('Cakes URL not configured. Cakes will be not provided.')

# Get coffee_status
coffee_status = {'coffee_status': False}
Expand All @@ -54,36 +55,45 @@ def order(self, data):
else:
log.warning('Coffee was not requested.')

# Coffee Amount and Sweets Amount <= 0 - no order
if data['coffee_amount'] <= 0 and data['sweets_amount'] <= 0:
# Coffee Amount and Cakes Amount <= 0 - no order
if data['coffee_amount'] <= 0 and data['cakes_amount'] <= 0:
result = {
'reason': 'Please make an order.'
}
log.warning('Please make an order.')
return make_response(result, 204)

if coffee_status['coffee_status'] is True or sweets_status['sweets_status'] is True:
if coffee_status['coffee_status'] is True or cakes_status['cakes_status'] is True:
return self.process_payment(data=data)
else:
if coffee_status['coffee_status'] is False and sweets_status['sweets_status'] is False:
if coffee_status['coffee_status'] is False and cakes_status['cakes_status'] is False:
result = {
'reason': 'Lack of requested products: {}, {}'.format(data['coffee'], data['sweets']),
'reason': 'Lack of requested products: {}, {}'.format(data['coffee'], data['cakes']),
}
elif coffee_status['coffee_status'] is True and sweets_status['sweets_status'] is False:
elif coffee_status['coffee_status'] is True and cakes_status['cakes_status'] is False:
result = {
'reason': 'Lack of requested product: {}, {} provided.'.format(data['sweets'], data['coffee']),
'reason': 'Lack of requested product: {}, {} provided.'.format(data['cakes'], data['coffee']),
}
elif coffee_status['coffee_status'] is False and sweets_status['sweets_status'] is True:
elif coffee_status['coffee_status'] is False and cakes_status['cakes_status'] is True:
result = {
'reason': 'Lack of requested product: {}, {} provided.'.format(data['coffee'], data['sweets']),
'reason': 'Lack of requested product: {}, {} provided.'.format(data['coffee'], data['cakes']),
}
return make_response(result, 404)

def get_sweets(self, data):
log.info('Check if %s is available', data['sweets'])
response = requests.post(url=self.sweets_url, json=data)
def get_cakes(self, data):
log.info('Check if %s is available', data['cakes'])
context = trace.get_current_span().get_span_context()
data['link_context'] = {
'trace_id': int(context.trace_id),
'span_id': int(context.span_id),
'is_remote': bool(context.is_remote),
'trace_flags': int(context.trace_flags),
'trace_state': bool(context.trace_state),
}

return set_product_status(response=response, status_key='sweets_status')
response = requests.post(url=self.cakes_url, json=data)

return set_product_status(response=response, status_key='cakes_status')

def get_coffee(self, data):
start_time = datetime.datetime.now()
Expand Down
Loading