Skip to content

Commit 13c7657

Browse files
author
Bhavesh
committed
v1.0.3.3.4.2019
1 parent 0546733 commit 13c7657

File tree

7 files changed

+243
-0
lines changed

7 files changed

+243
-0
lines changed
Binary file not shown.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import json
2+
import numpy as np
3+
import requests
4+
import time
5+
from flask import Flask, request, jsonify
6+
from flasgger import Swagger
7+
from flasgger.utils import swag_from
8+
from flasgger import LazyString, LazyJSONEncoder
9+
from py_zipkin.zipkin import create_http_headers_for_new_span
10+
from py_zipkin.zipkin import zipkin_span
11+
from py_zipkin.zipkin import ZipkinAttrs
12+
13+
ZIPKIN_SERVER = 'http://localhost:9411/api/v1/spans'
14+
15+
def http_transport(encoded_span):
16+
body = encoded_span
17+
response = requests.post(ZIPKIN_SERVER, data=body, headers={
18+
'Content-Type': 'application/x-thrift'})
19+
if response.ok:
20+
print("HTTP trace posted to Zipkin")
21+
else:
22+
print("response: {}".format(response.content))
23+
print("HTTP trace not posted to Zipkin")
24+
return 1
25+
26+
27+
@zipkin_span(service_name='square_api', span_name='computing_the_square')
28+
def square_nums(num):
29+
output = {"square_of_number": 0}
30+
square_of_numbers = num ** 2
31+
time.sleep(3)
32+
output["square_of_number"] = square_of_numbers
33+
return output
34+
35+
36+
app = Flask(__name__)
37+
app.config["SWAGGER"] = {"title": "Swagger-UI", "uiversion": 2}
38+
39+
swagger_config = {
40+
"headers": [],
41+
"specs": [
42+
{
43+
"endpoint": "apispec_1",
44+
"route": "/apispec_1.json",
45+
"rule_filter": lambda rule: True, # all in
46+
"model_filter": lambda tag: True, # all in
47+
}
48+
],
49+
"static_url_path": "/flasgger_static",
50+
# "static_folder": "static", # must be set by user
51+
"swagger_ui": True,
52+
"specs_route": "/swagger/",
53+
}
54+
55+
template = dict(
56+
swaggerUiPrefix=LazyString(lambda: request.environ.get("HTTP_X_SCRIPT_NAME", ""))
57+
)
58+
59+
app.json_encoder = LazyJSONEncoder
60+
swagger = Swagger(app, config=swagger_config, template=template)
61+
62+
63+
@app.route("/")
64+
def index():
65+
with zipkin_span(
66+
service_name='square_api',
67+
span_name='index_span',
68+
transport_handler=http_transport,
69+
sample_rate=100):
70+
return "Square the inputted Number!"
71+
72+
73+
@app.route("/square_numbers", methods=["POST"])
74+
@swag_from("swagger_config_square.yml")
75+
def square_number():
76+
with zipkin_span(
77+
service_name='square_api',
78+
zipkin_attrs=ZipkinAttrs(
79+
trace_id=request.headers['X-B3-TraceID'],
80+
span_id=request.headers['X-B3-SpanID'],
81+
parent_span_id=request.headers['X-B3-ParentSpanID'],
82+
flags=request.headers['X-B3-Flags'],
83+
is_sampled=request.headers['X-B3-Sampled'],
84+
),
85+
span_name='receive_inp_and_run_square_method',
86+
transport_handler=http_transport,
87+
sample_rate=100,
88+
port=7000,
89+
):
90+
input_json = request.get_json()
91+
try:
92+
inp_num = int(input_json["num"])
93+
res = square_nums(inp_num)
94+
except:
95+
res = {"success": False, "message": "Unknown error"}
96+
97+
return json.dumps(res)
98+
99+
100+
if __name__ == "__main__":
101+
app.run(debug=True, port=7000)

