# Callbacks

## Navigation
- [Blog](/blog)
  - [Introduction to Trifle Blog](/blog/2022-07-introduction)
  - [Running performance test for Trifle::Stats on your own](/blog/2022-08-running_performance_test_for_stats)
  - [Trifle as an Application Performance Monitoring](/blog/2022-11-performance)
    - [Trifle 01.png](/trifle-01.png)
    - [Trifle 02a.png](/trifle-02a.png)
    - [Trifle 02b.png](/trifle-02b.png)
    - [Trifle 03.png](/trifle-03.png)
    - [Trifle 04a.png](/trifle-04a.png)
    - [Trifle 04b.png](/trifle-04b.png)
    - [Trifle 05.png](/trifle-05.png)
    - [Trifle 06.png](/trifle-06.png)
    - [Trifle 07.png](/trifle-07.png)
  - [Trifle.Stats for Elixir](/blog/2023-06-15-trifle-stats-elixir)
  - [Series data manipulation in Trifle::Stats](/blog/2024-04-series-data-manipulation)
  - [Liveness management in Trifle::Stats](/blog/2025-07-03-beam-and-scan)
- [Robots.txt](/robots.txt)
- [Trifle::Docs](/trifle-docs)
  - [Case Studies](/trifle-docs/case_studies)
    - [Trifle](/trifle-docs/case_studies/trifle)
      - [Inception.png](/inception.png)
    - [Vaclavik](/trifle-docs/case_studies/vaclavik)
      - [Screenshot.png](/screenshot.png)
  - [Changelog](/trifle-docs/changelog)
  - [Configuration](/trifle-docs/configuration)
  - [Folder Structure](/trifle-docs/folder_structure)
    - [Walker](/trifle-docs/folder_structure/walker)
  - [Getting Started](/trifle-docs/getting_started)
    - [Rails with Engine](/trifle-docs/getting_started/rails_with_engine)
    - [Rails with Sinatra](/trifle-docs/getting_started/rails_with_sinatra)
    - [Raw Hardcode Mode](/trifle-docs/getting_started/raw_with_hardcore)
    - [Standalone Sinatra](/trifle-docs/getting_started/standalone_sinatra)
  - [How to Guides](/trifle-docs/guides)
    - [Render Menu](/trifle-docs/guides/menu)
    - [Next & Previous Documents](/trifle-docs/guides/neighbour)
    - [Nested Documents](/trifle-docs/guides/nested)
    - [Table of Content](/trifle-docs/guides/toc)
  - [Harvesters](/trifle-docs/harvesters)
    - [File](/trifle-docs/harvesters/file)
    - [Markdown](/trifle-docs/harvesters/markdown)
  - [Installation](/trifle-docs/installation)
  - [Templates](/trifle-docs/templates)
  - [Usage](/trifle-docs/usage)
- [Trifle::Logs](/trifle-logs)
  - [Case Studies](/trifle-logs/case_studies)
    - [DropBot](/trifle-logs/case_studies/dropbot)
      - [Dropbot Logs.png](/dropbot_logs.png)
  - [Changelog](/trifle-logs/changelog)
  - [Configuration](/trifle-logs/configuration)
  - [Drivers](/trifle-logs/drivers)
    - [File](/trifle-logs/drivers/file)
  - [Formatters](/trifle-logs/formatters)
    - [Json](/trifle-logs/formatters/json)
    - [Text](/trifle-logs/formatters/text)
    - [Timestamp](/trifle-logs/formatters/timestamp)
  - [Getting Started](/trifle-logs/getting_started)
  - [How to Guides](/trifle-logs/guides)
  - [Installation](/trifle-logs/installation)
  - [Result](/trifle-logs/result)
  - [Usage](/trifle-logs/usage)
- [Trifle.Stats](/trifle-stats-ex)
  - [Case Studies](/trifle-stats-ex/case_studies)
  - [Changelog](/trifle-stats-ex/changelog)
  - [Configuration](/trifle-stats-ex/configuration)
  - [Drivers](/trifle-stats-ex/drivers)
    - [Mongo](/trifle-stats-ex/drivers/mongo)
    - [Performance](/trifle-stats-ex/drivers/performance)
  - [Getting Started](/trifle-stats-ex/getting_started)
  - [How to Guides](/trifle-stats-ex/guides)
  - [Installation](/trifle-stats-ex/installation)
  - [Usage](/trifle-stats-ex/usage)
    - [Assert values](/trifle-stats-ex/usage/assert)
    - [Track values](/trifle-stats-ex/usage/track)
    - [Get values](/trifle-stats-ex/usage/values)
