Skip to content

Commit 0c79ad2

Browse files
authored
Merge pull request #1123 from ydb-platform/pid
* Added client pid to each gRPC requests to YDB over header `x-ydb-client-pid`
2 parents acc1205 + 91d7dfc commit 0c79ad2

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Added client pid to each gRPC requests to YDB over header `x-ydb-client-pid`
12
* Added `ydb.WithApplicationName` option
23
* Added `Dict` support for `ydb.ParamsBuilder()`
34

internal/meta/context.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ func traceID(ctx context.Context) (string, bool) {
2525

2626
// WithApplicationName returns a copy of parent context with custom user-agent info
2727
func WithApplicationName(ctx context.Context, applicationName string) context.Context {
28-
return metadata.AppendToOutgoingContext(ctx, HeaderApplicationName, applicationName)
28+
md, has := metadata.FromOutgoingContext(ctx)
29+
if !has {
30+
md = metadata.MD{}
31+
}
32+
md.Set(HeaderApplicationName, applicationName)
33+
34+
return metadata.NewOutgoingContext(ctx, md)
2935
}
3036

3137
// WithRequestType returns a copy of parent context with custom request type

internal/meta/context_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ func TestContext(t *testing.T) {
2121
header: HeaderApplicationName,
2222
values: []string{"test"},
2323
},
24+
{
25+
name: "WithApplicationName",
26+
ctx: WithApplicationName(
27+
WithApplicationName(
28+
context.Background(),
29+
"test1",
30+
),
31+
"test2",
32+
),
33+
header: HeaderApplicationName,
34+
values: []string{"test2"},
35+
},
2436
{
2537
name: "WithTraceID",
2638
ctx: WithTraceID(context.Background(), "my-trace-id"),

internal/meta/headers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
HeaderTraceID = "x-ydb-trace-id"
1010
HeaderApplicationName = "x-ydb-application-name"
1111
HeaderClientCapabilities = "x-ydb-client-capabilities"
12+
HeaderClientPid = "x-ydb-client-pid"
1213

1314
// outgoing hints
1415
HintSessionBalancer = "session-balancer"

internal/meta/meta.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package meta
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"strconv"
68

79
"google.golang.org/grpc/metadata"
810

@@ -20,6 +22,7 @@ func New(
2022
opts ...Option,
2123
) *Meta {
2224
m := &Meta{
25+
pid: strconv.Itoa(os.Getpid()),
2326
trace: trace,
2427
credentials: credentials,
2528
database: database,
@@ -67,6 +70,7 @@ func ForbidOption(feature string) Option {
6770
}
6871

6972
type Meta struct {
73+
pid string
7074
trace *trace.Driver
7175
credentials credentials.Credentials
7276
database string
@@ -81,6 +85,8 @@ func (m *Meta) meta(ctx context.Context) (_ metadata.MD, err error) {
8185
md = metadata.MD{}
8286
}
8387

88+
md.Set(HeaderClientPid, m.pid)
89+
8490
if len(md.Get(HeaderDatabase)) == 0 {
8591
md.Set(HeaderDatabase, m.database)
8692
}

internal/meta/test/meta_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func TestMetaRequiredHeaders(t *testing.T) {
4141
require.Equal(t, []string{"database"}, md.Get(internal.HeaderDatabase))
4242
require.Equal(t, []string{"requestType"}, md.Get(internal.HeaderRequestType))
4343
require.Equal(t, []string{"token"}, md.Get(internal.HeaderTicket))
44+
require.NotEmpty(t, md.Get(internal.HeaderClientPid))
45+
require.NotEmpty(t, md.Get(internal.HeaderClientPid)[0])
4446
require.Equal(t, []string{"test app"}, md.Get(internal.HeaderApplicationName))
4547
require.Equal(t, []string{"traceID"}, md.Get(internal.HeaderTraceID))
4648
require.Equal(t, []string{

0 commit comments

Comments
 (0)