background

In the recruitment interview, MY favorite question is how to do stability assurance, that is, how to do online system monitoring.

  • How do I collect errors (JS script errors, Promise exceptions, 404 resource load failures, etc.)?
  • How do I collect the success rate of the statistics interface?
  • How do I count page load performance? What are the key metrics and dimensions?


For startup teams, many don’t care about monitoring.

  • One is that business development is already busy, so how much work is needed to develop access log monitoring?
  • The second is the log monitoring system it is a system, the front end buried point, collect logs. There are log receiving and log display services on the back end. Advanced log monitoring system also has alarm and automatic problem location, and even problem reoccurrence and location functions. Very complicated, develop your own or access a third party service?


As a development lead (or wanna-be), think about the product you’re in charge of. What if there’s no front-end log monitoring?

  • Your product is streaking online! There is a problem online customers have launched complaints, you do not know where to start, there is no way to start.
  • If a user page is abnormal, how can I quickly locate the fault remotely? Where is the error stack information?
  • What does your boss say about how the page is so slow and how to optimize load performance?
  • More importantly, your boss will sometimes say, this product launched, it feels like a dead end. These you can stand out, pat chest say: boss please rest assured, we have monitoring system 666, online stability has its own guarantee! Don’t worry about sleeping well!


Anyway, “If you cannot measure it, you cannot improve it.”

Therefore, based on the above pain points in actual production, we hope to have a front-end monitoring platform. It provides powerful online page monitoring capabilities that allow you to flexibly monitor business data, set alarms, and display monitoring data with visual diagrams to give you real-time insight and improve quality and performance.


Online search front-end monitoring system will come out of a lot of options, such as:

  1. They can plan and define a set of perfect monitoring system. Human redevelopment is needed.
  1. How to have enough manpower and money. You are advised to deploy ELK.
  1. Leverage/deploy third-party services/plug-ins
  • Ali ARMS: Is a front-end data monitoring service of Ali, which is paid for.
  • Fundebug: quite perfect front-end error log service, is also charged.
  • BadJS: An open source project by Tencent team. I haven’t seen it. It should be very good
  • Webfunny: Author is an individual, open source deployment, unused.
  • Sentry: No charge for individual projects, enterprise charges. However, it is an open source project on Github that supports error monitoring on all sides


Paid projects/own development from start to finish is not the first choice for a startup’s technical team. The cost is there. The strategy is to pick an open source project quickly, meet the current business needs first, and when the business picks up, if the open source project is not satisfied, then figure out how to upgrade or migrate to a paid project.

Therefore, this paper will be based on sentry quickly built error monitoring system, including server and client deployment (small program side)


Build sentry

The official website provides two deployment modes: 1. Docker and 2. Python source code deployment

Here I use docker to install, which is relatively fast

Server environment: CentOS 7.x


1. Use wGET to install docker

  • Install wget
$sudo apt-get update 
$ sudo apt-get install wgetCopy the code


  • Install the docker
 $ wget -qO- https://get.docker.com/ | sh
 $ docker --versionCopy the code

Use docker –version to check the version number and verify that the installation was successful.


Ps: Due to some mysterious reason, the image cannot be directly obtained from the official Docker library in China. Here we can use the docker accelerator to run the following command.

$ curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://4031ebb7.m.daocloud.ioCopy the code



Install Docker-compose with PIP

  • Install the PIP
$ sudo yum install -y python-pipCopy the code

  • The installation found that Python required an upgrade
$yum install python3 $python3 -v $cd /usr/bin
Back up the original default Python path
$ mv python python.bak
Link Python to python3
$ ln -sPython3 python $python -v displays the 3.x versionCopy the code

For details, please refer to www.cnblogs.com/leon-zyl/p/…


  • How does the installation process find out that PIP dependencies do not support Python 2.x, and that 3.x is required to upgrade first
$PIP -v PIP 20.0.2 from /usr/lib/python2.7 $rm /usr/bin/pip $ln-s /usr/local/bin/pip3.8 /usr/bin/pip $PIP -v PIP 19.2.3 from /usr/local/ lib/python3.8 / site - packages/PIP (python 3.8)Copy the code

  • Install Docker-compose with PIP
$sudo PIP install docker-compose $docker-compose --version // Check whether the version is successfully installedCopy the code

Docker – compose learning refer to: www.cnblogs.com/minseo/p/11…


3. Deploy Sentry

Clone sentry project $gitclone https://github.com/getsentry/onpremise.git
If you want to create an initial account, you will be asked if you want to create an initial account
$  bash install.sh
Start the container when done
$ docker-compose up -d
Copy the code




The web says click above, which allows us to monitor the system’s back-end servers are up and running, accessing local 9000 ports.

But my actual operation process was not successful. The secret-key is not configured.


$ docker-compose run --rm web config generate-secret-keyCopy the code

Here the production key is generated:

  • To install SENTRY_SECRET_KEY, add it to docker-comemage.yml. But vi docker-comemage. yml does not have this KEY, which should be a 2.0 version.
  • Env file. The vi. Env file does not have this KEY either

Later, I found that it was needed when the Docker service was started.


$ SENTRY_IMAGE=getsentry/sentry:10 ./install.sh
$ docker run -d --name sentry-redis redis
$ docker run -d --name sentry-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=sentry postgres
$ docker run --rm sentry config generate-secret-key
$ docker run -it --rm -e SENTRY_SECRET_KEY='Generated secret-key key string' --link sentry-postgres:postgres --link sentry-redis:redis sentry upgradeCopy the code

At this point an account will be created:



Ensure that there are no extra Spaces before and after the command line

$ docker run -d -p 9000:9000 --name my-sentry -e SENTRY_SECRET_KEY='Generated secret-key key string' --link sentry-redis:redis --link sentry-postgres:postgres sentry
$ docker run -d --name sentry-cron -e SENTRY_SECRET_KEY='Generated secret-key key string' --link sentry-postgres:postgres --link sentry-redis:redis sentry run cron
$ docker run -d --name sentry-worker-1 -e SENTRY_SECRET_KEY='Generated secret-key key string' --link sentry-postgres:postgres --link sentry-redis:redis sentry run workerCopy the code


At this point, sentry background service 9000 is running, use the account created just now to log in


2. Sentry Creates a project to obtain the monitoring report configuration




The code selected above is the one that needs to be configured before the front end calls the monitoring API.



Use sentry report in small program

Take Taro as an example.

 $ npm install --save sentry-weapp  
 Copy the code




src/app.js

import Raven from 'sentry-weapp'


 componentDidMount () {
    const options = {
      environment: 'production', // sampleRate: 1 is reported when production is specified. // 100% is reported. Sample 0.1 indicates 10%.true} // Above in the background project generated report address raven.config ('http://xxxxxxxxxxxxx@xxxxxxxxxx:9000/2', options).install()
    Taro.Logger = Raven;
 }Copy the code


The test business code says so

    Taro.Logger.captureMessage('Hello, world! '));
    // Taro.Sentry = Sentry;
    Taro.Logger.captureException('try to ', {
      level: 'error'
    })
   
    try{
      test() } catch (ex) { Taro.Logger.captureException(ex); // Reporting an exception}Copy the code



Backstage, the exception has been reported.




At last.

A mature error monitoring system is more than this, intelligent monitoring is a higher level of application. We will continue to share this practice in the future.


Like to read technical management articles, please pay attention to the public account “qunyu Bay”, grow up with you.