StackLight provides a vast variety of metrics for MOSK
components. However, you may need to create a custom log-based metric to use it
for alert notifications, for example, in the following cases:
If a component producing logs does not expose scraping targets. In this case,
component-specific metrics may be missing.
If a scraping target lacks information that can be collected by aggregating
the log messages.
If alerting reasons are more explicitly presented in log messages.
For example, you want to receive alert notifications when more than 10 cases
are created in Salesforce within an hour. The sf-notifier scraping
endpoint does not expose such information. However, sf-notifier logs are
stored in OpenSearch and using prometheus-es-exporter you can perform the
following:
Configure a query using Query DSL (Domain Specific Language) and test it in
Dev Tools in in OpenSearch Dashboards.
Configure Prometheus Elasticsearch Exporter to expose the result as a
Prometheus metric showing the total amount of Salesforce cases created
daily, for example, salesforce_cases_daily_total_value.
Configure StackLight to send a notification once the value of this metric
increases by 10 or more within an hour.
Caution
StackLight logging must be enabled and functional.
Prometheus-es-exporter uses OpenSearch Search API. Therefore,
configured queries must be tuned for this specific API and must include:
The query part to filter documents
The aggregation part to combine filtered documents into a
metric-oriented result
In the manifest that opens, verify that StackLight logging is enabled:
logging:enabled:true
Create a query using Query DSL:
Select one of the following options:
Since Container Cloud 2.26.0 (Cluster releases 17.1.0 and
16.1.0)
In the OpenSearch Dashboards web UI, select an index to query.
StackLight stores logs in hourly OpenSearch indices.
Note
Optimize the query time by limiting the number of results.
For example, we will use the OpenSearch event.provider field
set to sf-notifier to limit the number of logs to search.
For example:
GET system/_search{"query":{"bool":{"filter":[{"term":{"event.provider":{"value":"sf-notifier"}}},{"range":{"@timestamp":{"gte":"now/d"}}}]}}}
Before Container Cloud 2.26.0 (Cluster releases 17.1.0 and
16.1.0)
In the OpenSearch Dashboards web UI, select an index to query.
StackLight stores logs in hourly OpenSearch indices. To select all
indices for a day, use the <logstash-{now/d}*> index pattern,
which stands for %3Clogstash-%7Bnow%2Fd%7D*%3E when URL-encoded.
Note
Optimize the query time by limiting the number of results.
For example, we will use the OpenSearch logger field set to
sf-notifier to limit the number of logs to search.
For example:
GET /%3Clogstash-%7Bnow%2Fd%7D*%3E/_search{"query":{"bool":{"must":{"term":{"logger":{"value":"sf-notifier"}}}}}}
Test the query in Dev Tools in OpenSearch Dashboards.
Select the log lines that include information about Salesforce cases
creation. For the info logging level, to indicate case creation,
sf-notifier produces log messages similar to the following one:
[2021-07-02 12:35:28,596] INFO in client: Created case: OrderedDict([('id', '5007h000007iqmKAAQ'), ('success', True), ('errors', [])]).
Such log messages include the Created case phrase. Use it in the query
to filter log messages for created cases:
Combine the query result to a single value that
prometheus-es-exporter will expose as a metric. Use the
value_count aggregation:
Since Container Cloud 2.26.0 (Cluster releases 17.1.0 and
16.1.0)
GET system/_search{"query":{"bool":{"filter":[{"term":{"event.provider":{"value":"sf-notifier"}}},{"range":{"@timestamp":{"gte":"now/d"}}},{"match_phrase_prefix":{"message":"Created case"}}]}},"aggs":{"daily_total":{"value_count":{"field":"event.provider"}}}}
Before Container Cloud 2.26.0 (Cluster releases 17.1.0 and
16.1.0)
GET /%3Clogstash-%7Bnow%2Fd%7D*%3E/_search{"query":{"bool":{"must":{"term":{"logger":{"value":"sf-notifier"}}},"filter":{"match_phrase_prefix":{"message":"Created case"}}}},"aggs":{"daily_total":{"value_count":{"field":"logger"}}}}
The aggregation result in Dev Tools should look as follows:
"aggregations":{"daily_total":{"value":19}}
Note
The metric name is suffixed with the aggregation name and
the result field name: salesforce_cases_daily_total_value.
In the example below, salesforce_cases is the query name. The final
metric name can be generalized using the
<query_name>_<aggregation_name>_<aggregation_result_field_name>
template.
Since Container Cloud 2.26.0 (Cluster releases 17.1.0 and
16.1.0)
prometheusServer:customAlerts:-alert:SalesforceCasesDailyWarningannotations:description:The number of cases created today in Salesforce increased by 10 within the last hour.summary:Too many cases in Salesforceexpr:increase(salesforce_cases_daily_total_value[1h]) >= 10labels:severity:warningservice:custom