How-To: Set up Azure Monitor to search logs and collect metrics

Enable Dapr metrics and logs with Azure Monitor for Azure Kubernetes Service (AKS)

Prerequisites

Enable Prometheus metric scrape using config map

  1. Make sure that omsagents are running
  1. $ kubectl get pods -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. ...
  4. omsagent-75qjs 1/1 Running 1 44h
  5. omsagent-c7c4t 1/1 Running 0 44h
  6. omsagent-rs-74f488997c-dshpx 1/1 Running 1 44h
  7. omsagent-smtk7 1/1 Running 1 44h
  8. ...
  1. Apply config map to enable Prometheus metrics endpoint scrape.

You can use azm-config-map.yaml to enable prometheus metrics endpoint scrape.

If you installed Dapr to the different namespace, you need to change the monitor_kubernetes_pod_namespaces array values. For example:

  1. ...
  2. prometheus-data-collection-settings: |-
  3. [prometheus_data_collection_settings.cluster]
  4. interval = "1m"
  5. monitor_kubernetes_pods = true
  6. monitor_kubernetes_pods_namespaces = ["dapr-system", "default"]
  7. [prometheus_data_collection_settings.node]
  8. interval = "1m"
  9. ...

Apply config map:

  1. kubectl apply -f ./azm-config.map.yaml

Install Dapr with JSON formatted logs

  1. Install Dapr with enabling JSON-formatted logs
  1. helm install dapr dapr/dapr --namespace dapr-system --set global.logAsJson=true
  1. Enable JSON formatted log in Dapr sidecar and add Prometheus annotations.

Note: OMS Agent scrapes the metrics only if replicaset has Prometheus annotations.

Add dapr.io/log-as-json: "true" annotation to your deployment yaml.

Example:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pythonapp
  5. namespace: default
  6. labels:
  7. app: python
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: python
  13. template:
  14. metadata:
  15. labels:
  16. app: python
  17. annotations:
  18. dapr.io/enabled: "true"
  19. dapr.io/app-id: "pythonapp"
  20. dapr.io/log-as-json: "true"
  21. prometheus.io/scrape: "true"
  22. prometheus.io/port: "9090"
  23. prometheus.io/path: "/"
  24. ...

Search metrics and logs with Azure Monitor

  1. Go to Azure Monitor

  2. Search Dapr logs

Here is an example query, to parse JSON formatted logs and query logs from dapr system processes.

  1. ContainerLog
  2. | extend parsed=parse_json(LogEntry)
  3. | project Time=todatetime(parsed['time']), app_id=parsed['app_id'], scope=parsed['scope'],level=parsed['level'], msg=parsed['msg'], type=parsed['type'], ver=parsed['ver'], instance=parsed['instance']
  4. | where level != ""
  5. | sort by Time
  1. Search metrics

This query, queries process_resident_memory_bytes Prometheus metrics for Dapr system processes and renders timecharts

  1. InsightsMetrics
  2. | where Namespace == "prometheus" and Name == "process_resident_memory_bytes"
  3. | extend tags=parse_json(Tags)
  4. | project TimeGenerated, Name, Val, app=tostring(tags['app'])
  5. | summarize memInBytes=percentile(Val, 99) by bin(TimeGenerated, 1m), app
  6. | where app startswith "dapr-"
  7. | render timechart

References

Last modified March 18, 2021: Merge pull request #1321 from dapr/aacrawfi/logos (9a399d5)