Lambda monitoring ================= .. _`monitoring`: .. _influx database: https://www.influxdata.com/products/influxdb-overview/ .. _clickhouse database: https://clickhouse.com/ Data for monitoring ------------------- Now we monitor two types of events for monitoring: *request* and *error*. First type is all requests, second is failed requests only. **Comparison of data formats for Clickhouse and InfluxDB:** **InfluxDB:** Each event is presented as a "point" in a time series. The structure of a point includes: - series name - start event time - tags, indexed data in storage, dictionary: keys - string tag names, values - string, integer, float - fields, non indexed data in storage, dictionary: keys - string tag names, values - string, integer, float **Clickhouse:** In Clickhouse, the data structure resembles that of a traditional SQL table. Each event is represented as a **record**, where: - The **`time`** field contains the record's creation timestamp; - The **`data`** field contains a JSON object with all the information that would otherwise be distributed across tags and fields in InfluxDB. **Important**: In Clickhouse, there is no differentiation between "tags" and "fields"—all data is consolidated into a single JSON object within the `data` field. Monitoring series ----------------- The structure and the meaning of each monitoring series remain consistent. However, for **Clickhouse**, data from tags and fields are merged into a single JSON object under the `data` field. Below are examples for each series: * *Requests* series. Triggered on every request. Each point contains a data about corresponding request (execution time and etc). **InfluxDB:** - tags +--------------+----------------------------------------------------------------------------+ | tag name | description | +==============+============================================================================+ | service | always "lambda-" | +--------------+----------------------------------------------------------------------------+ | route | concatenation of a request method and a request resource (POST:/main) | +--------------+----------------------------------------------------------------------------+ | status_code | http status code of response | +--------------+----------------------------------------------------------------------------+ - fields +----------------+------------------------+ | fields | description | +================+========================+ | request_id | request id | +----------------+------------------------+ | execution_time | request execution time | +----------------+------------------------+ **ClickHouse JSON `data` field Example:** .. code-block:: json { "service": "lambda-", "route": "POST:/main", "status_code": 200, "request_id": "1536751345,6a5c2191-3e9b-f5a4-fc45-3abf43625c5f", "execution_time": 123.45 } * *Errors* series. Triggered on failed request. Each point contains *error_code* of luna error. **InfluxDB:** - tags +--------------+----------------------------------------------------------------------------+ | tag name | description | +==============+============================================================================+ | service | always "lambda-" | +--------------+----------------------------------------------------------------------------+ | route | concatenation of a request method and a request resource (POST:/main) | +--------------+----------------------------------------------------------------------------+ | status_code | http status code of response | +--------------+----------------------------------------------------------------------------+ | error_code | luna error code | +--------------+----------------------------------------------------------------------------+ - fields +----------------+------------------------+ | fields | description | +================+========================+ | request_id | request id | +----------------+------------------------+ **ClickHouse JSON `data` field Example:** .. code-block:: json { "service": "lambda-", "route": "POST:/main", "status_code": 400, "error_code": 13037, "request_id": "1536751345,6a5c2191-3e9b-f5a4-fc45-3abf43625c5f", } Custom Monitoring ----------------- For base monitoring usage information see `monitoring <./monitoring.html>`_. It is possible to create custom monitoring points. For example, there is a need to send data about how long it took to download or process images. It is possible to specify your own **series**, **tags** and **fields**, but there will always be a mandatory **tag** "service" with the "lambda-" value. For add custom monitoring point follow steps: 1) Add file **monitoring_points.py** to lambda archive with the following content: .. literalinclude:: examples/201/monitoring_lambda/lambda/monitoring_points.py :caption: monitoring_points.py :language: python There are several rules for this file: - The number of points can be any. - Each point class name may be specify by any unique name. - Each point class must be inherited by **CustomMonitoringPoint** - Each point class must contain **series** attribute to specify monitoring series. 2) Set monitoring points from **monitoring_points.py** to **lambda_main.py** and specify **tags** and **fields**: There are several general rules for this file: - Enable monitoring. See **INFLUX_MONITORING** setting in basic `principles of configuration <./config.html>`_. - If monitoring is unavailable, the points will not be sent without any errors. - Specify tags by set dictionary with tags to **pointTags** named argument. - Specify fileds by set dictionary with fields to **pointFields** named argument. .. warning:: There are diferencies between standalone, handlers and tasks lambda monitoring mechanism. See description below. Send points using **request.sendToMonitoring** function for **standalone** or **handlers** lambda: .. literalinclude:: examples/201/monitoring_lambda/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/monitoring_lambda/make_request.py :caption: request example :language: python Send points using **self.sendToMonitoring** function for **tasks** lambda: To run lambda tasks example, refer to the task processing description `here <./tasks.html#task-processing>`_. .. literalinclude:: examples/201/monitoring_lambda_task/lambda/lambda_main.py :caption: lambda_main.py :language: python .. literalinclude:: examples/201/monitoring_lambda_task/make_request.py :caption: request example :language: python Database -------- You can refer to documentation for `influx database`_ and `clickhouse database`_ to compare the databases and choose what benefit your needs more. Note that clickhouse might be the better choice for aggregation You can setup your database credentials in `configuration file `_ in section *"monitoring"*.