- [Trifle::Stats](/trifle-stats-rb)
  - [Aggregators](/trifle-stats-rb/aggregators)
    - [Avg](/trifle-stats-rb/aggregators/avg)
    - [Max](/trifle-stats-rb/aggregators/max)
    - [Min](/trifle-stats-rb/aggregators/min)
    - [Sum](/trifle-stats-rb/aggregators/sum)
  - [Case Studies](/trifle-stats-rb/case_studies)
    - [DropBot](/trifle-stats-rb/case_studies/dropbot)
      - [Daily Source.png](/daily_source.png)
      - [Daily Sqs.png](/daily_sqs.png)
      - [Hourly Calculations1.png](/hourly_calculations1.png)
      - [Hourly Calculations2.png](/hourly_calculations2.png)
  - [Changelog](/trifle-stats-rb/changelog)
  - [Configuration](/trifle-stats-rb/configuration)
  - [Designators](/trifle-stats-rb/designators)
    - [Custom](/trifle-stats-rb/designators/custom)
    - [Geometric](/trifle-stats-rb/designators/geometric)
    - [Linear](/trifle-stats-rb/designators/linear)
  - [Drivers](/trifle-stats-rb/drivers)
    - [Dummy](/trifle-stats-rb/drivers/dummy)
    - [Mongo](/trifle-stats-rb/drivers/mongo)
    - [Performance](/trifle-stats-rb/drivers/performance)
    - [Postgres](/trifle-stats-rb/drivers/postgres)
    - [Process](/trifle-stats-rb/drivers/process)
    - [Redis](/trifle-stats-rb/drivers/redis)
    - [Sqlite](/trifle-stats-rb/drivers/sqlite)
  - [Formatters](/trifle-stats-rb/formatters)
    - [Category](/trifle-stats-rb/formatters/category)
    - [Timeline](/trifle-stats-rb/formatters/timeline)
  - [Getting Started](/trifle-stats-rb/getting_started)
  - [How to Guides](/trifle-stats-rb/guides)
    - [Average](/trifle-stats-rb/guides/average)
    - [Distribution](/trifle-stats-rb/guides/distribution)
    - [Percentiles](/trifle-stats-rb/guides/percentiles)
  - [Installation](/trifle-stats-rb/installation)
  - [Transponders](/trifle-stats-rb/transponders)
    - [Average](/trifle-stats-rb/transponders/average)
    - [Standard Deviation](/trifle-stats-rb/transponders/standard_deviation)
  - [Usage](/trifle-stats-rb/usage)
    - [Assert values](/trifle-stats-rb/usage/assert)
    - [Assort values](/trifle-stats-rb/usage/assort)
    - [Beam values](/trifle-stats-rb/usage/beam)
    - [Scan values](/trifle-stats-rb/usage/scan)
    - [Get series](/trifle-stats-rb/usage/series)
    - [Track values](/trifle-stats-rb/usage/track)
    - [Get values](/trifle-stats-rb/usage/values)
- [Trifle::Traces](/trifle-traces)
  - [Callbacks](/trifle-traces/callbacks)
  - [Case Studies](/trifle-traces/case_studies)
    - [DropBot](/trifle-traces/case_studies/dropbot)
      - [Calculation.png](/calculation.png)
      - [List.png](/list.png)
  - [Changelog](/trifle-traces/changelog)
  - [Configuration](/trifle-traces/configuration)
  - [Getting Started](/trifle-traces/getting_started)
  - [How to Guides](/trifle-traces/guides)
  - [Installation](/trifle-traces/installation)
  - [Middleware](/trifle-traces/middleware)
    - [Rack](/trifle-traces/middleware/rack)
    - [Rails](/trifle-traces/middleware/rails)
    - [Sidekiq](/trifle-traces/middleware/sidekiq)
  - [Serializers](/trifle-traces/serializers)
  - [State](/trifle-traces/state)
  - [Tags and Artifacts](/trifle-traces/tags_and_artifacts)
  - [Usage](/trifle-traces/usage)

## Content
# Callbacks

`Trifle::Traces` does not persist any data. I considered using similar approach as drivers in `Trifle::Stats`, but I decided not to. While `Trifle::Stats` handles persisting and retrieving data in the most optimal way and acts as a single point of access, retrieving log entries and referencing them is a broad topic and can be better handled on user level.

And therefore several callbacks are used that allows user to handle persistance in best possible way.

## Liftoff

Whenever tracer is initialized, `liftoff` callback is executed. Return value from last `liftoff` callback is accessible as a `tracer.reference` in next callbacks.

## Bump

To provide updates during execution, `bump` callback is executed on new `trace` calls every `bump_every` seconds.

## Wrapup

Once you finish tracing, calling `Trifle::Traces.tracer.wrapup!` will trigger `wrapup` callback.

## Simple wrapup example

Sometimes you don't care about callbacks and all you want is to create a database record that will hold your tracer data.

```ruby
Trifle::Traces.configure do |config|
  config.on(:wrapup) do |tracer|
    next if tracer.ignore

    entry = Entry.create(
      key: tracer.key,
      data: tracer.data,
      state: tracer.state
    )
  end
end
```

## Complex example

Here is more complex example with utilizing all 3 callbacks.

```ruby
Trifle::Traces.configure do |config|
  config.bump_every = 5.seconds
  config.on(:liftoff) do |tracer|
    entry = Entry.create(
      key: tracer.key,
      data: tracer.data,
      state: tracer.state
    )
    entry.id
  end

  config.on(:bump) do |tracer|
    entry = Entry.find_by(id: tracer.reference)
    next if entry.nil?

    entry.update(
      data: tracer.data,
      state: tracer.state
    )
  end

  config.on(:wrapup) do |tracer|
    entry = Entry.find_by(id: tracer.reference)
    next if entry.nil?

    # Cleanup if ignoring
    if tracer.ignore
      entry.destroy
      next
    end

    entry.update(
      data: tracer.data,
      state: tracer.state
    )
  end
end
```
