zeroshade commented on code in PR #2729: URL: https://github.com/apache/arrow-adbc/pull/2729#discussion_r2076166470
########## go/adbc/driver/internal/driverbase/connection.go: ########## @@ -714,4 +768,52 @@ func ValueOrZero[T any](val *T) T { return *val } +func maybeAddTraceParent(ctx context.Context, cnxn adbc.OTelTracing, st adbc.OTelTracing) (context.Context, error) { + var traceParentStr string + if st != nil && st.GetTraceParent() != "" { + traceParentStr = st.GetTraceParent() + } else if cnxn != nil && cnxn.GetTraceParent() != "" { + traceParentStr = cnxn.GetTraceParent() + } + if traceParentStr != "" { + spanContext, err := propagateTraceParent(ctx, traceParentStr) + if err != nil { + return ctx, err + } + ctx = trace.ContextWithRemoteSpanContext(ctx, spanContext) + } + return ctx, nil +} + +func propagateTraceParent(ctx context.Context, traceParentStr string) (trace.SpanContext, error) { + if strings.TrimSpace(traceParentStr) == "" { + return trace.SpanContext{}, fmt.Errorf("traceparent string is empty") Review Comment: use `errors.New` since you're not doing utilizing the format part of this ########## go/adbc/driver/internal/driverbase/connection.go: ########## @@ -714,4 +768,52 @@ func ValueOrZero[T any](val *T) T { return *val } +func maybeAddTraceParent(ctx context.Context, cnxn adbc.OTelTracing, st adbc.OTelTracing) (context.Context, error) { + var traceParentStr string + if st != nil && st.GetTraceParent() != "" { + traceParentStr = st.GetTraceParent() + } else if cnxn != nil && cnxn.GetTraceParent() != "" { + traceParentStr = cnxn.GetTraceParent() + } + if traceParentStr != "" { + spanContext, err := propagateTraceParent(ctx, traceParentStr) + if err != nil { + return ctx, err + } + ctx = trace.ContextWithRemoteSpanContext(ctx, spanContext) + } + return ctx, nil +} + +func propagateTraceParent(ctx context.Context, traceParentStr string) (trace.SpanContext, error) { + if strings.TrimSpace(traceParentStr) == "" { + return trace.SpanContext{}, fmt.Errorf("traceparent string is empty") + } + + propagator := propagation.TraceContext{} + carrier := propagation.MapCarrier{"traceparent": traceParentStr} + extractedContext := propagator.Extract(ctx, carrier) + + spanContext := trace.SpanContextFromContext(extractedContext) + if !spanContext.IsValid() { + return trace.SpanContext{}, fmt.Errorf("invalid traceparent string") + } + return spanContext, nil +} + +func isValidateTraceParent(traceParent string) bool { + // Supports version-format 00 + // see: https://www.w3.org/TR/trace-context/#trace-context-http-headers-format + const tpPattern = `^(?<version>[0]{2})-(?<traceId>[0-9a-f]{32})-(?<parentId>[0-9a-f]{16})-(?<traceFlags>[0-9a-f]{2})$` + tpRegExp := regexp.MustCompile(tpPattern) Review Comment: can we push this to `init` or otherwise so that we don't do the regexp Compile on every call? ########## go/adbc/driver/internal/driverbase/connection.go: ########## @@ -332,6 +355,37 @@ func (b *ConnectionBuilder) Connection() Connection { return conn } +func (cnxn *ConnectionImplBase) GetTraceParent() string { + return cnxn.traceParent +} + +func (cnxn *ConnectionImplBase) SetTraceParent(traceParent string) { + cnxn.traceParent = traceParent +} + +func (cnxn *ConnectionImplBase) StartSpan( + ctx context.Context, + spanName string, + opts ...trace.SpanStartOption, +) (context.Context, trace.Span) { + var span trace.Span + ctx, _ = maybeAddTraceParent(ctx, cnxn, nil) + ctx, span = cnxn.Tracer.Start(ctx, spanName, opts...) + return ctx, span +} + +func (cnxn *ConnectionImplBase) SetSpanOnError(span trace.Span, err error) bool { Review Comment: would a more accurate name be `SetErrorOnSpan`? ########## go/adbc/driver/internal/driverbase/connection.go: ########## @@ -332,6 +355,37 @@ func (b *ConnectionBuilder) Connection() Connection { return conn } +func (cnxn *ConnectionImplBase) GetTraceParent() string { + return cnxn.traceParent +} + +func (cnxn *ConnectionImplBase) SetTraceParent(traceParent string) { + cnxn.traceParent = traceParent +} + +func (cnxn *ConnectionImplBase) StartSpan( + ctx context.Context, + spanName string, + opts ...trace.SpanStartOption, +) (context.Context, trace.Span) { + var span trace.Span + ctx, _ = maybeAddTraceParent(ctx, cnxn, nil) + ctx, span = cnxn.Tracer.Start(ctx, spanName, opts...) + return ctx, span Review Comment: `return cnxn.Tracer.Start(ctx, spanName, opts...)` ########## go/adbc/driver/internal/driverbase/connection.go: ########## @@ -714,4 +768,52 @@ func ValueOrZero[T any](val *T) T { return *val } +func maybeAddTraceParent(ctx context.Context, cnxn adbc.OTelTracing, st adbc.OTelTracing) (context.Context, error) { + var traceParentStr string + if st != nil && st.GetTraceParent() != "" { + traceParentStr = st.GetTraceParent() + } else if cnxn != nil && cnxn.GetTraceParent() != "" { + traceParentStr = cnxn.GetTraceParent() + } + if traceParentStr != "" { + spanContext, err := propagateTraceParent(ctx, traceParentStr) + if err != nil { + return ctx, err + } + ctx = trace.ContextWithRemoteSpanContext(ctx, spanContext) + } + return ctx, nil +} + +func propagateTraceParent(ctx context.Context, traceParentStr string) (trace.SpanContext, error) { + if strings.TrimSpace(traceParentStr) == "" { + return trace.SpanContext{}, fmt.Errorf("traceparent string is empty") + } + + propagator := propagation.TraceContext{} + carrier := propagation.MapCarrier{"traceparent": traceParentStr} + extractedContext := propagator.Extract(ctx, carrier) + + spanContext := trace.SpanContextFromContext(extractedContext) + if !spanContext.IsValid() { + return trace.SpanContext{}, fmt.Errorf("invalid traceparent string") Review Comment: same as above, use `errors.New` ########## go/adbc/driver/internal/driverbase/rotating_file_writer.go: ########## @@ -0,0 +1,328 @@ +// 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 driverbase + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "strings" + "time" +) + +const ( + defaultLogNamePrefix = "apache.adbc.go" + defaultFileSizeMaxKb = int64(1024) + defaultFileCountMax = 100 + defaultTraceFileExt = ".jsonl" +) + +// Holds the configuration from the options +type config struct { + TracingFolderPath string + LogNamePrefix string + FileSizeMaxKb int64 + FileCountMax int +} + +// An option for the RotatingFileWriter +type rotatingFileWriterOption func(*config) + +// Adds the TracingFolderPath option +func WithTracingFolderPath(tracingFolderPath string) rotatingFileWriterOption { + return func(cfg *config) { + cfg.TracingFolderPath = tracingFolderPath + } +} + +// Adds the LogNamePrefix option +func WithLogNamePrefix(logNamePrefix string) rotatingFileWriterOption { + return func(cfg *config) { + cfg.LogNamePrefix = logNamePrefix + } +} + +// Adds the FileSizeMaxKb option +func WithFileSizeMaxKb(fileSizeMaxKb int64) rotatingFileWriterOption { + return func(cfg *config) { + cfg.FileSizeMaxKb = fileSizeMaxKb + } +} + +// Adds the FileCountMax option +func WithFileCountMax(fileCountMax int) rotatingFileWriterOption { + return func(cfg *config) { + cfg.FileCountMax = fileCountMax + } +} + +func newConfig(options ...rotatingFileWriterOption) (cfg config, err error) { + cfg = config{ + TracingFolderPath: "", + LogNamePrefix: defaultLogNamePrefix, + FileSizeMaxKb: defaultFileSizeMaxKb, + FileCountMax: defaultFileCountMax, + } + for _, opt := range options { + opt(&cfg) + } + // Ensure default for tracingFolderPath + if strings.TrimSpace(cfg.TracingFolderPath) == "" { + var fullPath string + fullPath, err = getDefaultTracingFolderPath() + if err != nil { + return + } + cfg.TracingFolderPath = fullPath Review Comment: just do `cfg.TracingFolderPath, err = getDefaultTracingFolderPath()` and get rid of the `fullPath` variable. -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org