Work with Lightrun Snapshots in the JetBrains IDE¶
Overview¶
A snapshot is a one-time "breakpoint" that doesn't block your code from running. As opposed to a traditional breakpoint, snapshots collect the stack trace and variables without interrupting the application at all.
From your JetBrains IDE, you can:
- Add a snapshot
- View snapshot data
- Edit and delete a snapshot
- Copy and paste a snapshot
- Debug Java lambda experessions in single-line code: Note that starting with version 1.48.1, Lightrun introduces lambda snapshots, enabling debugging of specific or multiple Java lambda expressions within a single line. This capability complements line-based snapshots, offering greater flexibility and precision for analyzing complex code structures.
Prerequisites
To use the plugin, make sure you have:
- Installed the plugin in your local IntelliJ instance.
- Authenticated IntelliJ with your Lightrun account.
- Attached at least one agent to your app.
- Opened the source code file for the running application from the IDE.
Add a snapshot¶
-
Go to the line of your source code at which you'd like to insert the snapshot.
-
Right-click to open the context menu:
-
Select Snapshot:
The Create Snapshot dialog should appear similar to the following image.
The following table describes the present fields.
Field Description Source From the available options in the dropdown list, bind the action to a specific agent, tag, or custom source.
Click Create Custom Source to create a new custom source.Filename & Line The source code file and line of code into which you're inserting the snapshot. The default path is to the source code file from which you're currently working. Expression Variables or method results to be displayed in the snapshot stack trace. Click + to enter additional expressions. Condition The condition of an if
statement, used to limit the execution of the action. For example, The conditionmyVar % 7 == 0
limits the action (log, snapshot, metric) output so that it only prints for variables that are divisible by 7.Max Hit Count the maximum number of times the snapshot should be taken during the lifetime of the action; default == 1. -
Click Advanced to configure the following additional fields:
Field Description Ignore quotas The maximum number of times the snapshot should be taken during the lifetime of the action; default == 1. Expiry The time after which the action ceases to track code behavior and is automatically disabled; default = 1 hour. -
Click Save to add the snapshot.
A snapshot marker
should appear next to the selected code line in your Jetbrains code editor.
Once a snapshot hit has been captured, you will be notified directly in your IDE.
View snapshot data¶
Once a snapshot hit has been captured, you can view the snapshot data directly in your IDE or in the Lightrun management portal in your browser. Click here for more information on how to view your snapshot data in the Lightrun management portal.
To view snapshot data in your IDE, click on the snapshot marker to open the Lightrun Snapshot window or click on Lightrun Snapshots in the bottom part of your IDE.
The Lightrun Snapshot tool window should appear similar to the following image.
Click on a snapshot to populate the Captured Hits, Frames, and Variables section with its data.
See JetBrains plugin quick tour to learn more about the Lightrun Snapshot tool window.
Edit and delete a snapshot¶
- Right-click on the snapshot marker
on the snapshot code line.
- Click Delete to delete the snapshot.
- Click Edit to edit the snapshot configuration.
Copy and paste a Lightrun snapshot¶
This procedure allows you to easily reuse existing snapshots in multiple locations within your code. Note that the snapshot is saved in the clipboard and can be pasted multiple times.
- Right-click on the snapshot marker
on the snapshot code line that you would like to copy.
-
Click Copy to copy the snapshot settings.
The Insert a Counter window opens with the copied metrics.
-
Update with a new counter name and click OK.
-
Go to the line of your target source code at which you'd like to insert the copied snapshot, right-click and click Paste Snapshot.
Debug Java lambda expressions in single-line code with Lightrun Snapshots¶
Note
Lightrun Lambda Snapshots are supported from version 1.48.1.
A single line of code may contain multiple points of interest for debugging, such as Lambda expressions in Java streams. Lightrun supports placing snapshots directly within Lambda expressions that are part of a single line of code (often referred to as "Java one-liners"). Typically, Lightrun snapshots are placed at the beginning of a line to capture the application's state before the line of code executes.
Capture the application's state during execution example¶
The following code example demonstrates the use of multiple Lambda expressions in a single line of code:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
List<String> filteredNames = names.stream().filter(name -> name.startsWith("A")).map(name -> name.toUpperCase()).collect(Collectors.toList());
- In the
.filter
lambda: Capture the value ofname
to verify which names are being evaluated and filtered out. - In the
.map
lambda: Capture the transformed value ofname
to ensure the uppercase conversion is functioning as expected.
When adding a snapshot, Lightrun automatically detects lambda expressions on the selected line and offers options to place the snapshot either on the entire line or directly next to the detected lambda expressions. The dialog title confirms that a lambda snapshot has been selected. All operations—including adding, viewing, editing, and deleting—work the same for lambda snapshots as they do for line-based snapshots.
To add a lambda Snapshot¶
When working with lambda snapshots, the dialog titles indicate you are working on a Lambda snapshot. All operations—including adding, viewing, editing, and deleting—work the same for lambda snapshots as they do for line-based snapshots. For more information, refer to the relevant sections above.
-
Navigate to the line of your source code containing the lambda expression where you'd like to insert the snapshot, and position the snapshot next to it.
-
Right-click to open the context menu.
-
Select Snapshot (Virtual Brakepoint).
The line and the lambdas detected within it are listed.
-
From the list, select a lambda expression.
The Insert a Lambda Snapshot dialog opens.
-
In the Insert a Lambda Snapshot dialog, complete the fields (identical to line snapshots.
-
Click Save.
The lambda snapshot is added next to the selected code line and is added to the Actions tab, alongside line snapshots.
Lambda Snapshots in action¶
The following tutorial demonstrates how to add a lambda snapshot. For a line containing the lambda expression (e) -> (e.Ename.length() > 4 && e.City.length() > 4)
, the lambda expression is automatically detected.