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
I use this compose to provide services GitHub - deviantony/docker-elk: The Elastic stack (ELK) powered by Docker and Compose.
But any SQL trace is present on APM, only web request is present
My Code is exactly like the guide, I tested with Elastic Cloud too
axw
(Andrew Wilkins)
April 4, 2022, 2:10am
2
@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?
This is my code
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"]),
})
}
axw
(Andrew Wilkins)
April 13, 2022, 2:27am
5
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
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:
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:
Update:
Just v2 of GORM module doesn' t work, I test with v1 and this works normally.
apmgorm it's works with Postgres
apmgormv2 doesn't work with Postgres but works with Sqlite
I created this repo with my code.
axw
(Andrew Wilkins)
April 26, 2022, 3:08am
9
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
system
(system)
Closed
May 17, 2022, 10:53am
11
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.