swagger_flask_with_zipkin/sum_api.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import json
2+
import numpy as np
3+
import requests
4+
from flask import Flask, request, jsonify
5+
from flasgger import Swagger
6+
from flasgger.utils import swag_from
7+
from flasgger import LazyString, LazyJSONEncoder
8+
from py_zipkin.zipkin import create_http_headers_for_new_span
9+
from py_zipkin.zipkin import zipkin_span
10+
from py_zipkin.zipkin import ZipkinAttrs
11+
12+
ZIPKIN_SERVER = 'http://localhost:9411/api/v1/spans'
13+
SQUARE_URL = 'http://localhost:7000/square_numbers'
14+
15+
def http_transport(encoded_span):
16+
body = encoded_span
17+
response = requests.post(ZIPKIN_SERVER, data=body, headers={
18+
'Content-Type': 'application/x-thrift'})
19+
if response.ok:
20+
print("HTTP trace posted to Zipkin")
21+
else:
22+
print("response: {}".format(response.content))
23+
print("HTTP trace not posted to Zipkin")
24+
return 1
25+
26+
@zipkin_span(service_name='sum_api', span_name='adding_2_numbers')
27+
def add_2_numbers_and_square_it(num1, num2):
28+
output = {"sum_of_numbers": 0}
29+
sum_of_2_numbers = num1 + num2
30+
headers_new = create_http_headers_for_new_span()
31+
response_square = requests.post(
32+
SQUARE_URL, json={"num": sum_of_2_numbers}, headers=headers_new)
33+
if response_square.ok:
34+
resp_sq_json = response_square.json()
35+
output["sum_of_numbers"] = resp_sq_json['square_of_number']
36+
return output
37+
38+
39+
app = Flask(__name__)
40+
app.config["SWAGGER"] = {"title": "Swagger-UI", "uiversion": 2}
41+
42+
swagger_config = {
43+
"headers": [],
44+
"specs": [
45+
{
46+
"endpoint": "apispec_1",
47+
"route": "/apispec_1.json",
48+
"rule_filter": lambda rule: True, # all in
49+
"model_filter": lambda tag: True, # all in
50+
}
51+
],
52+
"static_url_path": "/flasgger_static",
53+
# "static_folder": "static", # must be set by user
54+
"swagger_ui": True,
55+
"specs_route": "/swagger/",
56+
}
57+
58+
template = dict(
59+
swaggerUiPrefix=LazyString(lambda: request.environ.get("HTTP_X_SCRIPT_NAME", ""))
60+
)
61+
62+
app.json_encoder = LazyJSONEncoder
63+
swagger = Swagger(app, config=swagger_config, template=template)
64+
65+
66+
@app.route("/")
67+
def index():
68+
with zipkin_span(
69+
service_name='sum_api',
70+
span_name='index_span',
71+
transport_handler=http_transport,
72+
sample_rate=100):
73+
return "Add 2 Numbers!"
74+
75+
76+
@app.route("/add_2_numbers", methods=["POST"])
77+
@swag_from("swagger_config.yml")
78+
def add_numbers():
79+
with zipkin_span(
80+
service_name="sum_api",
81+
span_name="receive_inp_and_run_add_method",
82+
transport_handler=http_transport,
83+
sample_rate=100,
84+
):
85+
input_json = request.get_json()
86+
try:
87+
num1 = int(input_json["x1"])
88+
num2 = int(input_json["x2"])
89+
res = add_2_numbers_and_square_it(num1, num2)
90+
except:
91+
res = {"success": False, "message": "Unknown error"}
92+
return json.dumps(res)
93+
94+
95+
if __name__ == "__main__":
96+
app.run(debug=True, port=7001)
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
summary: "Sqaure a Number"
2+
description: "This is a simple API which returns the square of a number"
3+
consumes:
4+
- "application/json"
5+
produces:
6+
- "application/json"
7+
parameters:
8+
- in: "body"
9+
name: "body"
10+
description: "Accepts a input dictionary of inputs x1 and x2"
11+
required: true
12+
schema:
13+
type: "object"
14+
properties:
15+
num:
16+
type: "integer"
17+
format: "int64"
18+
example : 212
19+
responses:
20+
405:
21+
description: "Invalid input"

swagger_with_flask/swagger_config.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
summary: "Add 2 Numbers"
2+
description: "This is a simple API which returns the sum of 2 numbers"
3+
consumes:
4+
- "application/json"
5+
produces:
6+
- "application/json"
7+
parameters:
8+
- in: "body"
9+
name: "body"
10+
description: "Accepts a input dictionary of inputs x1 and x2"
11+
required: true
12+
schema:
13+
type: "object"
14+
properties:
15+
x1:
16+
type: "integer"
17+
format: "int64"
18+
example : 45
19+
x2:
20+
type: "integer"
21+
format: "int64"
22+
example : 451
23+
responses:
24+
405:
25+
description: "Invalid input"

0 commit comments

Comments
 (0)