ELK Stack — Lens
Lens
Lens is Kibana’s modern drag-and-drop visualization editor. It provides the fastest and most intuitive way to create visualizations without needing to understand the underlying aggregation mechanics. Lens automatically suggests chart types based on your data and lets you switch between visualization types instantly.
Why Lens?
Lens was introduced as a replacement for the legacy aggregation-based visualization editor. Here’s why it’s the recommended approach:
| Feature | Lens | Legacy Visualizations |
|---|---|---|
| Interface | Drag-and-drop | Aggregation configuration forms |
| Chart type switching | Instant — click to switch | Must recreate from scratch |
| Suggestions | Yes — recommends chart types | No |
| Formulas | Yes — write math expressions | Limited (TSVB only) |
| Learning curve | Low | Medium to high |
| Customization depth | Good for most use cases | Deeper for specialized needs |
| Runtime fields | Supported inline | Not supported |
When to use Lens: For the majority of visualizations — line charts, bar charts, pie charts, metrics, tables, heatmaps.
When to use legacy or Vega: For specialized visualizations (TSVB with annotations, Vega for custom chart types) or when you need features not yet available in Lens.
Opening Lens
From the Visualize Library
- Click the hamburger menu (☰) → Analytics → Visualize Library.
- Click Create visualization.
- Select Lens (it is usually the default option).
From a Dashboard
- Open a dashboard in edit mode.
- Click Add panel → Create visualization.
- The Lens editor opens directly within the dashboard context.
The Lens Interface
The Lens editor has four main areas:
1. Field List (Left Sidebar)
- Shows all fields from the selected data view.
- Fields are grouped by type — timestamps, numbers, keywords, text, geo, etc.
- Each field shows a small preview icon indicating its type.
- You can search for fields using the search box at the top.
2. Drag-and-Drop Zones (Center)
- The chart configuration area where you drag fields to define axes and metrics.
- Zones vary by chart type:
- Bar/Line/Area: Horizontal axis, Vertical axis, Break down by.
- Pie/Donut: Slice by, Size by.
- Metric: Primary metric, Secondary metric.
- Table: Columns.
3. Chart Preview (Center)
- The live preview of your visualization updates as you configure it.
- Changes are reflected immediately — no need to click “Update”.
4. Suggestions Panel (Right Sidebar)
- Lens suggests alternative chart types based on your current configuration.
- Click any suggestion to instantly switch to that chart type.
- Suggestions help you discover the best way to represent your data.
Creating Visualizations with Lens
Line Chart — Error Rate Over Time
- Open Lens (from Visualize Library or a dashboard).
- Select the data view —
app-logs-*. - From the field list, drag
@timestampto the Horizontal axis zone.- Lens automatically applies a Date Histogram aggregation.
- The Vertical axis defaults to Count of documents.
- To filter to errors only, type in the query bar:
log.level: "ERROR". - To break down by service, drag
service.nameto the Break down by zone.- Lens applies a Terms aggregation automatically.
- The chart now shows error count over time, broken down by service.
- Click Save to save the visualization.
Bar Chart — Top 10 Error-Producing Services
- Open Lens and select your data view.
- Filter:
log.level: "ERROR"in the query bar. - Drag
service.nameto the Horizontal axis. - The Vertical axis defaults to Count.
- Lens renders a vertical bar chart with services on the x-axis and error counts on the y-axis.
- Click the chart type selector in the toolbar and switch to Horizontal Bar if you prefer horizontal bars.
Pie Chart — Traffic by Status Code Group
- Open Lens and select your data view.
- Drag
http.response.status_codeto the Slice by zone. - Lens creates a pie chart showing the distribution of status codes.
- To group by status code ranges (2xx, 3xx, 4xx, 5xx), click the field in the Slice by zone and change the aggregation to Ranges:
- 200–299 → “2xx Success”
- 300–399 → “3xx Redirect”
- 400–499 → “4xx Client Error”
- 500–599 → “5xx Server Error”
Metric — Average Response Time
- Open Lens.
- Click the chart type selector and choose Metric.
- Drag
event.durationto the Primary metric zone. - Click the field configuration and change the aggregation from Median to Average.
- The metric panel shows a single large number — the average response time.
- Optionally, add
@timestampas a Secondary metric to show the time range.
Data Table — Top Error Messages with Count
- Open Lens.
- Click the chart type selector and choose Table.
- Drag
error.messageto the Columns zone — Lens adds it as a Terms aggregation. - Drag
Count(from Metrics) as another column. - Filter:
log.level: "ERROR". - The table shows error messages ranked by occurrence count.
Heatmap — Requests by Hour and Day of Week
- Open Lens.
- Click the chart type selector and choose Heatmap.
- Drag
@timestampto the Horizontal axis. Configure the interval to 1 hour. - For the vertical axis, you’ll need a runtime field or a pre-computed
day_of_weekfield. - The Vertical axis shows the grouping, and cell color intensity represents request volume.
Formulas in Lens
Lens supports formulas — mathematical expressions that combine aggregations. This is one of Lens’s most powerful features.
Accessing Formulas
- Click on a metric in the Vertical axis zone.
- In the configuration panel, switch from a simple aggregation to Formula.
- Enter your expression.
Common Formula Examples
Error Rate Percentage:
count(kql='log.level: "ERROR"') / count() * 100
Average Response Time in Seconds (from milliseconds):
average(event.duration) / 1000
Requests Per Second:
count() / 60
(Assuming 1-minute Date Histogram buckets.)
95th Percentile Response Time:
percentile(event.duration, percentile=95)
Ratio of 5xx to Total Requests:
count(kql='http.response.status_code >= 500') / count()
Formula Functions
Lens formulas support these aggregation functions:
| Function | Description |
|---|---|
count() |
Document count |
sum(field) |
Sum of a numeric field |
average(field) |
Average value |
min(field) |
Minimum value |
max(field) |
Maximum value |
median(field) |
Median value |
percentile(field, percentile=N) |
Nth percentile |
unique_count(field) |
Cardinality (distinct values) |
last_value(field) |
Most recent value |
counter_rate(field) |
Rate of change for counters |
differences(metric) |
Difference between consecutive buckets |
moving_average(metric, window=N) |
Moving average |
cumulative_sum(metric) |
Running total |
You can combine these with standard math operators: +, -, *, /, and parentheses.
Runtime Fields in Lens
Runtime fields let you create computed fields on the fly without re-indexing your data.
Creating a Runtime Field in Lens
- In the Lens field list, click Create field at the top.
- Enter a name — for example,
response_time_seconds. - Choose the type — double.
- Enter the Painless script:
emit(doc['event.duration'].value / 1000.0)
- Click Save.
- The new field appears in the field list and can be used like any other field.
Use Cases for Runtime Fields
- Converting units (milliseconds to seconds, bytes to megabytes).
- Extracting parts of a field (domain from a URL, hour from a timestamp).
- Categorizing data (status code groups from raw codes).
Lens Chart Type Selector
You can switch between chart types instantly in Lens. Click the chart type icon in the toolbar to see all available options:
- Bar (vertical and horizontal)
- Stacked bar
- Line
- Area
- Stacked area
- Pie / Donut
- Treemap
- Metric
- Table
- Heatmap
- Gauge
- Mosaic
When you switch types, Lens preserves your field assignments as much as possible. For example, switching from a bar chart to a line chart keeps the same axes and data.
Best Practices
Use Descriptive Titles
When saving Lens visualizations, use names that describe:
- What is being measured — “Error Count”, “Avg Response Time”.
- How it’s grouped — “by Service”, “Over Time”.
- Chart type — “Line”, “Bar”, “Table”.
Example: [Logs] Error Count by Service Over Time — Line
Start with Suggestions
Drag a field to the workspace and look at Lens suggestions. They often reveal insights you wouldn’t have thought to visualize.
Use Formulas for Derived Metrics
Rather than building separate visualizations for numerator and denominator, use formulas to compute rates and percentages directly.
Limit Terms Aggregations
Keep the “Top values” count reasonable (5–20). Too many terms create cluttered, unreadable charts.
Leverage Runtime Fields
Instead of re-indexing data to add computed fields, use runtime fields in Lens for quick transformations.
Quick Reference
| Task | How To |
|---|---|
| Open Lens | Visualize Library → Create visualization → Lens |
| Set data view | Click the data view dropdown at the top |
| Add a field to chart | Drag from field list to a drop zone |
| Change chart type | Click chart type selector in the toolbar |
| Add a filter | Type a KQL query in the query bar |
| Use a formula | Click a metric zone → switch to Formula |
| Create runtime field | Field list → Create field |
| Break down by | Drag a keyword field to “Break down by” zone |
| Switch axes | Drag fields between horizontal and vertical zones |
| Save visualization | Click Save → enter name → Save |
Next: Practice Exercises — Hands-on exercises to reinforce your Kibana skills.