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

Added support for go-sql-driver #161

Merged
merged 22 commits into from
Sep 30, 2022

Conversation

Thiyagu55
Copy link
Collaborator

@Thiyagu55 Thiyagu55 commented Sep 19, 2022

This PR implements support for QueryRow method and QueryContext method

Usage
Import sqlcommentergo and use it's db driver

db, err := sqlcommenterGo.Open("mysql", "root:root@/gotest", sqlcommenter.CommenterOptions{EnableDBDriver: true, EnableTraceparent: true})

bsp := sdktrace.NewSimpleSpanProcessor(exp) // Create and add span processor
	tp := sdktrace.NewTracerProvider(
		sdktrace.WithSampler(sdktrace.AlwaysSample()),
		sdktrace.WithSpanProcessor(bsp),
	)

	propgator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})

	ctx, span := tp.Tracer("foo").Start(context.Background(), "parent-span-name")
	defer span.End()

	db.AddorUpdateCurrentSpan(ctx)
	

For http-router middleware implementation

// middleware is used to intercept incoming HTTP calls and apply general functions upon them.
func middleware(n httprouter.Handle) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		ctx := sqlcommenter.AddHttpRouterTags(r)
		log.Printf("HTTP request sent to %s from %s", r.URL.Path, r.RemoteAddr)

		// call registered handler
		n(w, r.WithContext(ctx), ps)
	}
}
  • dbDriver is static and will be set by the sqlcommenter
  • route needs to be passed via context

go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
@Thiyagu55 Thiyagu55 changed the title Added support for Query Row and Query Context Added support for go-sql-driver Sep 23, 2022
@sjs994
Copy link
Collaborator

sjs994 commented Sep 23, 2022

Hi Thiyagu, Have updated few files. Please verify that things looks right.

@Thiyagu55
Copy link
Collaborator Author

Thiyagu55 commented Sep 23, 2022 via email

go/go-sql.go Outdated Show resolved Hide resolved
go/README.md Outdated Show resolved Hide resolved
go/README.md Outdated Show resolved Hide resolved
go/README.md Outdated Show resolved Hide resolved
go/README.md Outdated Show resolved Hide resolved
@sjs994
Copy link
Collaborator

sjs994 commented Sep 23, 2022

Please resolve the comments as they may not be required after the last commit.

go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@sjs994 sjs994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This OTEL propagation model will not work. Please do the needful change.

go/go-sql.go Outdated Show resolved Hide resolved
go/go-sql_test.go Outdated Show resolved Hide resolved
go/go-sql_test.go Outdated Show resolved Hide resolved
go/go-sql_test.go Outdated Show resolved Hide resolved
go/go-sql_test.go Outdated Show resolved Hide resolved
go/README.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@sjs994 sjs994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks okay.

How much effort will it be to move all these into a folder one level down go/go-sql ?
This is now under go folder, but the implementation is only for go database/sql. It's better to separate it in my opinion from start.

go/README.md Outdated Show resolved Hide resolved
.github/workflows/go-tests.yaml Outdated Show resolved Hide resolved
@Thiyagu55 Thiyagu55 merged commit 89887ed into google:master Sep 30, 2022
ctx := AddHttpRouterTags(r, context.Background())

got := db.withComment(ctx, "Select 1")
want := "Select 1/*driver=database%2Fsql,framework=net%2Fhttp,route=hello%2F1*/"
Copy link

@bdewater bdewater Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing escaping % like Python and PHP.

Why not use the same examples their test suites and are shown on the public docs, i.e. testing congo=t61rcWkgMzE,rojo=00f067aa0ba902b7 becomes congo%%3Dt61rcWkgMzE%%2Crojo%%3D00f067aa0ba902b7.

Edit: see #168 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants