Project: HertzBeat (Observability and Analysis / Observability)
File: extend-http-example-token.md

id: extend-http-example-token title: Tutorial 2 Obtain TOKEN index value based on HTTP protocol for subsequent collection and authentication sidebar_label: Tutorial 2 Get TOKEN for subsequent authentication Through this tutorial, we will describe step by step how to modify on the basis of tutorial 1, add an metrics, first call the authentication interface to obtain the TOKEN, and use the TOKEN as a parameter for the subsequent metrics collection and authentication. Before reading this tutorial, we hope that you are familiar with how to customize types, metrics, protocols, etc. from and . Authentication information metrics (highest priority)HTTP interface carries account password call->Response data analysis->Analysis and issuance of TOKEN-accessToken as an metric ] -> [Assign accessToken as a variable parameter to other collection index groups] Here we still use the hertzbeat monitoring example of Tutorial 1! The hertzbeat background interface not only supports the basic direct account password authentication used in Tutorial 1, but also supports token authentication. We need `POST` to call the login interface `/api/account/auth/form` to get `accessToken`, the request body (json format) is as follows: ```json { "credential": "hertzbeat", "identifier": "admin" } ``` The response structure data is as follows: ```json { "data": { "token": "xxxx", "refreshToken": "xxxx" }, "msg": null, "code": 0 } ``` HertzBeat Dashboard -> Monitoring Templates -> New Template -> Config Monitoring Template Yml -> Save and Apply -> Add A Monitoring with The New Monitoring Type We define all monitoring collection types (mysql,jvm,k8s) as yml monitoring templates, and users can import these templates to support corresponding types of monitoring. Monitoring template is used to define the name of monitoring type(international), request parameter mapping, index information, collection protocol configuration information, etc. The custom monitoring type needs to add a new configuration monitoring template yml. We directly reuse the `hertzbeat` monitoring type in Tutorial 1 and modify it based on it A monitoring configuration definition file named after the monitoring type - hertzbeat_token We directly reuse the definition content in `hertzbeat` and modify it to our current monitoring type `hertzbeat_auth` configuration parameters, such as `app, category, etc`. ```yaml category: custom app: hertzbeat_token name: en-GB: HertzBeat Monitoring (Token) en-US: Hertz Beat Monitor (Token) params: field: host name: en-CN: Host Host en-US: Host type: host required: true field: port name: en-CN: port en-US: Port type: number range: '[0,65535]' required: true defaultValue: 1157 placeholder: 'Please enter the port' field: ssl name: en-CN: Enable SSL en-US: SSL type: boolean required: false field: contentType name: en-CN: Content-Type en-US: Content-Type type: text placeholder: 'Request Body Type' required: false field: payload name: en-CN: request BODY en-US: BODY type: textarea placeholder: 'Available When POST PUT' required: false metrics: .... ``` Add an index group definition `auth` in `hertzbeat_token`, set the collection priority to the highest 0, and collect the index

```yaml category: custom app: hertzbeat_token name: en-GB: HertzBeat Monitoring (Token) en-US: Hertz Beat Monitor (Token) params: field: host name: en-CN: Host Host en-US: Host type: host required: true field: port name: en-CN: port en-US: Port type: number range: '[0,65535]' required: true defaultValue: 1157 placeholder: 'Please enter the port' field: ssl name: en-CN: Enable SSL en-US: SSL type: boolean required: false field: contentType name: en-CN: Content-Type en-US: Content-Type type: text placeholder: 'Request Body Type' required: false field: payload name: en-CN: request BODY en-US: BODY type: textarea placeholder: 'Available When POST PUT' required: false metrics: name: auth priority: 0 fields: field: token type: 1 field: refreshToken type: 1 protocol: http http: host: ^^host^^ port: ^^port^^ url: /api/account/auth/form method: POST ssl: ^^ssl^^ payload: ^^payload^^ headers: content-type: ^^contentType^^ parseType: jsonPath parseScript: '$.data' ``` At this time, save and apply, add `hertzbeat_token` type monitoring on the system page, configure input parameters, `content-type` fill in `application/json`, `request Body` fill in the account password json as follows: ```json { "credential": "hertzbeat", "identifier": "admin" } ``` After the addition is successful, we can see the `token`, `refreshToken` metric data we collected on the details page. Add an index group definition `summary` in `app-hertzbeat_token.yml`, which is the same as `summary` in Tutorial 1, and set the collection priority to 1 Set the authentication method in the HTTP protocol configuration of this index group to `Bearer Token`, assign the index `token` collected by the previous index group `auth` as a parameter, and use `^o^` as the internal replacement symbol, that is ` ^o^token^o^`. as follows: ```yaml name: summary http: authorization: type: Bearer Token bearerTokenToken: ^o^token^o^ ``` The final `hertzbeat_token` template yml is defined as follows: ```yaml category: custom app: hertzbeat_token name: en-GB: HertzBeat Monitoring (Token) en-US: Hertz Beat Monitor (Token) params: field: host name: en-CN: Host Host en-US: Host type: host required: true field: port name: en-CN: port en-US: Port type: number range: '[0,65535]' required: true defaultValue: 1157 placeholder: 'Please enter the port' field: ssl name: en-CN: Enable SSL en-US: SSL type: boolean required: false field: contentType name: en-CN: Content-Type en-US: Content-Type type: text placeholder: 'Request Body Type' required: false field: payload name: en-CN: request BODY en-US: BODY type: textarea placeholder: 'Available When POST PUT' required: false metrics: name: auth priority: 0 fields: field: token type: 1 field: refreshToken type: 1 protocol: http http: host: ^^host^^ port: ^^port^^ url: /api/account/auth/form method: POST ssl: ^^ssl^^ payload: ^^payload^^ headers: content-type: ^^contentType^^ ^^headers^^: ^^headers^^ params: ^^params^^: ^^params^^ parseType: jsonPath parseScript: '$.data' name: summary priority: 1 fields: field: category type: 1 field: app type: 1 field: size type: 0 field: status type: 0 protocol: http http: host: ^^host^^ port: ^^port^^ url: /api/summary method: GET ssl: ^^ssl^^ authorization: type: Bearer Token bearerTokenToken: ^o^token^o^ parseType: jsonPath parseScript: '$.data.apps.*' ``` After the configuration is complete, save and apply, and check the monitoring details page Next, we can set the threshold normally. After the alarm is triggered, we can view it in the alarm center, add a new recipient, set alarm notification, etc. Have Fun!!! This is the end of the practice of custom monitoring of the HTTP protocol. The HTTP protocol also has other parameters such as headers and params. We can define it like postman, and the playability is also very high! If you think hertzbeat is a good open source project, please star us on GitHub Gitee, thank you very much. github: https://github.com/apache/hertzbeat gitee: https://gitee.com/hertzbeat/hertzbeat