Skip to content

Commit 1f967e3

Browse files
committed
* Added ydb.WithApplicationName option
1 parent 0ee201e commit 1f967e3

File tree

14 files changed

+112
-36
lines changed

14 files changed

+112
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Added `ydb.WithApplicationName` option
12
* Added `Dict` support for `ydb.ParamsBuilder()`
23

34
## v3.57.3

config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,19 @@ func WithTraceRetry(t *trace.Retry, opts ...trace.RetryComposeOption) Option {
161161
}
162162
}
163163

164+
// WithApplicationName add provided application name to all api requests
165+
func WithApplicationName(applicationName string) Option {
166+
return func(c *Config) {
167+
c.metaOptions = append(c.metaOptions, meta.WithApplicationNameOption(applicationName))
168+
}
169+
}
170+
171+
// WithUserAgent add provided user agent to all api requests
172+
//
173+
// Deprecated: use WithApplicationName instead
164174
func WithUserAgent(userAgent string) Option {
165175
return func(c *Config) {
166-
c.metaOptions = append(c.metaOptions, meta.WithUserAgentOption(userAgent))
176+
c.metaOptions = append(c.metaOptions, meta.WithApplicationNameOption(userAgent))
167177
}
168178
}
169179

internal/meta/context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func traceID(ctx context.Context) (string, bool) {
2323
return "", false
2424
}
2525

