SkyAPM / go2sky

Distributed tracing and monitor SDK in Go for Apache SkyWalking APM
https://skywalking.apache.org/
Apache License 2.0
447 stars 122 forks source link

Database is not getting registered and the database is not being displayed in the topology #77

Closed Saicasm closed 3 years ago

Saicasm commented 3 years ago

Describe the bug Database metrics are not getting generated when the agent is connected to OAP backend

To Reproduce

Expected behavior

Screenshots Screen Shot 2020-11-19 at 17 12 45 Screen Shot 2020-11-19 at 17 15 44 Screen Shot 2020-11-19 at 17 15 13

Code main file of demo app `package main

import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "strconv"

"github.com/SkyAPM/go2sky"
httpPlugin "github.com/SkyAPM/go2sky/plugins/http"
"github.com/SkyAPM/go2sky/reporter"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"

)

var db *gorm.DB var err error

type Booking struct { Id int json:"id" User string json:"user" Members int json:"members" }

func homePage(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Welcome to HomePage!") fmt.Println("Endpoint Hit: HomePage") }

func main() {

database, err := gorm.Open("mysql", "user:password@/test?charset=utf8&parseTime=True&loc=Local")
if err != nil {
    panic("Failed to connect to database!")
}

database.AutoMigrate(&Booking{})
db = database
re, err := reporter.NewGRPCReporter("localhost:11800")
if err != nil {
    log.Fatalf("new reporter error %v \n", err)
}
defer re.Close()
// re, err := reporter.NewLogReporter()
// if err != nil {
//  log.Fatalf("new reporter error %v \n", err)
// }
// defer re.Close()

tracer, err := go2sky.NewTracer("go-http-client", go2sky.WithReporter(re))
if err != nil {
    log.Fatalf("create tracer error %v \n", err)
}

sm, err := httpPlugin.NewServerMiddleware(tracer)
if err != nil {
    log.Fatalf("create server middleware error %v \n", err)
}
// span, ctx, err := tracer.CreateLocalSpan(context.Background())
// go2sky.TraceID(ctx)
// subSpan, newCtx, err := tracer.CreateLocalSpan(ctx)
// fmt.Println(newCtx)
// subSpan.End()
// span.End()

log.Println("Starting development server at http://127.0.0.1:10000/")
log.Println("Quit the server with CONTROL-C.")
// creates a new instance of a mux router

myRouter := mux.NewRouter().StrictSlash(true)

myRouter.HandleFunc("/", homePage)
myRouter.HandleFunc("/new-booking", createNewBooking).Methods("POST")
myRouter.HandleFunc("/all-bookings", returnAllBookings)
myRouter.HandleFunc("/booking/{id}", returnSingleBooking)
log.Fatal(http.ListenAndServe(":10000", sm(myRouter)))

} func createNewBooking(w http.ResponseWriter, r http.Request) { // get the body of our POST request // return the string response containing the request body reqBody, _ := ioutil.ReadAll(r.Body) var booking Booking json.Unmarshal(reqBody, &booking) db.Create(&booking) fmt.Println("Endpoint Hit: Creating New Booking") json.NewEncoder(w).Encode(booking) } func returnAllBookings(w http.ResponseWriter, r http.Request) { bookings := []Booking{} db.Find(&bookings) fmt.Println("Endpoint Hit: returnAllBookings") json.NewEncoder(w).Encode(bookings) } func returnSingleBooking(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) key := vars["id"] bookings := []Booking{} db.Find(&bookings) for _, booking := range bookings { // string to int s, err := strconv.Atoi(key) if err == nil { if booking.Id == s { fmt.Println(booking) fmt.Println("Endpoint Hit: Booking No:", key) json.NewEncoder(w).Encode(booking) } } } } `

Desktop (please complete the following information):

Additional context I'm fairly new to agent side connections ,please let me know if there is anomaly in the agent connection i followed the steps shown on the go2Sky repo

arugal commented 3 years ago
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "strconv"

    "github.com/SkyAPM/go2sky"
    httpPlugin "github.com/SkyAPM/go2sky/plugins/http"
    "github.com/SkyAPM/go2sky/reporter"
    "github.com/gorilla/mux"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

var db *gorm.DB
var err error

type Booking struct {
    Id      int    `json:"id"`
    User    string `json:"user"`
    Members int    `json:"members"`
}

func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Welcome to HomePage!")
    fmt.Println("Endpoint Hit: HomePage")
}

