In this use case, you need to automate anomaly detection. See how you can extract data from logs, create a processing rule, create a metric, and create an alert that generates a notification if an anomaly occurs.
Your logs contain important business data in a raw format. It needs to be extracted and normalized before analysis. The log records that you ingest have the following structure:
"timestamp": "2023-08-10T14:30:23.383000000+02:00","content": "AddItemAsync called with userId=04e412f0-c030-4d4b-9c16-72250b071fbd, productId=2ZYFJ3GM2N, quantity=4","event.type": "LOG","host.name": "gke-myhost-cos-bd5b5ae9-sad319","k8s.container.name": "cartservice",
You plan to extract the product identifier and quantity already at ingest time with a log processing rule, and then turn it into a metric with the same dimensions. Then you can track this data via the metric. Finally, you create an alert based on the metric values.
To build and run your query
fetch logs| filter matchesValue(k8s.container.name, "cartservice") and matchesPhrase(content, "AddItemAsync")| parse content, "LD 'productId=' LD:product ', quantity=' INT:quantity"| fields timestamp, product, quantity| summarize sum(quantity), by:{product, bin(timestamp, 1h)}| fieldsRename field:`bin(timestamp, 1h)`, alias:`hour`
This query performs the following actions:
cartservice
that contain the phrase AddItemAsync
in the log content.product
and quantity
.quantity
field for each product
per one-hour interval.hour
.To create a metric, you first need to create a processing rule that processes the incoming log data and then creates a metric based on log records matching the specific phrase. With each match, the processing rule extracts product ID and quantity values and places them in their respective fields. This provides you with additional dimensions for this metric (productId
and quantity
).
Product and Quantity from logs
matchesValue(k8s.container.name, "cartservice") and matchesPhrase(content, "AddItemAsync")
PARSE(content, "LD 'productId=' LD:product ', quantity=' INT:quantity")
This processing rule processes the incoming log data, extracts the product ID and quantity, and places their values in their respective fields.
log.
metric key: cartservice-product-quantity
matchesValue(k8s.container.name, "cartservice") and matchesPhrase(content, "AddItemAsync")
quantity
.product
.Check the result in Data Explorer:
log.cartservice-product-quantity
.sum
.product
.quantity
value for each product
graph.log.cartservice-product-quantity:splitBy(product):sum
).Go to Settings > Anomaly detection > Metric events and select Add metric event.
For Summary, enter Abnormal product quantity
.
In the Query definition section:
Metric selector
option for Type.log.cartservice-product-quantity:splitBy(product):sum
for Metric selector.In the Monitoring strategy section:
1
) for Number of signal fluctuations option.In the Event template section:
Abnormal product quantity
for Title.Select Save changes to create the metric event.
To check the problem alert created by your anomaly detection metric
Abnormal product quantity
problems.