# APM for external plugin server side

**URL:** https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759
**Category:** APM
**Tags:** nodejs
**Created:** [July 25, 2024, 6:21am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759 "2024-07-25T06:21:40Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [July 25, 2024, 6:21am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/1 "2024-07-25T06:21:40Z")

</div>

Hello,

I am creating an **external kibana plugin in React** , which has both public and server side, using ES and Kibana **8.13.0** (also testing in **8.14.3** )

I would like to have APM for the same. I have successfully added APM for the public side. But, in the **server side, it throws error that the start() function of elastic apm cannot be called more than once**. On further investigation, I found the reason for the same is an active APM for Kibana.

When I print **`elasticApm.isStarted()`, it returns true**....... and **`elasticApm.getServiceName()` returns kibana**.

How can I achieve APM for external plugin server side.

Thanks

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [July 25, 2024, 2:55pm UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/2 "2024-07-25T14:55:02Z")

</div>

Hi @Sheereen,

Can you confirm which agents you're using on the UI and server side? Are you using [the Elastic APM Node.js agent](https://www.elastic.co/guide/en/apm/agent/nodejs/current/index.html) or [Elastic RUM agent](https://www.elastic.co/guide/en/apm/agent/rum-js/current/intro.html) and where are you using each?

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [July 29, 2024, 5:58am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/3 "2024-07-29T05:58:04Z")

</div>

Hi,

Thanks for the reply

In the **plugin UI side, I am using RUM agent** , and initializing the same in the _**setUp()**_ function of _ **plugin.ts** _, using the _**init()**_ function of _ **'@elastic/apm-rum'** _.

In the **server side, I am using the Node Js agent,** and initializing the same in _**setUp()**_ function of _ **plugin.ts** _, using the _**start()**_ function of _ **'elastic-apm-node'** _. When I start, I get an error in Kibana console that start() can be called only once. So I printed out isStarted() and also the agent object, and found APM for kibana is already running (_ **serviceName: kibana** _).

Thanks

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [July 29, 2024, 8:49am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/4 "2024-07-29T08:49:45Z")

</div>

Thanks for confirming. Can you share your setup configuration for both please?

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [July 29, 2024, 9:20am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/5 "2024-07-29T09:20:21Z")

</div>

**Plugin UI**

In public/plugin.ts

```auto
 public setup(core: CoreSetup): DcmMinimalPluginSetup {
    // Register an application into the side navigation menu
    core.application.register({
        ...
    });

    // Add this to the very top of the first file loaded in your app
    const apm = initElasticApm({
          serviceName: 'plugin-frontend',
          serverUrl: 'https://<ip>:8200',
          environment: 'dev',
          serviceVersion: '1.0',
    });
    apm.addLabels({ group: 'plugin' });

    return {};
  }

```

  

* * *
  

**Plugin Server**

In server/plugin.ts

```auto
constructor(initializerContext: PluginInitializerContext) {
    this.logger = initializerContext.logger.get();

    console.log(ElasticAPM.isStarted()); // Prints true, so the following LOC is never executed

    if (!ElasticAPM.isStarted()) {
        // Add this to the very top of the first file loaded in your app
        const apm: ElasticAPM.Agent = ElasticAPM.start({
            serviceName: 'plugin-backend',
            serverUrl: 'https://<ip>:8200',
            environment: 'dev',
            verifyServerCert: false,
            serviceVersion: '1.0',
            captureBody: 'all',
            captureErrorLogStackTraces: 'always',
            captureExceptions: true,
            captureHeaders: true,
            captureSpanStackTraces: true,
            globalLabels: { group: 'plugin' },
    });
}

```

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 2, 2024, 6:39am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/6 "2024-08-02T06:39:44Z")

</div>

Hi @carly.richmond

Any input on this?

Thanks

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [August 2, 2024, 10:43am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/7 "2024-08-02T10:43:11Z")

</div>

Hi Sheereen,

Thanks for your patience. I'm assuming you're seeing this issue as part of your local development?

I've been having a dig around, and I _think_ the issue you're seeing is that the [APM agent is already initialized as it's shipped with Kibana for debugging purposes](https://www.elastic.co/guide/en/kibana/current/kibana-debugging.html#kibana-debugging). So by initializing the agent in your plugin you're attempting to start it again.

Have you tried configuring the agent with your additional options in the `config/kibana.dev.yml` and see if that does what you need to do?

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 2, 2024, 10:56am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/8 "2024-08-02T10:56:50Z")

</div>

Hi Carly,

Thanks for the response

Yes, I am running kibaa in dev mode and using kibana.dev.yml as the config file.

I haven't tried adding the agent initialization to the kibana.dev.yml file. But is it possible. It is a yml file write , and I donot think such function calls are possible there. Do you have any example or reference for the same?

Thanks,

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 2, 2024, 11:01am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/9 "2024-08-02T11:01:34Z")

</div>

Oh yes... I think this m8 be the answer

[elastic.apm.active: false](https://www.elastic.co/guide/en/kibana/current/kibana-debugging.html#kibana-debugging:~:text=All%20config%20options%20can%20be%20set%20by%20creating%20an%20appropriate%20config%20file%20under%20config/kibana.dev.yml.)

I will try and let you know

Thanks

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 2, 2024, 12:56pm UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/10 "2024-08-02T12:56:36Z")

</div>

Hi @carly.richmond ,

I gave the following in kibana.dev.yml

```auto
elastic:
  apm:
    active: false
    contextPropagationOnly: false

```

I donot get any erros, but when I start the APM, it still picks up the kibana apm configuration and starts the same.

```auto
// Add this to the very top of the first file loaded in your app
      const apm: ElasticAPM.Agent = ElasticAPM.start({
        ...ELASTIC_APM_OPTIONS.BE,
        globalLabels: {
          ...ELASTIC_APM_OPTIONS.COMMON.globalLabels,
        },
      });

```

I have clearly specified my own configuration, but when I print **apm** , it has the Kibana apm ip and settings. It is actually taking a combo of kibana settings and my custom settings. So kibana apm settings overrides my custom settings ({...custom\_settings, ...kibana\_settings}).

[Let me try mentioning the apm url and other settings in kibana.dev.yml itself and check](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html). It m8 work. Though, is it a good approach?

[I also tried mentioning the apm config file](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#config-file), that also did not work.

Thanks

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 7, 2024, 1:40pm UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/11 "2024-08-07T13:40:41Z")

</div>

Hi @carly.richmond ,

Any updates?

Thanks

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [August 7, 2024, 1:51pm UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/12 "2024-08-07T13:51:46Z")

</div>

Hi @Sheereen,

Did you try adding the APM url settings in `kibana.dev.yml` to see if that works?

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 9, 2024, 11:06am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/13 "2024-08-09T11:06:06Z")

</div>

Yes I tried... din't work

```auto
elastic:
  apm:
    active: true
    configFile: <confilg_file_path>/plugin-elastic-apm-node.js
    serviceName: plugin_server
    serverUrl: https://<ip>:8200
    secretToken: <secret_token>
    verifyServerCert: true
    serverCaCertFile: <cert_file_path>/ca.crt
    serviceVersion: 1.0
    environment: dev
    centralConfig: false
    breakdownMetrics: true
    transactionSampleRate: 0.1
    captureBody: all
    captureErrorLogStackTraces: always
    captureExceptions: true
    captureHeaders: true
    captureSpanStackTraces: true

```

I also tried with confile file, as u can see in the above  
Below are the contents of the config file, _plugin-elastic-apm-node.js_

```auto
module.exports = {
    serviceName: 'plugin_server',
    serverUrl: 'https://<ip>:8200',
    secretToken: <secret_token>,
    verifyServerCert: true,
    serverCaCertFile: '<cert_file_path>/ca.crt',
    serviceVersion: 1.0,
    environment: 'dev',
    captureBody: 'all',
    captureErrorLogStackTraces: 'always',
    captureExceptions: true,
    captureHeaders: true,
    captureSpanStackTraces: true
  }

```

None of these work. The apm config in kibana replaces the custom config I provide, so the serviceName and serverUrl gets replaced.

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [August 12, 2024, 1:17pm UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/14 "2024-08-12T13:17:20Z")

</div>

Thanks for sharing. Is this instrumentation for production or local development purposes? If it's only necessary for local debugging adding the settings temporarily to the Kibana APM config could be an option.

---

<div class="post-metadata">

### Author: ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)
#### Post date: [August 14, 2024, 5:21am UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/15 "2024-08-14T05:21:28Z")

</div>

Right now. I am testing on Kibana dev mode.

But in near future, I would like to use it in production.

How can I add to Kibana APM config? Is it via [kibana.dev.yml](https://www.elastic.co/guide/en/kibana/current/kibana-debugging.html#kibana-debugging:~:text=All%20config%20options%20can%20be%20set%20by%20creating%20an%20appropriate%20config%20file%20under%20config/kibana.dev.yml.)? I yes, then I have tried that and doesn't work.

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [September 2, 2024, 3:15pm UTC](https://discuss.elastic.co/t/apm-for-external-plugin-server-side/363759/16 "2024-09-02T15:15:24Z")

</div>

Thanks for the update @Sheereen. I've been out on vacation so just getting back to this. Looking at the [testing steps in the documentation](https://www.elastic.co/guide/en/kibana/current/kibana-debugging.html#_quick_start_guide) adding the config to `config/kibana.dev.yml` should be the correct approach.

Do you see any errors or warnings in the logs at all?