func main() {

    database, err := gorm.Open("mysql", "user:password@/test?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        panic("Failed to connect to database!")
    }

    database.AutoMigrate(&Booking{})
    db = database
    re, err := reporter.NewGRPCReporter("localhost:11800")
    if err != nil {
        log.Fatalf("new reporter error %v \n", err)
    }
    defer re.Close()
    // re, err := reporter.NewLogReporter()
    // if err != nil {
    //  log.Fatalf("new reporter error %v \n", err)
    // }
    // defer re.Close()

    tracer, err := go2sky.NewTracer("go-http-client", go2sky.WithReporter(re))
    if err != nil {
        log.Fatalf("create tracer error %v \n", err)
    }

    sm, err := httpPlugin.NewServerMiddleware(tracer)
    if err != nil {
        log.Fatalf("create server middleware error %v \n", err)
    }
    // span, ctx, err := tracer.CreateLocalSpan(context.Background())
    // go2sky.TraceID(ctx)
    // subSpan, newCtx, err := tracer.CreateLocalSpan(ctx)
    // fmt.Println(newCtx)
    // subSpan.End()
    // span.End()

    log.Println("Starting development server at http://127.0.0.1:10000/")
    log.Println("Quit the server with CONTROL-C.")
    // creates a new instance of a mux router

    myRouter := mux.NewRouter().StrictSlash(true)

    myRouter.HandleFunc("/", homePage)
    myRouter.HandleFunc("/new-booking", createNewBooking).Methods("POST")
    myRouter.HandleFunc("/all-bookings", returnAllBookings)
    myRouter.HandleFunc("/booking/{id}", returnSingleBooking)
    log.Fatal(http.ListenAndServe(":10000", sm(myRouter)))
}
func createNewBooking(w http.ResponseWriter, r *http.Request) {
    // get the body of our POST request
    // return the string response containing the request body
    reqBody, _ := ioutil.ReadAll(r.Body)
    var booking Booking
    json.Unmarshal(reqBody, &booking)
    db.Create(&booking)
    fmt.Println("Endpoint Hit: Creating New Booking")
    json.NewEncoder(w).Encode(booking)
}
func returnAllBookings(w http.ResponseWriter, r *http.Request) {
    bookings := []Booking{}
    db.Find(&bookings)
    fmt.Println("Endpoint Hit: returnAllBookings")
    json.NewEncoder(w).Encode(bookings)
}
func returnSingleBooking(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    key := vars["id"]
    bookings := []Booking{}
    db.Find(&bookings)
    for _, booking := range bookings {
        // string to int
        s, err := strconv.Atoi(key)
        if err == nil {
            if booking.Id == s {
                fmt.Println(booking)
                fmt.Println("Endpoint Hit: Booking No:", key)
                json.NewEncoder(w).Encode(booking)
            }
        }
    }
}

Hi @Saicasm, currently go2sky does not have gorm plugin.

arugal commented 3 years ago

@Saicasm I found that gorm has hooks and maybe we can implement a plugin based on them.

Saicasm commented 3 years ago

Hi @arugal thanks for the response ,so go2sky doesn't support any database related metrics ?

arugal commented 3 years ago

Hi @arugal thanks for the response ,so go2sky doesn't support any database related metrics ?

For now, yes. Here is the list of plugins https://github.com/SkyAPM/go2sky-plugins#go2sky-plugins

Saicasm commented 3 years ago

Hi @arugal thanks for the response ,so go2sky doesn't support any database related metrics ?

For now, yes. Here is the list of plugins https://github.com/SkyAPM/go2sky-plugins#go2sky-plugins

Okay, thank you @arugal

wu-sheng commented 3 years ago

@Saicasm I think the point is you need to have the exit span, with spanLayer=DATABASE and several specific tag keys.

https://github.com/apache/skywalking/blob/master/oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/parser/listener/MultiScopesAnalysisListener.java#L198-L232

Saicasm commented 3 years ago

@Saicasm I think the point is you need to have the exit span, with spanLayer=DATABASE and several specific tag keys.

https://github.com/apache/skywalking/blob/master/oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/parser/listener/MultiScopesAnalysisListener.java#L198-L232

@wu-sheng thanks for the info ,i'm able to register database but the traces and other db metrics are not shown and i believe we have it coming up in the go2sky roadmap !? Also for distributed tracing does it have to be manually instrumented ? Go doesn't support auto instrumentation ?

wu-sheng commented 3 years ago

I don't know what do you mean. Is database showing up on the UI? If so, it must have included in a span.

And about auto instrumentation, yes, I don't think there is a way, according to Google golang team.

arugal commented 3 years ago

Discuss in new issue https://github.com/SkyAPM/go2sky/issues/78