Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#69] calcite-avatica-go plugin #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions plugin/avatica/avatica.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ppavatica

import (
"database/sql"
avatica "github.com/apache/calcite-avatica-go/v5"
"github.com/pinpoint-apm/pinpoint-go-agent"
"net"
"net/url"
"strings"
)

var dbInfo = pinpoint.DBInfo{
ParseDSN: parseDSN,
}

func init() {
dbInfo.DBType = pinpoint.ServiceTypeAvatica // TODO: need to add
dbInfo.QueryType = pinpoint.ServiceTypeAvaticaExecuteQuery // TODO: need to add
sql.Register("sqlserver-avatica", pinpoint.WrapSQLDriver(&avatica.Driver{}, dbInfo))
}

func parseDSN(info *pinpoint.DBInfo, dsn string) {
u, err := url.Parse(dsn)
if err != nil {
return
}

host, _, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
} else if host == "" {
host = "localhost"
}

s := strings.Split(u.Path, "/")
schema := s[len(s)-1]

info.DBHost = host
info.DBName = schema
}
69 changes: 69 additions & 0 deletions plugin/avatica/example/avatica_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"database/sql"
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/pinpoint-apm/pinpoint-go-agent"
_ "github.com/pinpoint-apm/pinpoint-go-agent/plugin/avatica"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/http"
)

func query(w http.ResponseWriter, r *http.Request) {
dsn := "http://localhost:8765/myschema?maxRowsTotal=1&frameMaxSize=1&location=Australia/Melbourne&transactionIsolation=8&authentication=BASIC&avaticaUser=someuser&avaticaPassword=somepassword"
db, err := sql.Open("sqlserver-pinpoint", dsn)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, err.Error())
return
}
defer db.Close()

ctx := r.Context()
_, _ = db.ExecContext(ctx, "CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)")

stmt, err := db.Prepare("INSERT INTO Inventory VALUES (@p1, @p2, @p3)")
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, err.Error())
return
}

_, _ = stmt.ExecContext(ctx, 1, "foo", 100)
_, _ = stmt.ExecContext(ctx, 2, "bar", 200)
stmt.Close()

var (
id int
name string
quantity int
)

rows, _ := db.QueryContext(ctx, "SELECT * FROM Inventory")
for rows.Next() {
_ = rows.Scan(&id, &name, &quantity)
fmt.Printf("user: %d, %s, %d\n", id, name, quantity)
}
rows.Close()
}

func main() {
opts := []pinpoint.ConfigOption{
pinpoint.WithAppName("GoMsSQLTest"),
pinpoint.WithConfigFile(os.Getenv("HOME") + "/tmp/pinpoint-config.yaml"),
}
cfg, _ := pinpoint.NewConfig(opts...)
agent, err := pinpoint.NewAgent(cfg)
if err != nil {
log.Fatalf("pinpoint agent start fail: %v", err)
}
defer agent.Shutdown()

http.HandleFunc("/query", pphttp.WrapHandlerFunc(query))

http.ListenAndServe(":9000", nil)
}
14 changes: 14 additions & 0 deletions plugin/avatica/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/pinpoint-apm/pinpoint-go-agent/plugin/avatica

go 1.15

require (
github.com/apache/calcite-avatica-go/v5 v5.2.0
github.com/pinpoint-apm/pinpoint-go-agent v1.3.1
github.com/pinpoint-apm/pinpoint-go-agent/plugin/http v1.3.0
)


replace github.com/pinpoint-apm/pinpoint-go-agent => ../..

replace github.com/pinpoint-apm/pinpoint-go-agent/plugin/http => ../http