Log-based metrics allow a logging agent to record certain number of logs of certain types of messages (such as error logs) over certain period of time. There are system-defined log-based metrics and user-defined log-based metrics. To create one, perform following steps:
- Go to Stackdriver logging | logs and click on the CREATE METRIC button.
- Name your metric. We will name them my-metric and describe its use. Provide labels as required. Here, we will give it as resource.type=“audited_resource”.
- Click on Create to create the metric.
- You can edit or delete the metric once it is created by clicking on the three dots at the right end of the metric name.
- Optionally, you can also create an alert for the metric from the same popup.
- Clicking on it will guide you to Stackdriver UI. Here, you can mention the condition of the alert. We have asked for log entries above 4 for 5 minutes in past 1 hour.
- Click on Save Condition to save the rule. Optionally, you can also define documentation for it by clicking on +Add Documentation.
- Finally, save the policy.
Thus, this is how we create alerting policies for Stackdriver logs in GCP. Now, we will discuss the monitoring aspects of Stackdriver in GCP.