-
Notifications
You must be signed in to change notification settings - Fork 208
refactor(go/adbc): refactor logging instrumentation into OTel tracing - part 1/3 #4655
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
Open
birschick-bq
wants to merge
9
commits into
apache:main
Choose a base branch
from
birschick-bq:dev/birschick-bq/flight-sql-log-to-trace-3-reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b4f536c
feat(go/adbc): extend tracing lifecycle helpers
v-birschick-msft 0214000
feat(go/adbc): add Flight SQL tracing utilities
v-birschick-msft 0398194
feat(go/adbc): trace Flight SQL record readers
v-birschick-msft 7974884
fix codespell finding
v-birschick-msft bc3dffb
remove unused methods (golangci findings)
v-birschick-msft 8f08d10
remove unused methods (golangci-lint finding)
v-birschick-msft e3edd42
empty - retest
v-birschick-msft 8446e81
improvements from code review comments
v-birschick-msft d602600
empty - retest
v-birschick-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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 flightsql | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/apache/arrow-go/v18/arrow/flight" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/metadata" | ||
| ) | ||
|
|
||
| type responseMetadataKey struct{} | ||
|
|
||
| type responseMetadataCollector struct { | ||
| mutex sync.RWMutex | ||
| value metadata.MD | ||
| } | ||
|
|
||
| func withResponseMetadata(ctx context.Context) (context.Context, *responseMetadataCollector) { | ||
| collector := &responseMetadataCollector{} | ||
| return context.WithValue(ctx, responseMetadataKey{}, collector), collector | ||
| } | ||
|
|
||
| func captureResponseMetadata(ctx context.Context, value metadata.MD) { | ||
| collector, ok := responseMetadataFromContext(ctx) | ||
| if !ok { | ||
| return | ||
| } | ||
| collector.mutex.Lock() | ||
| collector.value = value.Copy() | ||
| defer collector.mutex.Unlock() | ||
| } | ||
|
|
||
| func responseMetadataFromContext(ctx context.Context) (*responseMetadataCollector, bool) { | ||
| collector, ok := ctx.Value(responseMetadataKey{}).(*responseMetadataCollector) | ||
| return collector, ok | ||
| } | ||
|
|
||
| func (c *responseMetadataCollector) snapshot() metadata.MD { | ||
| c.mutex.RLock() | ||
| defer c.mutex.RUnlock() | ||
| return c.value.Copy() | ||
| } | ||
|
|
||
| func responseMetadataStreamInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
| stream, err := streamer(ctx, desc, cc, method, opts...) | ||
| if err != nil { | ||
| return stream, err | ||
| } | ||
| if _, ok := responseMetadataFromContext(ctx); !ok { | ||
| return stream, nil | ||
| } | ||
| return &responseMetadataClientStream{ClientStream: stream, ctx: ctx}, nil | ||
| } | ||
|
|
||
| type responseMetadataClientStream struct { | ||
| grpc.ClientStream | ||
| ctx context.Context | ||
| } | ||
|
|
||
| func (s *responseMetadataClientStream) RecvMsg(message interface{}) error { | ||
| err := s.ClientStream.RecvMsg(message) | ||
| if err != nil { | ||
| header, _ := s.Header() | ||
| captureResponseMetadata(s.ctx, metadata.Join(header, s.Trailer())) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // endpointTraceKeyValues builds OpenTelemetry attributes describing a Flight | ||
| // endpoint. Ticket contents are intentionally never recorded. | ||
| func endpointTraceKeyValues(endpointIndex, numEndpoints int, endpoint *flight.FlightEndpoint) []attribute.KeyValue { | ||
| attrs := []attribute.KeyValue{ | ||
| attribute.Int("endpointIndex", endpointIndex), | ||
| attribute.Int("numEndpoints", numEndpoints), | ||
| } | ||
| if endpoint == nil { | ||
| return attrs | ||
| } | ||
| if endpoint.Ticket != nil { | ||
| attrs = append(attrs, attribute.Int("ticketBytes", len(endpoint.Ticket.Ticket))) | ||
| } | ||
| if len(endpoint.Location) == 0 { | ||
| attrs = append(attrs, attribute.String("locations", "<empty: using default client connection>")) | ||
| } else { | ||
| uris := make([]string, 0, len(endpoint.Location)) | ||
| for _, loc := range endpoint.Location { | ||
| uris = append(uris, loc.Uri) | ||
| } | ||
| attrs = append(attrs, attribute.StringSlice("locations", uris)) | ||
| } | ||
| if endpoint.ExpirationTime != nil { | ||
| attrs = append(attrs, attribute.String("expirationTime", endpoint.ExpirationTime.AsTime().String())) | ||
| } | ||
| return attrs | ||
| } | ||
|
|
||
| // logKeyValues returns OpenTelemetry attributes summarizing stream progress. | ||
| func (p *streamProgress) logKeyValues() []attribute.KeyValue { | ||
| attrs := []attribute.KeyValue{ | ||
| attribute.Int64("batchesRead", p.batchesRead), | ||
| attribute.Int64("recordsRead", p.recordsRead), | ||
| attribute.Int64("approxBytesRead", p.bytesEstimate), | ||
| attribute.String("elapsed", time.Since(p.start).String()), | ||
| } | ||
| if !p.firstBatchAt.IsZero() { | ||
| attrs = append(attrs, attribute.String("timeToFirstBatch", p.firstBatchAt.Sub(p.start).String())) | ||
| } else { | ||
| attrs = append(attrs, attribute.String("timeToFirstBatch", "never")) | ||
| } | ||
| if !p.lastBatchAt.IsZero() { | ||
| attrs = append(attrs, attribute.String("timeSinceLastBatch", time.Since(p.lastBatchAt).String())) | ||
| } | ||
| return attrs | ||
| } | ||
|
|
||
| // flightInfoTracingKeyValues returns OpenTelemetry attributes describing a FlightInfo: | ||
| // descriptor type and command prefix, AppMetadata prefix (some backends | ||
| // embed a server-side query handle there), and advisory record/byte | ||
| // counts. Returns nil for a nil info. | ||
| func flightInfoTracingKeyValues(info *flight.FlightInfo) []attribute.KeyValue { | ||
| if info == nil { | ||
| return nil | ||
| } | ||
| attrs := []attribute.KeyValue{ | ||
| attribute.Int("numEndpoints", len(info.Endpoint)), | ||
| attribute.Int64("totalRecords", info.TotalRecords), | ||
| attribute.Int64("totalBytes", info.TotalBytes), | ||
| attribute.Bool("haveSchemaInFlightInfo", len(info.Schema) > 0), | ||
| } | ||
| if desc := info.FlightDescriptor; desc != nil { | ||
| attrs = append(attrs, attribute.String("descriptorType", desc.Type.String())) | ||
| if len(desc.Cmd) > 0 { | ||
| limit := len(desc.Cmd) | ||
| if limit > maxLoggedBlobBytes { | ||
| limit = maxLoggedBlobBytes | ||
| } | ||
| attrs = append(attrs, | ||
| attribute.Int("descriptorCmdBytes", len(desc.Cmd)), | ||
| attribute.String("descriptorCmdPrefixHex", hex.EncodeToString(desc.Cmd[:limit])), | ||
| ) | ||
| } | ||
| if len(desc.Path) > 0 { | ||
| attrs = append(attrs, attribute.String("descriptorPath", fmt.Sprint(desc.Path))) | ||
| } | ||
| } | ||
| if len(info.AppMetadata) > 0 { | ||
| limit := len(info.AppMetadata) | ||
| if limit > maxLoggedBlobBytes { | ||
| limit = maxLoggedBlobBytes | ||
| } | ||
| attrs = append(attrs, | ||
| attribute.Int("appMetadataBytes", len(info.AppMetadata)), | ||
| attribute.String("appMetadataPrefixHex", hex.EncodeToString(info.AppMetadata[:limit])), | ||
| ) | ||
| } | ||
| return attrs | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(1) Is this actually useful to record?
(2) While it doesn't apply to gRPC, some implementations may be putting things like presigned URLs into "locations" (I've seen this before)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a port of work that @davidhcoe did in
PR for
func endpointLogAttrs(endpointIndex, numEndpoints int, endpoint *flight.FlightEndpoint) []any {