diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 8ee4b3a0..859242f8 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -6,9 +6,5 @@ on: # Common Go workflows from go faster # See https://github.com/go-faster/x jobs: - test: - uses: go-faster/x/.github/workflows/main.yml@main - lint: - uses: go-faster/x/.github/workflows/lint.yml@main commit: uses: go-faster/x/.github/workflows/commit.yml@main diff --git a/.github/workflows/x.yml b/.github/workflows/x.yml index 1a499537..c684dec8 100644 --- a/.github/workflows/x.yml +++ b/.github/workflows/x.yml @@ -3,6 +3,7 @@ name: x on: push: branches: [main] + pull_request: # Common Go workflows from go faster # See https://github.com/go-faster/x diff --git a/query.go b/query.go index ed53f541..f8aabbe2 100644 --- a/query.go +++ b/query.go @@ -191,6 +191,9 @@ type Query struct { ExternalData []proto.InputColumn // ExternalTable name. Defaults to _data. ExternalTable string + + // Logger for query, optional, defaults to client logger with `query_id` field. + Logger *zap.Logger } // CorruptedDataErr means that provided hash mismatch with calculated. @@ -578,6 +581,30 @@ func (c *Client) Do(ctx context.Context, q Query) (err error) { if q.QueryID == "" { q.QueryID = uuid.New().String() } + { + // Setup query logger. + // + // Since Do is not goroutine-safe, we can safely reuse client logger, + // so next calls will utilize changed c.lg. + lg := c.lg + defer func(v *zap.Logger) { + // Set logger back after query is done. + c.lg = v + }(lg) + if q.Logger != nil { + // Using provided query logger. + lg = q.Logger + } else { + // Using client logger. + // Allow correlation of queries by query_id. + lg = lg.With( + zap.String("query_id", q.QueryID), + ) + } + // Set current logger to query-scoped. + // This will be used by all function calls until query is done. + c.lg = lg + } if c.otel { newCtx, span := c.tracer.Start(ctx, "Do", trace.WithSpanKind(trace.SpanKindClient),