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

fix: use AS::N subscriber for serialize events #1075

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

robbkidd
Copy link
Member

Resolves #992

This mimicks a fair amount of the current ActionMailer and ActionView instrumentation.

The details for Context management (i.e. setting current span) are already handled by the OTel ActiveSupport instrumentation. Reuse the notifications subscriber here for ActiveModel serialization events.

Reworked the example app into two: one Rails which works with the usual SDK configuration and one standalone (no Rails) to demonstrate that the subscription needs to be made after the SDK configuration is complete. If the subscription is created during instrumentation install, the subscription's tracer will be a NO-OP API tracer and won't produce spans.

TODO:

  • review/update README and doc comments for accurate usage steps

The details for Context management (i.e. setting current span) are
already handled by the OTel ActiveSupport instrumentation. Reuse
the notifications subscriber here for ActiveModel serialization events.

Reworked the example app into two: one Rails which works with the usual
SDK configuration and one standalone (no Rails) to demonstrate that the
subscription needs to be made after the SDK configuration is complete.
If the subscription is created during instrumentation install, the
subscription's tracer will be a NO-OP API tracer and won't produce
spans.
@robbkidd robbkidd requested review from a team July 19, 2024 15:44
@robbkidd robbkidd added ruby Pull requests that update Ruby code fix labels Jul 19, 2024
@robbkidd robbkidd self-assigned this Jul 19, 2024
@@ -38,7 +39,7 @@
_(exporter.finished_spans.size).must_equal 1

_(span).must_be_kind_of OpenTelemetry::SDK::Trace::SpanData
_(span.name).must_equal 'ModelSerializer render'
_(span.name).must_equal 'render.active_model_serializers'
Copy link
Member Author

Choose a reason for hiding this comment

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

One disadvantage of using the current OTel AS::N SpanSubscriber is that the span_name_formatter callable only receives the notification event name as a parameter. It has no access to the event payload to generate a span name like previously.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you want to change that about the OTel ASN gem?

What about writing your own span subscriber?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would very much like to change that about the subscriber in the AS::N instrumentation.

We chatted in CNCF Slack briefly around the idea of the OTel AS::N subscriber taking a block that receives a mutable span and the AS::N event payload. The instrumentation developer can perform whatever name and attribute hijinks they desire. (I propose we add to the documentation of this potential future interface a warning that the behavior, performance, and exception handling of that block is entirely the responsibility of the block's author.)

I can picture (vaguely) that any of the instrumentation we write that leverages this block-accepting AS::N subscriber could themselves accept a block to allow end-user customization.


# without Rails and the Railtie automation, must manually trigger
# instrumentation subscription after SDK is configured
OpenTelemetry::Instrumentation::ActiveModelSerializers.subscribe
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't love this extra needed step outside the configure block to get the instrumentation to Actually Instrument when Rails wasn't around to detect and call Railtie hooks. I couldn't get the SpanScriber to have a real, configured Tracer when instantiating the subscriber within the instrumentation install step. I see that ActionMailer and ActionView are relying entirely on a Railtie to subscribe at the end of Rails initialization. I figured I would add this interface for folks who may be using ActiveModelSerializers outside of a full Rails app.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand what you mean.

Want to do some sort of hacky thing that checks if Rails is defined and use the Railtie, otherwise manually install?

gem 'opentelemetry-common'
gem 'opentelemetry-instrumentation-active_model_serializers', path: '../'
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
Copy link
Contributor

Choose a reason for hiding this comment

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

We often output example data to the console. What do you think about doing that here? Or maybe outputting to both OTLP and the console?

end

# TraceRequestApp is a minimal Rails application inspired by the Rails
# bug report template for action controller.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# bug report template for action controller.
# bug report template for Action Controller.

end

# Rails app initialization will pick up the instrumentation Railtie
# and subscribe to ActiveSupport notifications
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# and subscribe to ActiveSupport notifications
# and subscribe to Active Support notifications

Copy link
Contributor

github-actions bot commented Sep 8, 2024

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions bot added the stale Marks an issue/PR stale label Sep 8, 2024
@arielvalentin
Copy link
Collaborator

@robbkidd mind taking a look at @kaylareopelle comments?

@github-actions github-actions bot removed the stale Marks an issue/PR stale label Sep 9, 2024
Copy link
Contributor

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions bot added the stale Marks an issue/PR stale label Oct 19, 2024
@arielvalentin arielvalentin added keep Ensures stale-bot keeps this issue/PR open and removed fix stale Marks an issue/PR stale labels Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep Ensures stale-bot keeps this issue/PR open ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ActiveModelSerializer instrumentation does not make the span current
3 participants