# Add (Datadog APM) initializer in custom plugin?

**URL:** https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804
**Category:** Development
**Created:** [June 24, 2020, 12:52pm UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804 "2020-06-24T12:52:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![raph](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/raph/32/184924_2.png) [@raph](https://meta.discourse.org/u/raph)
#### Post date: [June 24, 2020, 12:52pm UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804/1 "2020-06-24T12:52:17Z")

</div>

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

I’d like to configure discourse for [Datadog APM instrumentation](https://docs.datadoghq.com/tracing/setup/ruby/#quickstart-for-rails-applications) and figured it would make sense to use a custom plugin for this purpose (following the idea of [discourse-prometheus](https://github.com/discourse/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 😅

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 25, 2020, 6:40am UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804/2 "2020-06-25T06:40:27Z")

</div>

Follow the same pattern in the prometheus plugin:

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

---

<div class="post-metadata">

### Author: ![raph](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/raph/32/184924_2.png) [@raph](https://meta.discourse.org/u/raph)
#### Post date: [June 25, 2020, 10:34am UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804/3 "2020-06-25T10:34:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![raph](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/raph/32/184924_2.png) [@raph](https://meta.discourse.org/u/raph)
#### Post date: [June 30, 2020, 3:30pm UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804/4 "2020-06-30T15:30:02Z")

</div>

@sam I tried that but apparently this particular `ddtrace` gem [expects](https://github.com/DataDog/dd-trace-rb/blob/f5733c8f9f48447a176833488db7428c38392585/lib/ddtrace/contrib/rails/patcher.rb#L30-L37) 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:

```plaintext
[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'

```

---

<div class="post-metadata">

### Author: ![raph](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/raph/32/184924_2.png) [@raph](https://meta.discourse.org/u/raph)
#### Post date: [June 30, 2020, 3:44pm UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804/5 "2020-06-30T15:44:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jeffbyrnes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeffbyrnes/32/121097_2.png) [@jeffbyrnes](https://meta.discourse.org/u/jeffbyrnes)
#### Post date: [November 2, 2021, 4:20pm UTC](https://meta.discourse.org/t/add-datadog-apm-initializer-in-custom-plugin/155804/6 "2021-11-02T16:20:43Z")

</div>

@raph did you ever get this plugin working, even just with the automatic instrumentation?
