Repository: milvus-io/milvus-sdk-go
Pull request #306 (2022-08-23 03:04:18)

add client tracing interfaces
add interfaces to pass tracing variables:
- log level
- client trace id
ISSUE: #307 

sre-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/306#" title="Author self-approved">Zach41</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **congqixia** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @congqixia` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["congqixia"]} -->
mergify[bot]: @Zach41 Please associate the related issue to the body of your Pull Request.  (eg. “issue: #187”)
codecov[bot]: # [Codecov](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/306?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) Report
> Merging [#306](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/306?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (515bf69) into [master](https://codecov.io/gh/milvus-io/milvus-sdk-go/commit/9feb383bd3ab615096c5368160c429bf6eb31fff?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (9feb383) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/306/graphs/tree.svg?width=650&height=150&src=pr&token=WJK43PV390&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)

```diff
@@            Coverage Diff             @@
##           master     #306      +/-   ##
==========================================
+ Coverage   90.02%   90.04%   +0.01%     
==========================================
  Files          24       25       +1     
  Lines        2676     2681       +5     
==========================================
+ Hits         2409     2414       +5     
  Misses        182      182              
  Partials       85       85              
```


| [Impacted Files](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) | Coverage Δ | |
|---|---|---|
| [client/ctx.go](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/306/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-Y2xpZW50L2N0eC5nbw==) | `100.00% <100.00%> (ø)` | |

congqixia: /kind enhancement
congqixia: /lgtm
/approve
sre-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/306#issuecomment-1223486551" title="Approved">congqixia</a>*, *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/306#" title="Author self-approved">Zach41</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/master/OWNERS)~~ [congqixia]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/client/ctx.go b/client/ctx.go
new file mode 100644
index 0000000..c00c53b
--- /dev/null
+++ b/client/ctx.go
@@ -0,0 +1,37 @@
+package client
+
+import (
+	"context"
+
+	"google.golang.org/grpc/metadata"
+)
+
+const (
+	logLevelRPCMetaKey = "log_level"
+	clientRequestIDKey = "client_request_id"
+
+	debugLevel = "debug"
+	infoLevel  = "info"
+	warnLevel  = "warn"
+	errorLevel = "error"
+)
+
+func WithDebugLogLevel(ctx context.Context) context.Context {
+	return metadata.AppendToOutgoingContext(ctx, logLevelRPCMetaKey, debugLevel)
+}
+
+func WithInfoLogLevel(ctx context.Context) context.Context {
+	return metadata.AppendToOutgoingContext(ctx, logLevelRPCMetaKey, infoLevel)
+}
+
+func WithWarnLogLevel(ctx context.Context) context.Context {
+	return metadata.AppendToOutgoingContext(ctx, logLevelRPCMetaKey, warnLevel)
+}
+
+func WithErrorLogLevel(ctx context.Context) context.Context {
+	return metadata.AppendToOutgoingContext(ctx, logLevelRPCMetaKey, errorLevel)
+}
+
+func WithClientRequestID(ctx context.Context, reqID string) context.Context {
+	return metadata.AppendToOutgoingContext(ctx, clientRequestIDKey, reqID)
+}


Test patch:
diff --git a/client/ctx_test.go b/client/ctx_test.go
new file mode 100644
index 0000000..269735e
--- /dev/null
+++ b/client/ctx_test.go
@@ -0,0 +1,46 @@
+package client
+
+import (
+	"context"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc/metadata"
+)
+
+func TestOutgoingCtx(t *testing.T) {
+	t.Run("debug log level", func(t *testing.T) {
+		ctx := WithDebugLogLevel(context.TODO())
+		md, ok := metadata.FromOutgoingContext(ctx)
+		assert.True(t, ok)
+		assert.Equal(t, []string{"debug"}, md.Get(logLevelRPCMetaKey))
+	})
+
+	t.Run("info log level", func(t *testing.T) {
+		ctx := WithInfoLogLevel(context.TODO())
+		md, ok := metadata.FromOutgoingContext(ctx)
+		assert.True(t, ok)
+		assert.Equal(t, []string{"info"}, md.Get(logLevelRPCMetaKey))
+	})
+
+	t.Run("warn log level", func(t *testing.T) {
+		ctx := WithWarnLogLevel(context.TODO())
+		md, ok := metadata.FromOutgoingContext(ctx)
+		assert.True(t, ok)
+		assert.Equal(t, []string{"warn"}, md.Get(logLevelRPCMetaKey))
+	})
+
+	t.Run("error log level", func(t *testing.T) {
+		ctx := WithErrorLogLevel(context.TODO())
+		md, ok := metadata.FromOutgoingContext(ctx)
+		assert.True(t, ok)
+		assert.Equal(t, []string{"error"}, md.Get(logLevelRPCMetaKey))
+	})
+
+	t.Run("client request id", func(t *testing.T) {
+		ctx := WithClientRequestID(context.TODO(), "test-trace")
+		md, ok := metadata.FromOutgoingContext(ctx)
+		assert.True(t, ok)
+		assert.Equal(t, []string{"test-trace"}, md.Get(clientRequestIDKey))
+	})
+}