Skip to content

Commit 6b7be71

Browse files
authored
Merge pull request #1603 from ydb-platform/fix-conns-counter
fix broken metric `ydb_go_sdk_ydb_database_sql_conns`
2 parents d157002 + 5047bea commit 6b7be71

File tree

12 files changed

+256
-153
lines changed

12 files changed

+256
-153
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed broken metric `ydb_go_sdk_ydb_database_sql_conns`
2+
13
## v3.96.1
24
* Fixed drop session from pool unnecessary in query service
35

internal/xslices/keys.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package xslices
2+
3+
import (
4+
"cmp"
5+
"slices"
6+
)
7+
8+
func Keys[Key cmp.Ordered, T any](m map[Key]T) []Key {
9+
keys := make([]Key, 0, len(m))
10+
11+
for key := range m {
12+
keys = append(keys, key)
13+
}
14+
15+
slices.Sort(keys)
16+
17+
return keys
18+
}

internal/xslices/keys_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package xslices
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestKeys(t *testing.T) {
10+
require.Equal(t, []string{"1", "2"}, Keys(map[string]any{
11+
"1": nil,
12+
"2": nil,
13+
}))
14+
require.Equal(t, []int{1, 2}, Keys(map[int]any{
15+
1: nil,
16+
2: nil,
17+
}))
18+
}

internal/xsql/connector.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
1616
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query"
17+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1718
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1819
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1920
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/legacy"
@@ -67,8 +68,8 @@ func (e Engine) String() string {
6768
switch e {
6869
case LEGACY:
6970
return "LEGACY"
70-
case QUERY_SERVICE:
71-
return "QUERY_SERVICE"
71+
case PROPOSE:
72+
return "PROPOSE"
7273
default:
7374
return "UNKNOWN"
7475
}
@@ -115,26 +116,33 @@ func (c *Connector) Scheme() scheme.Client {
115116
}
116117

117118
const (
118-
QUERY_SERVICE = iota + 1 //nolint:revive,stylecheck
119-
LEGACY //nolint:revive,stylecheck
119+
PROPOSE = iota + 1
120+
LEGACY
120121
)
121122

122123
func (c *Connector) Open(name string) (driver.Conn, error) {
123124
return nil, xerrors.WithStackTrace(driver.ErrSkip)
124125
}
125126

126-
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
127+
func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, finalErr error) { //nolint:funlen
128+
onDone := trace.DatabaseSQLOnConnectorConnect(c.Trace(), &ctx,
129+
stack.FunctionID("", stack.Package("database/sql")),
130+
)
131+
127132
switch c.processor {
128-
case QUERY_SERVICE:
133+
case PROPOSE:
129134
s, err := query.CreateSession(ctx, c.Query())
135+
defer func() {
136+
onDone(s, finalErr)
137+
}()
130138
if err != nil {
131139
return nil, xerrors.WithStackTrace(err)
132140
}
133141

134142
id := uuid.New()
135143

136144
conn := &Conn{
137-
processor: QUERY_SERVICE,
145+
processor: PROPOSE,
138146
cc: propose.New(ctx, c, s, append(
139147
c.Options,
140148
propose.WithOnClose(func() {
@@ -152,6 +160,9 @@ func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
152160

153161
case LEGACY:
154162
s, err := c.Table().CreateSession(ctx) //nolint:staticcheck
163+
defer func() {
164+
onDone(s, finalErr)
165+
}()
155166
if err != nil {
156167
return nil, xerrors.WithStackTrace(err)
157168
}
@@ -207,7 +218,7 @@ func Open(parent ydbDriver, balancer grpc.ClientConnInterface, opts ...Option) (
207218
balancer: balancer,
208219
processor: func() Engine {
209220
if overQueryService, _ := strconv.ParseBool(os.Getenv("YDB_DATABASE_SQL_OVER_QUERY_SERVICE")); overQueryService {
210-
return QUERY_SERVICE
221+
return PROPOSE
211222
}
212223

213224
return LEGACY

internal/xsql/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func WithQueryOptions(opts ...propose.Option) Option {
200200

201201
func WithQueryService(b bool) Option {
202202
if b {
203-
return queryProcessorOption(QUERY_SERVICE)
203+
return queryProcessorOption(PROPOSE)
204204
}
205205

206206
return queryProcessorOption(LEGACY)

0 commit comments

Comments
 (0)