Add Timeline Markers Without Altering your Code

By Jérôme Vieilledent, on Oct 26, 2020

One of the underrated features of the Timeline View is the ability to add Markers. These cue-points can be very helpful as they are like beacons, helping you to find your way within the timeline.

In order to add a Marker, you can add the following instructions in your code:

PHP

\BlackfireProbe::addMarker('My Marker Label');

Python

from blackfire import probe
probe.add_marker('My Marker Label')

While this is quite efficient (and production-safe), you might not want to add Blackfire specific instructions within your code…

Add Markers from a Metric

If you prefer not to modify your code, but still want to use Markers, you may now define a metric using the new marker key:

metrics:
    greetings:
        marker: 'Greetings function call'
        matching_calls:
            php:
                -
                    callee: "=App\\Utils\\Greetings::phrase"

By defining this metric in the .blackfire.yaml file, each time App\Utils\Greetings::phrase() function is called, a marker will be added to the Timeline View, with “Greetings function call” as a label.

You can go even further when using argument capturing, by interpolating the captured argument(s) value into the label:

metrics:
    greetings:
        # Interpolates the value of the first argument in the marker label.
        marker: 'Greetings - ${1}'
        timeline: true
        matching_calls:
            php:
                -
                    callee:
                        selector: "=App\\Utils\\Greetings::phrase"
                        argument:
                            1: "/^(Hello|Hi)/"

Using the metric above, each time App\Utils\Greetings::phrase() function is called, a marker will be added with label will varying depending on the first argument value (e.g. “Greetings – Hello” / “Greetings – Hi”).

Another good reason to use metrics, in complement to all the reasons provided in “The Metrics” trilogy:

Happy Profiling!

Jérôme Vieilledent

As a Developer Advocate, Jérôme is all about spreading the love! His technical/development background enable him to talk as a peer to peers with our developer users. You’ll read his tips and advices on performance management with Blackfire. And he’ll support you as a day-to-day user.