Add (Datadog APM) initializer in custom plugin?

Hi all! First time poster, sorry if I got the category wrong.

I’d like to configure discourse for Datadog APM instrumentation and figured it would make sense to use a custom plugin for this purpose (following the idea of discourse-prometheus).

The usual way of configuring a Rails app for APM is to use an initializer - I’m not (yet) deeply familiar with the plugin system in discourse. Is there any way to load such an initializer early in the lifetime of the discourse app using the existing plugin system? If not, what other options exist? I’d prefer not to have to fork the entire app for the purpose of adding a single initializer :sweat_smile:

2 Likes

Follow the same pattern in the prometheus plugin:

after_initialize is very likely where you want to put this code.

5 Likes

Thanks for the swift response @sam! I’ll give that a try and let you know how it goes.

1 Like

@sam I tried that but apparently this particular ddtrace gem expects to be able to run in before_initialize, too. But it doesn’t seem like before_initialize is a valid method in plugin.rb. Are there any other options for a plugin to run earlier in the lifecycle of the app?

Error:

[2020-06-30T15:30:40.348519 #2313] ERROR -- ddtrace: [ddtrace] (/workspace/plugins/discourse-datadog/gems/2.6.2/gems/ddtrace-0.37.0/lib/ddtrace/contrib/patcher.rb:43:in `on_patch_error') Failed to apply Datadog::Contrib::Rails::Patcher patch. Cause: can't modify frozen Array Location: /home/vscode/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actionpack-6.0.3.1/lib/action_dispatch/middleware/stack.rb:102:in `insert'

My next step would be to not use the automagic instrumentation that is applied with that gem’s “patchers” but instead fold some of the actual instrumentation logic into the plugin itself, similar to how you’ve done it with discourse-prometheus. I guess that would make sense anyway to plug into some of the specifics of discourse. Was just hoping there’s a way to leverage the magic that I haven’t seen yet.