Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit efdf869

Browse files
committed
trace request logging
1 parent 80eec93 commit efdf869

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

pkg/log/log.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func Init(cfg Config) error {
9595
// NOTE: we add a level of indirection with our logging functions,
9696
// so we need additional caller depth
9797
logger = log.With(l, "ts", timestampFormat, "caller", log.Caller(4))
98+
traceRequestEnabled = isTraceRequestEnabled()
9899
return nil
99100
}
100101

@@ -205,3 +206,28 @@ func WarnRateLimited(keyvals ...interface{}) {
205206
func DebugRateLimited(keyvals ...interface{}) {
206207
rateLimit(debug, keyvals...)
207208
}
209+
210+
var traceRequestEnabled bool
211+
212+
func isTraceRequestEnabled() bool {
213+
value := os.Getenv("PROMSCALE_TRACE_REQUEST")
214+
if value == "" {
215+
return false
216+
}
217+
enabled, err := strconv.ParseBool(value)
218+
if err != nil || !enabled {
219+
//assume off
220+
return false
221+
}
222+
return true
223+
}
224+
225+
func TraceRequestEnabled() bool {
226+
return traceRequestEnabled
227+
}
228+
229+
func TraceRequest(keyvals ...interface{}) {
230+
if TraceRequestEnabled() {
231+
Debug(keyvals...)
232+
}
233+
}

pkg/pgmodel/ingestor/dispatcher.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/timescale/promscale/pkg/pgmodel/metrics"
2222
"github.com/timescale/promscale/pkg/pgmodel/model"
2323
"github.com/timescale/promscale/pkg/pgxconn"
24+
"github.com/timescale/promscale/pkg/psctx"
2425
"github.com/timescale/promscale/pkg/tracer"
2526
tput "github.com/timescale/promscale/pkg/util/throughput"
2627
)
@@ -245,6 +246,16 @@ func (p *pgxDispatcher) InsertTs(ctx context.Context, dataTS model.Data) (uint64
245246
}
246247
p.getMetricBatcher(metricName) <- &insertDataRequest{requestCtx: ctx, spanCtx: span.SpanContext(), metric: metricName, data: data, finished: workFinished, batched: batched, errChan: errChan}
247248
}
249+
250+
var startTime time.Time
251+
if log.TraceRequestEnabled() {
252+
t, err := psctx.StartTime(ctx)
253+
if err != nil {
254+
log.TraceRequest("component", "dispatcher", "err", err)
255+
}
256+
startTime = t
257+
log.TraceRequest("component", "dispatcher", "event", "start", "metrics", len(rows), "samples", numRows, "start_time", startTime.UnixNano())
258+
}
248259
span.SetAttributes(attribute.Int64("num_rows", int64(numRows)))
249260
span.SetAttributes(attribute.Int("num_metrics", len(rows)))
250261
reportIncomingBatch(numRows)
@@ -261,6 +272,7 @@ func (p *pgxDispatcher) InsertTs(ctx context.Context, dataTS model.Data) (uint64
261272
case err = <-errChan:
262273
default:
263274
}
275+
log.TraceRequest("component", "dispatcher", "event", "ack", "start_time", startTime.UnixNano(), "took", time.Since(startTime))
264276
reportMetricsTelemetry(maxt, numRows, 0)
265277
close(errChan)
266278
} else {
@@ -275,6 +287,7 @@ func (p *pgxDispatcher) InsertTs(ctx context.Context, dataTS model.Data) (uint64
275287
if err != nil {
276288
log.Error("msg", fmt.Sprintf("error on async send, dropping %d datapoints", numRows), "err", err)
277289
}
290+
log.TraceRequest("component", "dispatcher", "event", "async_ack", "start_time", startTime.UnixNano(), "took", time.Since(startTime))
278291
reportMetricsTelemetry(maxt, numRows, 0)
279292
}()
280293
}

pkg/pgmodel/ingestor/reservation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"sync"
66
"sync/atomic"
77
"time"
8+
9+
"github.com/timescale/promscale/pkg/log"
810
)
911

1012
type reservation struct {
@@ -163,6 +165,7 @@ func (rq *ReservationQueue) Peek() (time.Time, bool) {
163165
case <-waitch:
164166
case <-time.After(250 * time.Millisecond):
165167
}
168+
log.TraceRequest("component", "reservation", "event", "peek", "batched_metrics", rq.q.Len(), "waited", waited, "took", time.Since((*rq.q)[0].GetStartTime()))
166169
}
167170
return reservation.GetStartTime(), ok
168171
}
@@ -213,6 +216,7 @@ func (rq *ReservationQueue) PopOntoBatch(batch []readRequest) ([]readRequest, in
213216
} else if !(len(batch) == 0 || items+total_items < 20000) {
214217
reason = "size_samples"
215218
}
219+
log.TraceRequest("component", "reservation", "event", "pop", "reason", reason, "metrics", count, "items", total_items, "remaining_metrics", rq.q.Len())
216220
return batch, count, reason
217221
}
218222

0 commit comments

Comments
 (0)