Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(bigquery/storage/managedwriter): add client instrumentation #6480

Merged
merged 6 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions bigquery/storage/apiv1/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package storage

// SetGoogleClientInfo sets the name and version of the application in
// the `x-goog-api-client` header passed on each request. Also passes any
// provided key-value pairs. Intended for use by Google-written clients.
//
// Internal use only.
func (c *BigQueryWriteClient) SetGoogleClientInfo(keyval ...string) {
c.setGoogleClientInfo(keyval...)
}

// SetGoogleClientInfo sets the name and version of the application in
// the `x-goog-api-client` header passed on each request. Also passes any
// provided key-value pairs. Intended for use by Google-written clients.
//
// Internal use only.
func (c *BigQueryReadClient) SetGoogleClientInfo(keyval ...string) {
c.setGoogleClientInfo(keyval...)
}
2 changes: 2 additions & 0 deletions bigquery/storage/managedwriter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"runtime"
"strings"

"cloud.google.com/go/bigquery/internal"
storage "cloud.google.com/go/bigquery/storage/apiv1"
"cloud.google.com/go/internal/detect"
"github.com/googleapis/gax-go/v2"
Expand Down Expand Up @@ -59,6 +60,7 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
if err != nil {
return nil, err
}
rawClient.SetGoogleClientInfo("gccl", internal.Version)

// Handle project autodetection.
projectID, err = detect.ProjectID(ctx, projectID, "", opts...)
Expand Down
11 changes: 10 additions & 1 deletion bigquery/storage/managedwriter/managed_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"sync"

"cloud.google.com/go/bigquery/internal"
"github.com/googleapis/gax-go/v2"
"go.opencensus.io/tag"
storagepb "google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1"
Expand Down Expand Up @@ -126,10 +127,18 @@ func defaultStreamSettings() *streamSettings {
streamType: DefaultStream,
MaxInflightRequests: 1000,
MaxInflightBytes: 0,
TraceID: "",
TraceID: buildTraceID(""),
}
}

func buildTraceID(id string) string {
base := fmt.Sprintf("go-managedwriter:%s", internal.Version)
if id != "" {
return fmt.Sprintf("%s %s", base, id)
}
return base
}

// StreamName returns the corresponding write stream ID being managed by this writer.
func (ms *ManagedStream) StreamName() string {
return ms.streamSettings.streamID
Expand Down
2 changes: 1 addition & 1 deletion bigquery/storage/managedwriter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func WithMaxInflightBytes(n int) WriterOption {
// This is generally for diagnostic purposes only.
func WithTraceID(traceID string) WriterOption {
return func(ms *ManagedStream) {
ms.streamSettings.TraceID = traceID
ms.streamSettings.TraceID = buildTraceID(traceID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to allow users to add in their own trace id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes from just setting the traceID in total to building a prefix for the library, and the user-specific trace ID. Same as jsonwriter etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take a look at options_test.go for the expected output

}
}

Expand Down
19 changes: 16 additions & 3 deletions bigquery/storage/managedwriter/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package managedwriter

import (
"fmt"
"sync"
"testing"

"cloud.google.com/go/bigquery/internal"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gax-go/v2"
"google.golang.org/grpc"
Expand Down Expand Up @@ -64,13 +66,24 @@ func TestWriterOptions(t *testing.T) {
}(),
},
{
desc: "WithTracePrefix",
desc: "WithTraceID",
options: []WriterOption{WithTraceID("foo")},
want: func() *ManagedStream {
ms := &ManagedStream{
streamSettings: defaultStreamSettings(),
}
ms.streamSettings.TraceID = "foo"
ms.streamSettings.TraceID = fmt.Sprintf("go-managedwriter:%s %s", internal.Version, "foo")
return ms
}(),
},
{
desc: "WithoutTraceID",
options: []WriterOption{},
want: func() *ManagedStream {
ms := &ManagedStream{
streamSettings: defaultStreamSettings(),
}
ms.streamSettings.TraceID = fmt.Sprintf("go-managedwriter:%s", internal.Version)
return ms
}(),
},
Expand Down Expand Up @@ -122,7 +135,7 @@ func TestWriterOptions(t *testing.T) {
}
ms.streamSettings.MaxInflightBytes = 5
ms.streamSettings.streamType = PendingStream
ms.streamSettings.TraceID = "id"
ms.streamSettings.TraceID = fmt.Sprintf("go-managedwriter:%s %s", internal.Version, "id")
return ms
}(),
},
Expand Down