# Apm Agent Send Duplicate record in mysql

**URL:** <https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [December 17, 2022, 3:34am UTC](https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440 "2022-12-17T03:34:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brayan\_Alvarez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brayan_alvarez/32/114839_2.png) [@Brayan\_Alvarez](https://discuss.elastic.co/u/Brayan_Alvarez)\
**Post date:** [December 17, 2022, 3:34am UTC](https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440/1 "2022-12-17T03:34:36Z")

</div>

**Kibana version** : 8.5.3

**Elasticsearch version** : 8.5.3

**APM Server version** : 8.5.3

**APM Agent language and version** : Node Js 3.41.0

**Browser version** : FIrefox

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**: deb

**Fresh install or upgraded from other version?** Fresh install

**Is there anything special in your setup?** No

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:  
We have worked with version 3.31.0 of the agent for node js, this version works fine, when we update to the latest version we now have a duplicate record of the last sql (mysql) operation executed inside a function, when we add a span in the function.

If we don't add the span in the function, it works fine, but when we add the span it generates the duplicate, this doesn't happen in version 3.31.0.

We tried remove version 3.41.0 and then install version 3.31.0 again and it works fine  
Code **Steps to reproduce** :

```auto
// Add this to the VERY top of the first file loaded in your app
const apm = require('elastic-apm-node').start({
    // Override service name from package.json
    // Allowed characters: a-z, A-Z, 0-9, -, _, and space
    serviceName: 'EXPRESS2',
  
    // Set custom APM Server URL (default: http://127.0.0.1:8200)
    serverUrl: 'http://192.168.XX,XX:8200',
  })
  
  async function consulta_1(data) 
  {
    //Creamos el Span
    const span = apm.startSpan('Procedure.subcuentavehiculos',"funcion","m_test","custom",{ exitSpan: false })

    sql = `SELECT privileges FROM gs_users WHERE id =${data.id}`
    resultado = await consulta(sql)

    // Finalizamos el Span
    if (span) span.end()
  }

async function inicio() 
{
    let obj ={"host":"192.168.xx.xx","user":"xxxxxxx","password":"xxxxxxx","database":"xxxxxxx","dateStrings":true,"multipleStatements": true}

    /*Conexion Mysql */
    resultado = await require('./middleware/database')( obj )  
    console.log(resultado)
    const app = require('express')()
  
    app.get('/', async function (req, res) {
        await consulta_1({"user_id":"7","id":32})
      res.send('Hello World!')
    })
    
    app.listen(3020)
}
 inicio()

```

 ![Error Apm Agent](https://us1.discourse-cdn.com/elastic/original/3X/c/b/cb8a248f5cc81987cdec332985592ff78838a7cf.png)

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 19, 2022, 10:53pm UTC](https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440/2 "2022-12-19T22:53:46Z")

</div>

@Brayan_Alvarez Thank you very much for the bug report and especially the sample code.

I can reproduce the issue with this slightly modified code:

```auto
// dupe-mysql-spans.example.js
const apm = require('./').start({ // elastic-apm-node
  serviceName: 'dupe-mysql-spans',
  // spanCompressionEnabled: false
  // serverUrl: 'http://192.168.XX,XX:8200'
})

const http = require('http')
const mysql2Promise = require('mysql2/promise')

let conn

async function consulta (sql) {
  return conn.query(sql)
}

async function consulta_1 (data) {
  // Creamos el Span
  const span = apm.startSpan('Procedure.subcuentavehiculos', 'funcion', 'm_test', 'custom', { exitSpan: false })

  var sql = 'SELECT 1 + 1 as solution'
  var resultado = await consulta(sql)
  console.log('resultado: ', resultado)

  // Finalizamos el Span
  if (span) span.end()
}

async function inicio () {
  // Conexion Mysql
  conn = await mysql2Promise.createConnection({ user: 'root' })

  const app = require('express')()

  app.get('/', async function (req, res) {
    await consulta_1({ user_id: '7', id: 32 })
    res.send('Hello World!')
  })

  app.listen(3020, function () {
    http.get('http://127.0.0.1:3020/', res => {
      res.resume()
      res.on('end', () => {
        console.log('done request')
        apm.flush(() => {
          process.exit()
        })
      })
    })
  })
}
inicio()

```

This issue was introduced in v3.32.0 of elastic-apm-node when the [span compression feature](https://www.elastic.co/guide/en/apm/guide/current/span-compression.html) was added. As a workaround you can avoid the error by turning off span compression with the [`spanCompressionEnabled: false` config setting](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#span-compression-enabled).

I have [a fix coming](https://github.com/elastic/apm-agent-nodejs/pull/3076). I hope to get a release out soon with that.

---

<div class="post-metadata">

**Author:** ![Brayan\_Alvarez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brayan_alvarez/32/114839_2.png) [@Brayan\_Alvarez](https://discuss.elastic.co/u/Brayan_Alvarez)\
**Post date:** [December 20, 2022, 11:50am UTC](https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440/3 "2022-12-20T11:50:10Z")

</div>

It is a pleasure to be able to support, greetings.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 21, 2022, 3:49pm UTC](https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440/4 "2022-12-21T15:49:50Z")

</div>

elastic-apm-node@3.41.1 is published with a fix for this now.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 11, 2023, 11:49am UTC](https://discuss.elastic.co/t/apm-agent-send-duplicate-record-in-mysql/321440/5 "2023-01-11T11:49:52Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
