Monitoring

OpenShift includes Prometheus, Alertmanager and a metrics dashboard, so on OpenShift you don’t need to run your own, as in the Observability section. It can also monitor your own applications, using the /metrics page that’s already there.

Turning it on#

Monitoring of your own projects is called user workload monitoring, and a cluster administrator turns it on once, for the whole cluster, with a ConfigMap:

cluster-monitoring-config.yamlyaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    enableUserWorkload: true
oc login -u kubeadmin https://api.crc.testing:6443
oc apply -f cluster-monitoring-config.yaml

OpenShift Local turns off cluster monitoring by default, to save memory. Turn it on (it needs about 14 GB of memory for the cluster) with crc config set enable-cluster-monitoring true, then crc stop and crc start. The Developer Sandbox already has user workload monitoring on.

Scraping the application#

Tell OpenShift’s Prometheus to scrape the application with a ServiceMonitor:

openshift/servicemonitor.yamlyaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-for-you
  labels:
    app: example-for-you
spec:
  selector:
    matchLabels:
      app: example-for-you
  endpoints:
    - port: http
      path: /metrics
      interval: 30s

It finds the Service by its app label, and scrapes /metrics on the Service’s http port every 30 seconds. Back as developer:

oc apply -f openshift/servicemonitor.yaml

In the web console’s Developer perspective, go to Observe, Metrics, and query, for example:

sum by (handler) (rate(http_requests_total[5m]))

The custom counter is there too, as home_page_hits_total. Alerting rules work the same way, with a PrometheusRule resource containing the same rules as the Observability section.

Logs#

OpenShift collects the output of every container, which is why the application writes its logs to standard output in the container rather than to files. oc logs and the web console show them. For searching and keeping logs, OpenShift’s logging (based on Loki) can be installed by an administrator.