Closed Saicasm closed 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.
@Saicasm I found that gorm
has hooks and maybe we can implement a plugin based on them.
Hi @arugal thanks for the response ,so go2sky doesn't support any database related metrics ?
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
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
@Saicasm I think the point is you need to have the exit span, with spanLayer=DATABASE
and several specific tag keys.
@Saicasm I think the point is you need to have the exit span, with
spanLayer=DATABASE
and several specific tag keys.
@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 ?
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.
Discuss in new issue https://github.com/SkyAPM/go2sky/issues/78
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 15 13](https://user-images.githubusercontent.com/53043627/99662840-cb9fcb80-2a8b-11eb-8e13-156208051bb1.png)
Code main file of demo app `package main
import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "strconv"
)
var db *gorm.DB var err error
type Booking struct { Id int
json:"id"
User stringjson:"user"
Members intjson:"members"
}func homePage(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Welcome to HomePage!") fmt.Println("Endpoint Hit: HomePage") }
func main() {
} 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