Observability
Observability is the ability to understand what happens inside a running application from four types of signals:
-
Metrics are statistics collected over many operations in a time interval: how many times an operation ran, its average and maximum duration, how much memory is used. A metric shows how the application behaves as a whole rather than individual operations.
-
Traces show the path of a single request through components and services, including requests handled by several applications: which operations the request consisted of and how long each of them took.
-
Logs are text records of events: what happened, for which user, with which error.
-
Profiles are samples of call stacks: in which methods the application spends CPU time, where it allocates memory and where threads wait for locks.
Jmix uses standard observability tools: logging through SLF4J and Logback, Spring Boot Actuator, Micrometer and Micrometer Observation, and the Pyroscope Java agent for profiles. The settings and export options for signals are the same as in any Spring Boot application.
Jmix-Specific Features
Observations of Data Loading, Views and UI Actions
Jmix wraps data loading and saving, the lifecycle stages of views, fragments and data loaders, and the execution of UI actions in Micrometer observations. A single observation produces both a metric and a trace span if the corresponding Spring Boot modules are added to the application.
The Jmix UI is built on Vaadin, and in a Vaadin application the standard Spring Boot metrics and traces are not enough: they do not show which view opens slowly or which user action takes long. The reason is that Vaadin sends all user actions, such as opening a view, clicking a button or typing in a field, as requests to a single URL. In the http.server.requests metric all these requests fall into one series POST /**, and their spans all have the same name http post /**. Jmix UI observations record the missing information: the view ID, its lifecycle stage, the data loader and the action.
| Observation | What is measured | Property that enables it |
|---|---|---|
|
Loading entities and values and counting records through |
|
|
Saving and removing entities through the same paths |
|
|
View lifecycle stages |
|
|
Fragment lifecycle stages |
|
|
Data loader stages |
|
|
Execution of UI actions |
Both properties are false by default.
User and Tenant Attributes in Observations
Observations get the username (jmix.user.username) and, in applications with multitenancy, the tenant ID (jmix.user.tenantId). This is controlled by the jmix.core.use-user-info-for-observation property, true by default.
Username in MDC
MDC (Mapped Diagnostic Context) is a set of key-value pairs that Logback keeps for the current thread and can output in every log line. Jmix puts the current username into MDC under the jmixUser key. If the log line format outputs this key, every line, including Jmix lines and SQL statements, shows on behalf of which user the code ran, and the lines of one user can be selected among the lines of concurrent requests from other users. See MDC: Log Record Context.
Email Sending Metric
The Email add-on records the time of sending emails in the jmix.EmailSender.send timer.
Connecting Metrics, Traces and Logs
Signals of different types complement each other, and when investigating a problem you move from one to another: from a metric to traces, from a trace to log lines and a profile. This is possible because the signals of one request share common identifiers: metrics and spans share tags, spans and log lines share trace and span IDs, and profiles carry the root span ID.
The metrics and spans of one observation have common tags: for example, the jmix.ui.views metric with the view.id=User.list tag and the spans of the same view. The metric points to a slow view, and the traces point to particular slow requests to it and the longest operations inside them. If metrics are collected from the prometheus endpoint, the Prometheus registry attaches trace IDs (exemplars) to timer values, and Grafana opens a trace directly from a point on the metric graph, see Exemplars.
If tracing is enabled in the application, Spring Boot adds the current trace and span IDs to every log line. The ID from a log line leads to the trace of the request, and the trace leads to all log lines written during its execution, including those in other services.
Profiles are linked to spans. If the Pyroscope span processor is added to tracing, the root span of a request opens the CPU or memory allocation profile recorded during that span, see Linking Profiles to Spans. A problem is investigated along the chain: the metric shows a slow view, the trace shows the longest operation of a request, the log shows the details of that operation, and the profile shows the methods that used CPU time and allocated memory.
This chain is used, in particular, for data access problems: an inefficient JPQL query, an incomplete fetch plan, loading in a loop (the N+1 problem), waiting for a connection from the pool, or a slow SQL query. The jmix.data.* metrics and the connection pool metrics show which entities are loaded often or slowly, the loading spans show the JPQL query and its parameters, and the EclipseLink log lines show the generated SQL statements and their execution time. See Performance Diagnostics for details.
For example, in the trace of one request, one application opens a list view, and the data loader of the view gets records through REST DataStore from a second application. The spans of both applications are collected in one tree: of the 99 ms of the request, the view took 78 ms, of which the data loader took 42 ms and the request processing in the second application took 31 ms.
Viewing Tools
Signals are exported by standard Spring Boot means, so you can choose any storage and viewing system. The application publishes metrics on the Actuator prometheus endpoint or sends them over the OTLP protocol. Traces and logs are sent over OTLP only.
The prometheus endpoint returns metrics in the Prometheus text format. Many monitoring systems support this format (VictoriaMetrics, Zabbix, OpenTelemetry Collector and others), so the endpoint can be scraped not only by Prometheus. Micrometer can also send metrics directly to other systems (Datadog, Elastic, InfluxDB and others), see Prometheus Endpoint.
The examples in the Jmix observability articles and guides use LGTM, a set of open source tools from Grafana Labs. Loki stores logs, Tempo stores traces, Prometheus stores metrics, and all three signal types are viewed in Grafana. To avoid installing and configuring each tool separately, Grafana Labs provides the grafana/otel-lgtm Docker image. The image contains all tools of the set, already configured and connected to each other. A container started from this image accepts application data over OTLP right away. Spring Boot recognizes this image: if it is described in the project’s Docker Compose file, the application gets the addresses for sending logs, metrics and traces at startup, see Development-time Services. The image is intended for development and demonstration; in production, the tools are deployed separately.
Grafana offers three ways to view signals:
-
Dashboards are preconfigured sets of visualizations for continuous monitoring. A visualization (Panel) is a single graph, table or number; it is built from a query to a data source and refreshes automatically. For Jmix metrics, there are ready-made dashboards in the jmix-grafana-dashboards repository. The "Jmix 3 Monitoring" dashboard shows the rate and duration of view opening by lifecycle stage, the slowest views, the time of data loaders, fragments and actions, data operations by entity, as well as the database connection pool, HTTP requests and the JVM. The "Jmix 3 Full Observability" dashboard contains the same metrics with exemplars, as well as slow traces, logs and profiles. Dashboards are imported into Grafana from JSON files.
-
Explore runs arbitrary queries to one data source: PromQL to Prometheus, LogQL to Loki, TraceQL to Tempo.
-
Drilldown lets you browse without writing queries. The Metrics, Logs, Traces and Profiles apps show the available metrics, logs, traces and profiles and let you narrow the selection by labels and attributes. This way is used when it is not yet known what to look for.
Profiling
Logs, metrics and traces show what happened and how long it took. A profile shows what the time of a span was spent on: in which methods CPU time was used and memory was allocated. Profiles are collected by the Pyroscope Java agent without changes to the application code, and the Pyroscope span processor links a profile to a particular request, including requests in which Jmix views and data loading run. See Profiling for details.
Metrics and Traces of Business Operations
In addition to the standard Jmix metrics and traces, you can add your own for business operations of the application, such as placing an order. The operation is wrapped in an observation using the Micrometer Observation API: one observation produces both a timer in metrics and a span in the trace, and framework calls inside the operation, such as data loading and saving, become its child spans. If a span is not needed, for example for a counter of placed orders, the metric is registered directly through MeterRegistry. See Metrics of Business Operations and Spans of Business Operations for details.
Observability in Production
Observability data can be collected continuously, including on production servers, if collection is configured as described below. Continuous collection lets you:
-
detect performance degradation: the accumulated data shows, for example, that a view started to open slower after a new version was released;
-
investigate incidents using the data collected before and during the failure, without reproducing the failure;
-
get notified about events, primarily abnormal ones: growing response time, errors, unavailable services;
-
notice problems before users run into them: a gradual growth of response time, database connection pool usage or memory consumption shows that a resource is approaching its limit, and the alert fires before the failure;
-
plan resources: the load history shows how the number of requests and users grows and when the current servers will no longer be enough.
Continuous collection has an overhead: the application spends CPU time and memory on recording data, and storage and log files grow in size. This overhead can be limited: all tools that Jmix uses let you collect detailed data only where it is needed and collect the rest in a reduced form or turn it off. The configuration for continuous operation follows these principles:
-
Detailed data is collected on demand. Summary data is collected continuously:
infolevel logs, metrics, a share of traces. Detailed output is enabled for a particular part of the application for the time of the investigation, without a restart. -
Only the needed data is collected. Observation groups, metrics and spans that nobody looks at are disabled, and histograms are enabled only where percentiles are needed.
-
Metric tags do not get unique values. The monitoring system stores a separate history of a metric for every tag value. If a username or order number is written to a tag, there will be as many histories as there are users or orders, and the amount of stored data grows quickly. Such values are written to traces and logs.
-
The retention period depends on the data type. Metrics are compact and are needed for comparison with past periods, so they are kept longer than logs, traces and profiles.
-
Access to the data is restricted. Spans and logs can contain query texts, parameter values and usernames, so the storage systems and the Actuator endpoints are closed to outsiders.
With such a configuration, the overhead can be small compared to the work of the application itself.
The settings for continuous operation are described in the article on each data type:
-
Metrics: observation groups, histograms, the number of series, the export interval, the retention period, access to Actuator endpoints.
-
Traces: the share of sampled traces, filtering in OpenTelemetry Collector, disabling unneeded observations, protecting query parameters, the retention period.
-
Logs: the permanent log level, SQL output, the retention period.
-
Profiles: memory allocation and lock profiles, the retention period.
Sections
-
Metrics: all Jmix metrics, UI metric modes, the Prometheus endpoint and OTLP export, ready-made Grafana dashboards.
-
Tracing: Jmix spans and their attributes, export through OpenTelemetry, the share of traces, viewing traces in Grafana.
-
Logging: log levels and the main logging categories, the username and trace IDs in MDC, centralized log collection.
-
Profiling: the Pyroscope Java agent, reading the profile of a Vaadin request, linking profiles to spans.
-
Performance Diagnostics: the order of investigating performance problems: slow views, extra database queries (N+1), waiting for connections, CPU load, memory and memory leaks.
Step-by-step guides with working examples:
Additional materials:
-
Monitoring Active Users in Enterprise Applications with Jmix: metrics of the number of active users (DAU, WAU, MAU), the jmix-app-usage-metrics-sample example application.