Skip to content

feat: introduce Commenter interface - #633

Open
boekkooi-impossiblecloud wants to merge 1 commit into
XSAM:mainfrom
boekkooi-impossiblecloud:boekkooi/sqlcommenter
Open

boekkooi-impossiblecloud wants to merge 1 commit into
XSAM:mainfrom
boekkooi-impossiblecloud:boekkooi/sqlcommenter

Conversation

@boekkooi-impossiblecloud

Copy link
Copy Markdown
Contributor

Good day!

This PR introduces the Commenter interface which allows users to use their own custom SQL commenter.
This can be useful when different comments are preferred for SQL statements.

Thanks for having a look and please let me know what you think.
Have a great weekend.

@XSAM

XSAM commented Aug 12, 2026

Copy link
Copy Markdown
Owner

@boekkooi-impossiblecloud Hey, sorry for the late response.

Could you provide a use case where users want to use a different comment style?

@boekkooi-impossiblecloud

Copy link
Copy Markdown
Contributor Author

Hey @XSAM

No worries, I'm already thankful you took the time to review this PR 😄.

In my case I needed a way to add specific comments to the end of all prepare queries which contains specific environment information (e.g. we have a shared set of repositories that are used by multiple workers and we needed to trace which worker called which prepared queries) but we also need to keep the tracing info for query calls.

As the use case is a bit more custom I think the interface makes sense but if you have a better Idea I'm game 😄

Thanks again and have a great day

@XSAM XSAM added this to the v0.45.0 milestone Sep 5, 2026
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.1%. Comparing base (1c61098) to head (d3dfae1).

Additional details and impacted files
@@           Coverage Diff           @@
##            main    #633     +/-   ##
=======================================
+ Coverage   89.9%   90.1%   +0.2%     
=======================================
  Files         16      16             
  Lines        608     622     +14     
=======================================
+ Hits         547     561     +14     
  Misses        61      61             

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This allows users of the library to use their own custom commenter.
This can be useful when different comments are prefered for Queries and Prepared statements.
@XSAM

XSAM commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Hey @boekkooi-impossiblecloud, if the goal is to add specific key/values to the comment, a custom propagator already does that today, so the new interface isn’t needed. WithTextMapPropagator only feeds the SQL commenter, so a propagator passed there doesn’t affect the propagator used for HTTP or gRPC.

For instance,

type staticAttrs map[string]string

func (s staticAttrs) Inject(_ context.Context, c propagation.TextMapCarrier) {
	for k, v := range s {
		c.Set(k, v)
	}
}
func (staticAttrs) Extract(ctx context.Context, _ propagation.TextMapCarrier) context.Context { return ctx }
func (s staticAttrs) Fields() []string { /* keys */ }

otelsql.Open("mysql", dsn,
	otelsql.WithSQLCommenter(true),
	otelsql.WithTextMapPropagator(propagation.NewCompositeTextMapPropagator(
		propagation.TraceContext{},
		staticAttrs{"service.name": "shoppingcart"},
	)),
)
// SELECT * FROM songs /*traceparent='…',service.name='shoppingcart'*/

@boekkooi-impossiblecloud

Copy link
Copy Markdown
Contributor Author

Hey @XSAM

Thanks for your response! I looked into using the existing commenter feature but it has some limitations.

In the case of a prepared statements we have to ensure that the query send to the driver is always identical (aka has no traceID/parentTrace) to ensure we re-use the same prepared statement but for a normal Query/Exec we want to include the traceID/parentTrace in order to make your own life easier when debugging a trace.

Sadly the scenario above it not possible with the current commenter which is why I propose this interface (or did I miss something?)

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.

2 participants