birschick-bq commented on code in PR #4655: URL: https://github.com/apache/arrow-adbc/pull/4655#discussion_r3761421199
########## go/adbc/driver/flightsql/flightsql_tracing.go: ########## @@ -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() + 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)) Review Comment: This is a port of work that @davidhcoe did in [PR](https://github.com/apache/arrow-adbc/pull/4322) [func endpointLogAttrs(endpointIndex, numEndpoints int, endpoint *flight.FlightEndpoint) []any \{](https://github.com/apache/arrow-adbc/blame/12dc472570f38bb48b10fce99e796a8e45b65864/go/adbc/driver/flightsql/logging.go#L57) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
