Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version-file: './go.mod'

- name: Build
run: go build -v ./...
Expand Down
11 changes: 8 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var ctxOptionsKey ctxKeyType
// to skip the commenting on Query.
//
// client.T.Query().All(sqlcomment.Skip(ctx))
//
func Skip(ctx context.Context) context.Context {
c, ok := ctx.Value(ctxOptionsKey).(*ctxOptions)
if !ok {
Expand All @@ -31,6 +30,7 @@ func Skip(ctx context.Context) context.Context {

// WithTag stores the key and val pair on the context.
// for example, if you want to add `route` tag to your SQL comment, put the url path on request context:
//
// middleware := func(next http.Handler) http.Handler {
// fn := func(w http.ResponseWriter, r *http.Request) {
// ctx := sqlcomment.WithTag(r.Context(), "route", r.URL.Path)
Expand All @@ -43,8 +43,13 @@ func WithTag(ctx context.Context, key, val string) context.Context {
if !ok {
return context.WithValue(ctx, ctxOptionsKey, &ctxOptions{tags: Tags{key: val}})
}
t.tags[key] = val
return ctx
// Create a copy of the existing ctxOptions to avoid modifying the original
newTags := make(Tags)
for k, v := range t.tags {
newTags[k] = v
}
newTags[key] = val
return context.WithValue(ctx, ctxOptionsKey, &ctxOptions{tags: newTags})
}

// FromContext returns the tags stored in ctx, if any.
Expand Down
37 changes: 37 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sqlcomment

import (
"context"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

func TestWithTagThreadSafety(t *testing.T) {
ctx := context.Background()
var wg sync.WaitGroup
const numGoroutines = 100
results := make(map[int]Tags)
mu := sync.Mutex{}
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
// Add a unique tag for each goroutine
newCtx := WithTag(ctx, "goroutine", string(rune('A'+i)))
tags := FromContext(newCtx)
// Store the result in the map
mu.Lock()
results[i] = tags
mu.Unlock()
}(i)
}

wg.Wait()
// Verify that each goroutine has its own independent tags
assert.Equal(t, numGoroutines, len(results))
for i := 0; i < numGoroutines; i++ {
assert.Contains(t, results[i], "goroutine")
assert.Equal(t, string(rune('A'+i)), results[i]["goroutine"])
}
}
3 changes: 0 additions & 3 deletions examples/ent/migrate/migrate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/otel/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (mcc CustomCommenter) Tag(ctx context.Context) sqlcomment.Tags {
}
}

func Example_OTELIntegration() {
func Example_otelIntegration() {
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
Expand Down
47 changes: 27 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
module ariga.io/sqlcomment

go 1.19
go 1.23.0

toolchain go1.24.3

require (
entgo.io/ent v0.11.3-0.20220816070906-2b54aadcce3a
github.com/mattn/go-sqlite3 v1.14.14
github.com/stretchr/testify v1.8.0
go.opencensus.io v0.23.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.25.0
go.opentelemetry.io/otel v1.0.1
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.0.1
go.opentelemetry.io/otel/sdk v1.0.1
entgo.io/ent v0.14.4
github.com/mattn/go-sqlite3 v1.14.28
github.com/stretchr/testify v1.10.0
go.opencensus.io v0.24.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0
go.opentelemetry.io/otel v1.37.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.37.0
go.opentelemetry.io/otel/sdk v1.37.0
)

require (
ariga.io/atlas v0.6.0 // indirect
ariga.io/atlas v0.31.1-0.20250212144724-069be8033e83 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.opentelemetry.io/otel/internal/metric v0.24.0 // indirect
go.opentelemetry.io/otel/metric v0.24.0 // indirect
go.opentelemetry.io/otel/trace v1.0.1 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
github.com/zclconf/go-cty-yaml v1.1.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.37.0 // indirect
go.opentelemetry.io/otel/trace v1.37.0 // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading