# SQL traces don't catched by agent

**URL:** <https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360>\
**Category:** APM\
**Tags:** docker, go\
**Created:** [April 1, 2022, 5:50pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360 "2022-04-01T17:50:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 1, 2022, 5:50pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/1 "2022-04-01T17:50:27Z")

</div>

Agent don't catch any trace when using GORM, PG or APMSQL modules.

I follow this guide, [Como fazer instrumentação do seu aplicativo Go com o agente Go do Elastic APM | Elastic Blog](https://www.elastic.co/pt/blog/how-to-instrument-your-go-app-with-the-elastic-apm-go-agent)  
I use this compose to provide services [GitHub - deviantony/docker-elk: The Elastic stack (ELK) powered by Docker and Compose.](https://github.com/deviantony/docker-elk)  
But any SQL trace is present on APM, only web request is present

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/0/a09953cedb32958c99c8bf447737b14b010e8cab.png)

My Code is exactly like the guide, I tested with Elastic Cloud too

---

<div class="post-metadata">

**Author:** ![axw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/axw/32/28197_2.png) [@axw](https://discuss.elastic.co/u/axw)\
**Post date:** [April 4, 2022, 2:10am UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/2 "2022-04-04T02:10:24Z")

</div>

@Adao_Santos welcome to the forum!

> My Code is exactly like the guide, I tested with Elastic Cloud too

There are many code snippets in that blog. Can you please share your full program, and which SQL operations that aren't showing up in Kibana?

---

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 4, 2022, 6:15pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/3 "2022-04-04T18:15:57Z")

</div>

This is my code

```auto
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	sqlite "go.elastic.co/apm/module/apmgormv2/v2/driver/sqlite"
	"gorm.io/gorm"

	"go.elastic.co/apm/module/apmgorilla/v2"
)

var db *gorm.DB

type Product struct {
	gorm.Model
	Code string
	Price uint
}

func main() {
  var err error
	db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// Migrate the schema
	db.Debug().AutoMigrate(&Product{})

	r := mux.NewRouter()
	r.HandleFunc("/hello/{name}", handler)
	apmgorilla.Instrument(r)
	log.Fatal(http.ListenAndServe(":8000", r))

}

func handler(w http.ResponseWriter, req *http.Request) {
	// Create
	db.Debug().Create(&Product{Code: "D42", Price: 100})

	// Read
	var product Product
	db.Debug().First(&product)

  vars := mux.Vars(req)
	w.Header().Add("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{
		"message": fmt.Sprintf("Hello %s", vars["name"]),
	})
}

```

---

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 12, 2022, 2:43pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/4 "2022-04-12T14:43:30Z")

</div>

Anyone have some idea ?

---

<div class="post-metadata">

**Author:** ![axw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/axw/32/28197_2.png) [@axw](https://discuss.elastic.co/u/axw)\
**Post date:** [April 13, 2022, 2:27am UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/5 "2022-04-13T02:27:14Z")

</div>

Sorry for the delay in getting back to you.

You need to propagate the HTTP request context to the database requests for spans to be produced. You can do that by adding `db := db.WithContext(req.Context())` to the top of `handler`.

See this test code for example: [apm-agent-go/apmgorm\_test.go at b69a26718dfad62b5db0d1aa5a2780d4609d8771 · elastic/apm-agent-go · GitHub](https://github.com/elastic/apm-agent-go/blob/b69a26718dfad62b5db0d1aa5a2780d4609d8771/module/apmgormv2/apmgorm_test.go#L93)

---

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 20, 2022, 4:36pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/6 "2022-04-20T16:36:38Z")

</div>

Thanks for you help, now it`s work when I use sqlite driver, but if I switch to postgres this stops working. My actually code:

```auto
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	postgres "go.elastic.co/apm/module/apmgormv2/driver/postgres"
	"gorm.io/gorm"

	"go.elastic.co/apm/module/apmgorilla/v2"
)

var db *gorm.DB

type Product struct {
	gorm.Model
	Code string
	Price uint
}

func main() {
	var err error
	db, err = gorm.Open(postgres.Open("host=localhost user=goapm sslmode=disable password=secret dbname=goapm"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// Migrate the schema
	db.Debug().AutoMigrate(&Product{})

	r := mux.NewRouter()
	r.HandleFunc("/hello/{name}", handler)
	apmgorilla.Instrument(r)
	log.Fatal(http.ListenAndServe(":8000", r))

}

func handler(w http.ResponseWriter, req *http.Request) {

	db := db.WithContext(req.Context())

	// Create
	db.Debug().Create(&Product{Code: "D42", Price: 100})

	// Read
	var product Product
	db.Debug().First(&product)

	vars := mux.Vars(req)
	w.Header().Add("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{
		"message": fmt.Sprintf("Hello %s", vars["name"]),
	})
}

```

Kibana screen:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/1/51acf9d1d01a0cf5ab0dc2caebdc2e866adc1a81.png)

---

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 20, 2022, 7:12pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/7 "2022-04-20T19:12:23Z")

</div>

Update:

Just v2 of GORM module doesn' t work, I test with v1 and this works normally.

- [apmgorm](https://www.elastic.co/guide/en/apm/agent/go/master/builtin-modules.html#builtin-modules-apmgorm) it's works with Postgres
- [apmgormv2](https://www.elastic.co/guide/en/apm/agent/go/master/builtin-modules.html#_moduleapmgormv2) doesn't work with Postgres but works with Sqlite

I created this [repo](https://github.com/adaosantos/goapm) with my code.

---

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 25, 2022, 5:10pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/8 "2022-04-25T17:10:56Z")

</div>

Anyone have some idea ?

---

<div class="post-metadata">

**Author:** ![axw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/axw/32/28197_2.png) [@axw](https://discuss.elastic.co/u/axw)\
**Post date:** [April 26, 2022, 3:08am UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/9 "2022-04-26T03:08:18Z")

</div>

Thanks for the repo. The version of the `apmgormv2` module needs to match the version of `go.elastic.co/apm/v2`. Please try changing `go.elastic.co/apm/module/apmgormv2` to `go.elastic.co/apm/module/apmgormv2/v2`

---

<div class="post-metadata">

**Author:** ![Adao\_Santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adao_santos/32/103845_2.png) [@Adao\_Santos](https://discuss.elastic.co/u/Adao_Santos)\
**Post date:** [April 26, 2022, 2:53pm UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/10 "2022-04-26T14:53:09Z")

</div>

It's work, thanks bro!!!

---

<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:** [May 17, 2022, 10:53am UTC](https://discuss.elastic.co/t/sql-traces-dont-catched-by-agent/301360/11 "2022-05-17T10:53:21Z")

</div>

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