Skip to content

Commit

Permalink
Add logging for config use (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldld authored Sep 26, 2022
1 parent 3cc01f2 commit a044001
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
23 changes: 23 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"fmt"
"log"
"runtime/debug"
"time"

"github.com/apache/thrift/lib/go/thrift"
Expand All @@ -17,9 +18,18 @@ type Conn struct {
session *hive.Session
client *hive.Client
log *log.Logger

// Randomly-generated unique ID used to refer to this Conn.
id uint64
}

func (c *Conn) logOp(op string) {
c.log.Printf("%s: connId=%d\n%s", op, c.id, debug.Stack())
}

func (c *Conn) Ping(ctx context.Context) error {
c.logOp("Ping")

session, err := c.OpenSession(ctx)
if err != nil {
return hive.WithStack(err)
Expand All @@ -36,6 +46,8 @@ func (c *Conn) Ping(ctx context.Context) error {
// and is called in place of any ColumnConverter. CheckNamedValue must do type
// validation and conversion as appropriate for the driver.
func (c *Conn) CheckNamedValue(val *driver.NamedValue) error {
c.logOp("CheckNamedValue")

t, ok := val.Value.(time.Time)
if ok {
val.Value = t.Format(hive.TimestampFormat)
Expand All @@ -51,6 +63,8 @@ func (c *Conn) Prepare(query string) (driver.Stmt, error) {

// PrepareContext returns prepared statement
func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
c.logOp("PrepareContext")

return &Stmt{
conn: c,
stmt: template(query),
Expand All @@ -59,6 +73,8 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e

// QueryContext executes a query that may return rows
func (c *Conn) QueryContext(ctx context.Context, q string, args []driver.NamedValue) (driver.Rows, error) {
c.logOp("QueryContext")

session, err := c.OpenSession(ctx)
if err != nil {
return nil, hive.WithStack(err)
Expand All @@ -74,6 +90,8 @@ func (c *Conn) QueryContext(ctx context.Context, q string, args []driver.NamedVa

// ExecContext executes a query that doesn't return rows
func (c *Conn) ExecContext(ctx context.Context, q string, args []driver.NamedValue) (driver.Result, error) {
c.logOp("ExecContext")

session, err := c.OpenSession(ctx)
if err != nil {
return nil, hive.WithStack(err)
Expand All @@ -94,6 +112,8 @@ func (c *Conn) Begin() (driver.Tx, error) {

// OpenSession ensure opened session
func (c *Conn) OpenSession(ctx context.Context) (*hive.Session, error) {
c.logOp("OpenSession")

if c.session == nil {
session, err := c.client.OpenSession(ctx)
if err != nil {
Expand All @@ -107,6 +127,8 @@ func (c *Conn) OpenSession(ctx context.Context) (*hive.Session, error) {

// ResetSession closes hive session
func (c *Conn) ResetSession(ctx context.Context) error {
c.logOp("ResetSession")

if c.session != nil {
if err := c.session.Close(ctx); err != nil {
return hive.WithStack(err)
Expand All @@ -118,6 +140,7 @@ func (c *Conn) ResetSession(ctx context.Context) error {

// Close connection
func (c *Conn) Close() error {
c.logOp("Close")
c.log.Printf("close connection")
return c.t.Close()
}
17 changes: 16 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"errors"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -109,6 +111,16 @@ func parseURI(uri string) (*Options, error) {
}
}

if logOut, ok := query["logOut"]; ok {
if logOut[0] == "stdout" {
opts.LogOut = os.Stdout
} else if logOut[0] == "stderr" {
opts.LogOut = os.Stderr
} else {
return nil, fmt.Errorf("invalid logOut %s", logOut[0])
}
}

return &opts, nil
}

Expand Down Expand Up @@ -176,5 +188,8 @@ func connect(opts *Options) (*Conn, error) {

client := hive.NewClient(tclient, logger, &hive.Options{MaxRows: opts.MaxRows, Loc: opts.Loc})

return &Conn{client: client, t: transport, log: logger}, nil
connId := rand.Uint64()

logger.Printf("connect: connId=%d host=%s port=%s httpPath=%s", connId, opts.Host, opts.Port, opts.HTTPPath)
return &Conn{client: client, t: transport, log: logger, id: connId}, nil
}
8 changes: 4 additions & 4 deletions hive/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func (op *Operation) GetResultSetMetadata(ctx context.Context) (*TableSchema, er
})
}

for _, col := range schema.Columns {
op.hive.log.Printf("fetch schema: %v", col)
}
//for _, col := range schema.Columns {
// op.hive.log.Printf("fetch schema: %v", col)
//}
}

return schema, nil
Expand Down Expand Up @@ -99,7 +99,7 @@ func fetch(ctx context.Context, op *Operation, schema *TableSchema) (*cli_servic
return nil, WithStack(err)
}

op.hive.log.Printf("results: %v", resp.Results)
//op.hive.log.Printf("results: %v", resp.Results)
return resp, nil
}

Expand Down

0 comments on commit a044001

Please sign in to comment.