26-
// WithUserAgent returns a copy of parent context with custom user-agent info
27-
func WithUserAgent(ctx context.Context, userAgent string) context.Context {
28-
return metadata.AppendToOutgoingContext(ctx, HeaderUserAgent, userAgent)
26+
// WithApplicationName returns a copy of parent context with custom user-agent info
27+
func WithApplicationName(ctx context.Context, applicationName string) context.Context {
28+
return metadata.AppendToOutgoingContext(ctx, HeaderApplicationName, applicationName)
2929
}
3030

3131
// WithRequestType returns a copy of parent context with custom request type
@@ -34,7 +34,7 @@ func WithRequestType(ctx context.Context, requestType string) context.Context {
3434
}
3535

3636
// WithAllowFeatures returns a copy of parent context with allowed client feature
37-
func WithAllowFeatures(ctx context.Context, features []string) context.Context {
37+
func WithAllowFeatures(ctx context.Context, features ...string) context.Context {
3838
kv := make([]string, 0, len(features)*2)
3939
for _, feature := range features {
4040
kv = append(kv, HeaderClientCapabilities, feature)

internal/meta/context_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package meta
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
"google.golang.org/grpc/metadata"
9+
)
10+
11+
func TestContext(t *testing.T) {
12+
for _, tt := range []struct {
13+
name string
14+
ctx context.Context
15+
header string
16+
values []string
17+
}{
18+
{
19+
name: "WithApplicationName",
20+
ctx: WithApplicationName(context.Background(), "test"),
21+
header: HeaderApplicationName,
22+
values: []string{"test"},
23+
},
24+
{
25+
name: "WithTraceID",
26+
ctx: WithTraceID(context.Background(), "my-trace-id"),
27+
header: HeaderTraceID,
28+
values: []string{"my-trace-id"},
29+
},
30+
{
31+
name: "WithRequestType",
32+
ctx: WithRequestType(context.Background(), "my-request-type"),
33+
header: HeaderRequestType,
34+
values: []string{"my-request-type"},
35+
},
36+
{
37+
name: "WithAllowFeatures",
38+
ctx: WithAllowFeatures(context.Background(), "feature-1", "feature-2", "feature-3"),
39+
header: HeaderClientCapabilities,
40+
values: []string{"feature-1", "feature-2", "feature-3"},
41+
},
42+
} {
43+
t.Run(tt.name, func(t *testing.T) {
44+
md, has := metadata.FromOutgoingContext(tt.ctx)
45+
require.True(t, has)
46+
require.Equal(t, tt.values, md.Get(tt.header))
47+
})
48+
}
49+
}

internal/meta/headers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const (
77
HeaderVersion = "x-ydb-sdk-build-info"
88
HeaderRequestType = "x-ydb-request-type"
99
HeaderTraceID = "x-ydb-trace-id"
10-
HeaderUserAgent = "x-ydb-user-agent"
10+
HeaderApplicationName = "x-ydb-application-name"
1111
HeaderClientCapabilities = "x-ydb-client-capabilities"
1212

1313
// outgoing hints

internal/meta/meta.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func New(
3535

3636
type Option func(m *Meta)
3737

38-
func WithUserAgentOption(userAgent string) Option {
38+
func WithApplicationNameOption(applicationName string) Option {
3939
return func(m *Meta) {
40-
m.userAgents = append(m.userAgents, userAgent)
40+
m.applicationName = applicationName
4141
}
4242
}
4343

@@ -67,12 +67,12 @@ func ForbidOption(feature string) Option {
6767
}
6868

6969
type Meta struct {
70-
trace *trace.Driver
71-
credentials credentials.Credentials
72-
database string
73-
requestsType string
74-
userAgents []string
75-
capabilities []string
70+
trace *trace.Driver
71+
credentials credentials.Credentials
72+
database string
73+
requestsType string
74+
applicationName string
75+
capabilities []string
7676
}
7777

7878
func (m *Meta) meta(ctx context.Context) (_ metadata.MD, err error) {
@@ -95,8 +95,8 @@ func (m *Meta) meta(ctx context.Context) (_ metadata.MD, err error) {
9595
}
9696
}
9797

98-
if len(m.userAgents) != 0 {
99-
md.Append(HeaderUserAgent, m.userAgents...)
98+
if m.applicationName != "" {
99+
md.Append(HeaderApplicationName, m.applicationName)
100100
}
101101

102102
if len(m.capabilities) > 0 {

internal/meta/test/meta_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ func TestMetaRequiredHeaders(t *testing.T) {
2020
credentials.NewAccessTokenCredentials("token"),
2121
&trace.Driver{},
2222
internal.WithRequestTypeOption("requestType"),
23-
internal.WithUserAgentOption("user-agent"),
23+
internal.WithApplicationNameOption("test app"),
2424
)
2525

2626
ctx := context.Background()
2727

28-
ctx = meta.WithUserAgent(ctx, "userAgent")
29-
3028
ctx = meta.WithTraceID(ctx, "traceID")
3129

3230
ctx = metadata.AppendToOutgoingContext(ctx, "some-user-header", "some-user-value")
@@ -43,7 +41,7 @@ func TestMetaRequiredHeaders(t *testing.T) {
4341
require.Equal(t, []string{"database"}, md.Get(internal.HeaderDatabase))
4442
require.Equal(t, []string{"requestType"}, md.Get(internal.HeaderRequestType))
4543
require.Equal(t, []string{"token"}, md.Get(internal.HeaderTicket))
46-
require.Equal(t, []string{"userAgent", "user-agent"}, md.Get(internal.HeaderUserAgent))
44+
require.Equal(t, []string{"test app"}, md.Get(internal.HeaderApplicationName))
4745
require.Equal(t, []string{"traceID"}, md.Get(internal.HeaderTraceID))
4846
require.Equal(t, []string{
4947
"ydb-go-sdk/" + version.Major + "." + version.Minor + "." + version.Patch,

meta/context.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ func WithTraceID(ctx context.Context, traceID string) context.Context {
1414
}
1515

1616
// WithUserAgent returns a copy of parent context with custom user-agent info
17-
func WithUserAgent(ctx context.Context, userAgent string) context.Context {
18-
return meta.WithUserAgent(ctx, userAgent)
17+
//
18+
// Deprecated: use WithApplicationName instead
19+
func WithUserAgent(ctx context.Context, _ string) context.Context {
20+
return ctx
21+
}
22+
23+
// WithApplicationName returns a copy of parent context with application name
24+
func WithApplicationName(ctx context.Context, applicationName string) context.Context {
25+
return meta.WithApplicationName(ctx, applicationName)
1926
}
2027

2128
// WithRequestType returns a copy of parent context with custom request type
@@ -25,7 +32,7 @@ func WithRequestType(ctx context.Context, requestType string) context.Context {
2532

2633
// WithAllowFeatures returns a copy of parent context with allowed client feature
2734
func WithAllowFeatures(ctx context.Context, features ...string) context.Context {
28-
return meta.WithAllowFeatures(ctx, features)
35+
return meta.WithAllowFeatures(ctx, features...)
2936
}
3037

3138
// WithTrailerCallback attaches callback to context for listening incoming metadata

options.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,21 @@ func WithAccessTokenCredentials(accessToken string) Option {
5454
)
5555
}
5656

57+
// WithApplicationName add provided application name to all api requests
58+
func WithApplicationName(applicationName string) Option {
59+
return func(ctx context.Context, c *Driver) error {
60+
c.options = append(c.options, config.WithApplicationName(applicationName))
61+
62+
return nil
63+
}
64+
}
65+
5766
// WithUserAgent add provided user agent value to all api requests
67+
//
68+
// Deprecated: use WithApplicationName instead
5869
func WithUserAgent(userAgent string) Option {
5970
return func(ctx context.Context, c *Driver) error {
60-
c.options = append(c.options, config.WithUserAgent(userAgent))
71+
c.options = append(c.options, config.WithApplicationName(userAgent))
6172

6273
return nil
6374
}

tests/integration/basic_example_native_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func TestBasicExampleNative(t *testing.T) { //nolint:gocyclo
232232
db, err := ydb.Open(ctx,
233233
os.Getenv("YDB_CONNECTION_STRING"),
234234
ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
235-
ydb.WithUserAgent("table/e2e"),
235+
ydb.WithApplicationName("table/e2e"),
236236
withMetrics(t, trace.DetailsAll, time.Second),
237237
ydb.With(
238238
config.WithOperationTimeout(time.Second*5),

0 commit comments

Comments
 (0)