Cloud Monitor Log Generation from Sample Application

Overview

Alibaba Cloud Monitor service allows monitoring of cloud resource usage and health of the application/resources by collecting various monitoring metrics. In this tutorial, we will see how Cloud Monitor can help us notified in case of an exception in an application. Sample application, which is used here to demonstrate the cloud monitor use case, is a simple web application for uploading file into OSS bucket.

  • Measure throughput or performance of the application from log entry timings.
  • Use Cloud Monitor Dashboard to monitor application health in real time

Architecture

In this below design, I have implemented a scenario where system administrator is notified by email if there is any failure while using file upload web application. The failure could be loss of connection between OSS and application or any other OSS technical/permission issue.

Prerequisite

This tutorial can easily be understood and followed if you are familiar with the below technologies and concepts,

  • Alibaba Cloud Environment
    If you do not have access, get 1 year free access with $10 credit using the below link
    Alibaba Cloud Free Account
  • Alibaba Log Service
    Click here to know basics of Cloud Message service
  • Alibaba Cloud Monitor
    Click here to know about Batch Compute product
  • Python Programing Knowledge and Flask Web Framework

Server Configurations

To develop and run the web application you will need a host server (Alibaba Cloud ECS or any other windows/unix Server) with python 2.7 installed.

  • Object Storage Service (OSS)
  • Cloud Monitor
  • Log Service

Configuring OSS, Log Service and Cloud Monitor

Please choose a single region for configuring the all the required Alibaba cloud services before you proceed. This will help you to avoid any technical challenges for integrating multi region services.

  1. Create a new bucket in your OSS console to store the files, which you are going to upload using this sample application.

Sample Application Walkthrough

Here is the folder and file structure,

sampleWebApplication│  common.py│  config.cfg|  config.py│  LICENSE│  Log.py│  README.md│  sampleweb.py└─── app│  routes.py│  __init__.py└─── templatesbase.htmlupload.html
AccessKeyId = <Cloud Account Access Key>AccessKeySecret = < Cloud Account Access Secret Key >LogEndpoint = <Log Service End points url>i.e. ap-southeast-1.log.aliyuncs.comLogProject = <Log Service Project Name>i.e. sampleapplicationLogstore = <Log Service Project Logstore name>i.e. sampleapplicationlogstoreOSSEndpoint = <OSS End Point url>i.e. oss-cn-shenzhen.aliyuncs.comOSSBucketName = <OSS Bucket Name>i.e. samplewebapp
# Read config.cfg and parse the configuration parametersparser = ConfigParser.ConfigParser()parser.read(cfg_fn)# Assign each of the configuration parameters values to separate varaiablesaccessKeyId = parser.get("Base", "AccessKeyId")accessKeySecret = parser.get("Base", "AccessKeySecret")logendpoint = parser.get("Base", "LogEndpoint")...return accessKeyId,accessKeySecret,logendpoint,logproject,logstore . . .
from common import Common
from aliyun.log.logitem import LogItemfrom aliyun.log.putlogsrequest import PutLogsRequest
@staticmethoddef write(client, project, logstore, topic, source, contents):
logItem = LogItem()logItem.set_time(int(time.time()))logItem.set_contents(contents)logitemList.append(logItem)
req = PutLogsRequest(project, logstore, topic, source, logitemList)res = client.put_logs(req)
set FLASK_APP=samplewebapp.pyflask run
from flask import Flaskapp = Flask(__name__)
{% block content %}<form action = "/upload" method = "POST"enctype = "multipart/form-data"><input type = "file" name = "file" /><input type = "submit"/></form>{% endblock %}
{% block content %}{% endblock %}
@app.route('/upload',methods = ['GET', 'POST'])def upload_file():if request.method == 'POST':. . . . .bucket.put_object(f.filename,'CustomUpload')flash('File uploaded successfully!'). . . . .return render_template('upload.html')
from aliyun.log.logclient import LogClientsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/..")from common import Commonfrom log import Log
accessKeyId,accessKeySecret,logEndPoint,logProject,logStore,ossEndPoint,ossBucketName,securityToken = Common.LoadConfig()
client = LogClient(logProject+'.'+logEndPoint, accessKeyId, accessKeySecret)
try:bucket.put_object(f.filename,'CustomUpload'). . . . .except Exception, e:contents = [('Msg', str(e) )]Log.write(client, logProject, logStore, '', '', contents). . . . .

Run and Test

Now let’s run the application.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com