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

Deploy to aks #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions azure-vote/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ RUN pip install opencensus-ext-azure
RUN pip install opencensus-ext-flask
RUN pip install opencensus-ext-logging
RUN pip install flask
RUN pip install opencensus-ext-requests
# Copy the content of the current directory to the /app of the container
ADD . /app
79 changes: 66 additions & 13 deletions azure-vote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,56 @@
from datetime import datetime

# App Insights
# TODO: Import required libraries for App Insights
from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.azure.log_exporter import AzureEventHandler
from opencensus.ext.azure import metrics_exporter
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import stats as stats_module
from opencensus.stats import view as view_module
from opencensus.tags import tag_map as tag_map_module
from opencensus.trace import config_integration
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
# For metrics
stats = stats_module.stats
view_manager = stats.view_manager

# Logging
logger = # TODO: Setup logger
config_integration.trace_integrations(['logging'])
config_integration.trace_integrations(['requests'])
# Standard Logging
logger = logging.getLogger(__name__)
handler = AzureLogHandler(connection_string='InstrumentationKey=a5ab3fd1-7295-40ce-9752-dda04163d715')
handler.setFormatter(logging.Formatter('%(traceId)s %(spanId)s %(message)s'))
logger.addHandler(handler)
# Logging custom Events
logger.addHandler(AzureEventHandler(connection_string='InstrumentationKey=a5ab3fd1-7295-40ce-9752-dda04163d715'))
# Set the logging level
logger.setLevel(logging.INFO)

# Metrics
exporter = # TODO: Setup exporter
exporter = metrics_exporter.new_metrics_exporter(
enable_standard_metrics=True,
connection_string='InstrumentationKey=a5ab3fd1-7295-40ce-9752-dda04163d715')
view_manager.register_exporter(exporter)

# Tracing
tracer = # TODO: Setup tracer

tracer = Tracer(
exporter=AzureExporter(
connection_string='InstrumentationKey=a5ab3fd1-7295-40ce-9752-dda04163d715'),
sampler=ProbabilitySampler(1.0),
)
app = Flask(__name__)

# Requests
middleware = # TODO: Setup flask middleware
middleware = FlaskMiddleware(
app,
exporter=AzureExporter(connection_string="InstrumentationKey=a5ab3fd1-7295-40ce-9752-dda04163d715"),
sampler=ProbabilitySampler(rate=1.0)
)

# Load configurations from environment or config file
app.config.from_pyfile('config_file.cfg')
Expand All @@ -42,8 +77,20 @@
else:
title = app.config['TITLE']

# Redis Connection
r = redis.Redis()
# Redis configurations
redis_server = os.environ['REDIS']

# Redis Connection to another container
try:
if "REDIS_PWD" in os.environ:
r = redis.StrictRedis(host=redis_server,
port=6379,
password=os.environ['REDIS_PWD'])
else:
r = redis.Redis(redis_server)
r.ping()
except redis.ConnectionError:
exit('Failed to connect to Redis, terminating.')

# Change title to host name to demo NLB
if app.config['SHOWHOST'] == "true":
Expand All @@ -60,9 +107,15 @@ def index():

# Get current values
vote1 = r.get(button1).decode('utf-8')
# TODO: use tracer object to trace cat vote
vote2 = r.get(button2).decode('utf-8')
# TODO: use tracer object to trace dog vote
with tracer.span(name='Cat vote') as span:
vote1 = r.get(button1).decode('utf-8')
properties = {'custom_dimensions': {'Cats Vote': vote1}}
span.add_attribute('custom_dimensions', properties)
vote2 = r.get(button2).decode('utf-8')
with tracer.span(name='Dog vote') as span:
vote2 = r.get(button2).decode('utf-8')
properties = {'custom_dimensions': {'Dogs Vote': vote2}}
span.add_attribute('custom_dimensions', properties)

# Return index with values
return render_template("index.html", value1=int(vote1), value2=int(vote2), button1=button1, button2=button2, title=title)
Expand All @@ -76,11 +129,11 @@ def index():
r.set(button2,0)
vote1 = r.get(button1).decode('utf-8')
properties = {'custom_dimensions': {'Cats Vote': vote1}}
# TODO: use logger object to log cat vote
logger.info('Cat vote recorded', extra=properties)

vote2 = r.get(button2).decode('utf-8')
properties = {'custom_dimensions': {'Dogs Vote': vote2}}
# TODO: use logger object to log dog vote
logger.info('Dog vote recored', extra=properties)

return render_template("index.html", value1=int(vote1), value2=int(vote2), button1=button1, button2=button2, title=title)

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3'
services:
azure-vote-back:
image: mcr.microsoft.com/oss/bitnami/redis:6.0.8
platform: linux/amd64
container_name: azure-vote-back
environment:
ALLOW_EMPTY_PASSWORD: "yes"
Expand Down
25 changes: 23 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
cachetools==5.4.0
certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
Flask==1.1.2
google-api-core==1.34.1
google-auth==2.33.0
googleapis-common-protos==1.63.2
idna==3.7
itsdangerous==1.1.0
Jinja2==3.0.3
MarkupSafe==2.1.5
opencensus==0.7.13
opencensus-context==0.1.2
opencensus-ext-azure==1.0.4
opencensus-ext-flask==0.7.3
redis==3.5.3
opencensus==0.7.13
opencensus-ext-logging==0.1.0
protobuf==3.20.3
psutil==6.0.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
redis==3.5.3
requests==2.32.3
rsa==4.9
six==1.16.0
urllib3==2.2.2
Werkzeug==0.16.1
Loading