Skip to content

Commit f90e3e3

Browse files
committed
added code to make it easier to add tags to tagging workshop
1 parent 75850d6 commit f90e3e3

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# This setup script will replace the main.py file for the credit check service
4+
# with a version that already includes the tagging changes
5+
#
6+
cp creditcheckservice/main-with-tags.py creditcheckservice/main.py
7+
8+
echo ""
9+
echo Applied the tagging changes to creditcheckservice.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import requests
2+
from flask import Flask, request
3+
from waitress import serve
4+
from opentelemetry import trace # <--- ADDED BY WORKSHOP
5+
6+
app = Flask(__name__)
7+
8+
@app.route('/')
9+
def hello():
10+
return 'Hello'
11+
12+
@app.route('/test')
13+
def test_it():
14+
return 'OK'
15+
16+
@app.route('/check')
17+
def credit_check():
18+
current_span = trace.get_current_span() # <--- ADDED BY WORKSHOP
19+
customerNum = request.args.get('customernum')
20+
current_span.set_attribute("customer.num", customerNum) # <--- ADDED BY WORKSHOP
21+
22+
# Get Credit Score
23+
creditScoreReq = requests.get("http://creditprocessorservice:8899/getScore?customernum=" + customerNum)
24+
creditScoreReq.raise_for_status()
25+
creditScore = int(creditScoreReq.text)
26+
current_span.set_attribute("credit.score", creditScore) # <--- ADDED BY WORKSHOP
27+
28+
creditScoreCategory = getCreditCategoryFromScore(creditScore)
29+
current_span.set_attribute("credit.score.category", creditScoreCategory) # <--- ADDED BY WORKSHOP
30+
31+
# Run Credit Check
32+
creditCheckReq = requests.get("http://creditprocessorservice:8899/runCreditCheck?customernum=" + str(customerNum) + "&score=" + str(creditScore))
33+
creditCheckReq.raise_for_status()
34+
checkResult = str(creditCheckReq.text)
35+
current_span.set_attribute("credit.check.result", checkResult) # <--- ADDED BY WORKSHOP
36+
37+
return checkResult
38+
39+
def getCreditCategoryFromScore(score):
40+
creditScoreCategory = ''
41+
match score:
42+
case num if num > 850:
43+
creditScoreCategory = 'impossible'
44+
case num if 800 <= num <= 850 :
45+
creditScoreCategory = 'exceptional'
46+
case num if 740 <= num < 800 :
47+
creditScoreCategory = 'very good'
48+
case num if 670 <= num < 740 :
49+
creditScoreCategory = 'good'
50+
case num if 580 <= num < 670 :
51+
creditScoreCategory = 'fair'
52+
case num if 300 <= num < 580 :
53+
creditScoreCategory = 'poor'
54+
case _:
55+
creditScoreCategory = 'impossible'
56+
return creditScoreCategory
57+
58+
if __name__ == '__main__':
59+
serve(app, port=8888)

0 commit comments

Comments
 (0)