The background,

Company client project is based on the development of the Electron, it integrates some third-party plug-ins and library, often will encounter some compatibility and performance problems, is now through user feedback, operations to remote assistance, treatment is not the will of the bug to the developer, tested and difficult to recreate part of the problem, so is difficult to handle. Therefore, it occurred to me to output error log files and error alarm reports, and then send the error information to the enterprise wechat. Easy to locate and troubleshoot problems.

Second, open the robot permission

Firstly, we need to create a robot in the wechat group of the enterprise, called log monitoring robot, which is specially used to report Sentry error alarms, as shown in the figure below

Because the format of data packets sent by Sentry calling Webhook is inconsistent with that required by the enterprise wechat robot, Node service needs to be set up to convert the data packets of Sentry down and send them according to the format of data packets of the enterprise wechat robot.

Data message format required by enterprise wechat robot:

{
    "msgtype": "text"."text": {
        "content": "Weather in Guangzhou today: 29 degrees, mostly cloudy, chance of rain: 60%"."mentioned_list": ["wangqing"."@all"]."mentioned_mobile_list": ["13800001111"."@all"]}}Copy the code

Format of data packets sent by Sentry:

{
  id: '7'.project: 'xx'.project_name: 'xx'.project_slug: 'xx'.logger: null.level: 'error'.culprit: 'raven.scripts.runner in main'.message: 'This is an example Python exception'.url: 'http://sentry.xxxxxxx.com/organizations/sentry/issues/7/?referrer=webhooks_plugin'.triggering_rules: [].event: {
    event_id: 'f602ac321ee04bc28a20c9f4d446ef48'.level: 'error'.version: '5'.type: 'default'.logentry: {
      formatted: 'This is an example Python exception'.message: null.params: null
    },
    logger: ' '.modules: { 'my.package': '1.0.0' },
    platform: 'python'.timestamp: 1622734089.769465.received: 1622734089.7702.environment: 'prod'.user: {
      id: '1'.email: '[email protected]'.ip_address: '127.0.0.1'.username: 'sentry'.name: 'Sentry'.geo: [Object]},request: {
      url: 'http://example.com/foo'.method: 'GET'.data: [Object].query_string: [Array].cookies: [Array].headers: [Array].env: [Object].inferred_content_type: 'application/json'.fragment: null},... }}Copy the code

3. Build the Node service

Build a Node service quickly based on egg.js

npm init egg-message-notification --type=saimple
npm i
Copy the code

Start the project

npm run dev
open http://localhost:7001
Copy the code

The following figure shows that the startup was successful

The route configuration is as follows

Create a SentryController in app/controller/sentry.js, which is responsible for converting data packets.

'use strict';

const Controller = require('egg').Controller;
const axios = require('axios');
const CircularJSON = require('circular-json');

/** * Formats the current time */
const fmtDateTime = () = > {
  const date = new Date(a);const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let hour = date.getHours();
  let min = date.getMinutes();

  month = month < 10 ? ` 0${month}` : month;
  hour = hour < 10 ? ` 0${hour}` : hour;
  min = min < 10 ? ` 0${min}` : min;

  return `${year}-${month}-${date.getDate()} ${hour}:${min}`;
};

class SentryController extends Controller {
  /** * Receive Webhook from Sentry */
  async recvSentryWebhook() {
    const { ctx } = this;
    const { request: { body } } = ctx;
    const error = body.data && body.data.error || {};

    ctx.logger.info(body);

    const ROBOT_DATA = {
      msgtype: 'markdown'.markdown: {
        content: `<font color=\"warning\">${error.release || error.extra._productName}<font color= "info ">${error.title}<font color= "info ">${fmtDateTime()}<font color=\"${error.level === 'fatal' ? '#FF0000' : '# 008000'}\ ">${error.level}</font> >${error.web_url}) `,}};const result = await axios({
      url: 'Enterprise wechat robot Webhook Address'.method: 'POST'.headers: {
        'content-type': 'application/json',},data: JSON.stringify(ROBOT_DATA),
    });

    ctx.body = {
      status: 'success'.data: CircularJSON.stringify(result),
      msg: 'Remind success'}; }}module.exports = SentryController;
Copy the code

Finally, don’t forget to configure CSRF by adding the following code to config/config.default.js

config.security = {
  csrf: {
    enable: false,}};Copy the code

Deploy the Node service

What I did was to submit the code to git repository, clone the server, install the dependencies, and execute NPM run start to name and start the service. As shown in the following figure, it was successfully started

Add internal integration

Vi. Enterprise wechat information notification