Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve nested Sinatra app instrumentation #1097

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

tombruijn
Copy link
Member

@tombruijn tombruijn commented Jun 18, 2024

Sinatra apps, mounted in Rails app, would run into the issue that the Rails middleware has already created a transaction for the request. It could "force" a new transaction to be made, which loses information from everything that happened before it.

Previously, before PR #1089, it would also leave a transaction that was not closed properly. Even with that change, for one request, now two transactions are created, one for Rails and one for the nested Sinatra app.

This change reads if there's a current transaction from the request env, and uses that instead of creating a new one. Some logic in the Transaction class would read from the request object given to it on Transaction.create to set metadata like parameters, so these need to be set manually now.

It will also make sure not to close the transaction if one existed already before this middleware was called.

Part of #329, the Rack middleware refactor.

Sinatra apps, mounted in Rails app, would run into the issue that the
Rails middleware has already created a transaction for the request. It
would "force" a new transaction to be made, which loses information from
everything that happened before it.

Previously, before PR #1089, it would also leave a transaction that was
not closed properly. Even with that change, for one request, now two
transactions are created, one for Rails and one for the nested Sinatra
app.

This change reads if there's a current transaction from the request env,
and uses that instead of creating a new one. Some logic in the
Transaction class would read from the request object given to it on
`Transaction.create` to set metadata like parameters, so these need to
be set manually now.

It will also make sure not to close the transaction if one existed
already before this middleware was called.

Part of #329, the Rack middleware refactor.
@tombruijn
Copy link
Member Author

Event timeline before this change, before #1089:
image

Event timeline before this change, after #1089:
image

Event timeline after this change:
image

@tombruijn
Copy link
Member Author

Build fixed in #1098

Copy link
Member

@luismiramirez luismiramirez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏼

@tombruijn tombruijn merged commit c76952f into main Jun 19, 2024
15 of 16 checks passed
tombruijn added a commit that referenced this pull request Jun 19, 2024
When more of our Rack, Rails and Sinatra middlewares are nested in one
app, improve reporting for those requests. This follows PRs #1089 for
Rails and #1097 for Sinatra. This change improves this reporting for
Rack apps using the GenericInstrumentation middleware. Other than
supporting this scenario better, no one should notice this change.

## Refactor details

I've refactored the SinatraInstrumentation middleware to inherit from a
new AbstractMiddleware which includes much of the behavior from the
GenericInstrumentation and previous SinatraInstrumentation
implementations.
All the logic for nested instrumentation middlewares are now handled in
this AbstractMiddleware.

Subclasses of AbstractMiddleware can set their library's specific
metadata using the `add_transaction_metadata_before` and
`add_transaction_metadata_after` methods, along with specifying the
`request_class`, `params_method` and `instrument_span_name` settings.

This already works with the EventHandler, as both set the transaction on
the request env to detect nested instrumentation. An app could add both
the EventHandler and a subclass of the AbstractMiddleware, and it would
report the request transaction properly.

## GenericInstrumentation

I've kept the GenericInstrumentation middleware. The only thing it
really does different that we don't want to put in the
AbstractMiddleware is the fallback to the "unknown" action name. I
didn't want to break existing behavior, so that is all it still does.

If we move the GenericInstrumentation action name fallback to the
AbstractMiddleware, we may be reporting more requests than we want for
other middlewares that inherit from it. For example, for Rails app, I
also want to use this AbstractMiddleware, and it relies on asset
requests having no action name so the extension can drop them. That way
we don't report transactions for asset requests.

## Next steps

If this change is approved, I will update the other Rack
instrumentations, like Rails, Grape and Padrino.
tombruijn added a commit that referenced this pull request Jun 19, 2024
When more of our Rack, Rails and Sinatra middlewares are nested in one
app, improve reporting for those requests. This follows PRs #1089 for
Rails and #1097 for Sinatra. This change improves this reporting for
Rack apps using the GenericInstrumentation middleware. Other than
supporting this scenario better, no one should notice this change.

## Refactor details

I've refactored the SinatraInstrumentation middleware to inherit from a
new AbstractMiddleware which includes much of the behavior from the
GenericInstrumentation and previous SinatraInstrumentation
implementations.
All the logic for nested instrumentation middlewares are now handled in
this AbstractMiddleware.

Subclasses of AbstractMiddleware can set their library's specific
metadata using the `add_transaction_metadata_before` and
`add_transaction_metadata_after` methods, along with specifying the
`request_class`, `params_method` and `instrument_span_name` settings.

This already works with the EventHandler, as both set the transaction on
the request env to detect nested instrumentation. An app could add both
the EventHandler and a subclass of the AbstractMiddleware, and it would
report the request transaction properly.

## GenericInstrumentation

I've kept the GenericInstrumentation middleware. The only thing it
really does different that we don't want to put in the
AbstractMiddleware is the fallback to the "unknown" action name. I
didn't want to break existing behavior, so that is all it still does.

If we move the GenericInstrumentation action name fallback to the
AbstractMiddleware, we may be reporting more requests than we want for
other middlewares that inherit from it. For example, for Rails app, I
also want to use this AbstractMiddleware, and it relies on asset
requests having no action name so the extension can drop them. That way
we don't report transactions for asset requests.

## Next steps

If this change is approved, I will update the other Rack
instrumentations, like Rails, Grape and Padrino.
tombruijn added a commit that referenced this pull request Jun 21, 2024
When more of our Rack, Rails and Sinatra middlewares are nested in one
app, improve reporting for those requests. This follows PRs #1089 for
Rails and #1097 for Sinatra. This change improves this reporting for
Rack apps using the GenericInstrumentation middleware. Other than
supporting this scenario better, no one should notice this change.

## Refactor details

I've refactored the SinatraInstrumentation middleware to inherit from a
new AbstractMiddleware which includes much of the behavior from the
GenericInstrumentation and previous SinatraInstrumentation
implementations.
All the logic for nested instrumentation middlewares are now handled in
this AbstractMiddleware.

Subclasses of AbstractMiddleware can set their library's specific
metadata using the `add_transaction_metadata_before` and
`add_transaction_metadata_after` methods, along with specifying the
`request_class`, `params_method` and `instrument_span_name` settings.

This already works with the EventHandler, as both set the transaction on
the request env to detect nested instrumentation. An app could add both
the EventHandler and a subclass of the AbstractMiddleware, and it would
report the request transaction properly.

## GenericInstrumentation

I've kept the GenericInstrumentation middleware. The only thing it
really does different that we don't want to put in the
AbstractMiddleware is the fallback to the "unknown" action name. I
didn't want to break existing behavior, so that is all it still does.

If we move the GenericInstrumentation action name fallback to the
AbstractMiddleware, we may be reporting more requests than we want for
other middlewares that inherit from it. For example, for Rails app, I
also want to use this AbstractMiddleware, and it relies on asset
requests having no action name so the extension can drop them. That way
we don't report transactions for asset requests.

## Next steps

If this change is approved, I will update the other Rack
instrumentations, like Rails, Grape and Padrino.
@tombruijn tombruijn deleted the sinatra-parent-instrumentation branch July 10, 2024 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants