|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package internal |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "log" |
| 20 | + "time" |
| 21 | + |
| 22 | + btopt "cloud.google.com/go/bigtable/internal/option" |
| 23 | + "go.opentelemetry.io/otel/attribute" |
| 24 | + "go.opentelemetry.io/otel/metric" |
| 25 | +) |
| 26 | + |
| 27 | +// Pacemaker monitors the runtime scheduling delay |
| 28 | +// It measures the time difference between when a ticker was scheduled to fire |
| 29 | +// and when the ticker actually fires. |
| 30 | +type Pacemaker struct { |
| 31 | + meterProvider metric.MeterProvider |
| 32 | + logger *log.Logger |
| 33 | + histogram metric.Float64Histogram |
| 34 | + attrs metric.MeasurementOption |
| 35 | +} |
| 36 | + |
| 37 | +// NewPacemaker creates a new Pacemaker and initializes its metrics. |
| 38 | +func NewPacemaker(mp metric.MeterProvider, logger *log.Logger) *Pacemaker { |
| 39 | + pm := &Pacemaker{ |
| 40 | + meterProvider: mp, |
| 41 | + logger: logger, |
| 42 | + attrs: metric.WithAttributes(attribute.String("executor", "goroutine")), |
| 43 | + } |
| 44 | + |
| 45 | + if mp == nil { |
| 46 | + return pm |
| 47 | + } |
| 48 | + |
| 49 | + // create meter |
| 50 | + meter := mp.Meter(clientMeterName) |
| 51 | + var err error |
| 52 | + // Buckets in microseconds (us). |
| 53 | + // Ranges cover: 0us, 100us, 500us, 1ms(1k), 2ms(2k), 5ms(5k), 10ms(10k), |
| 54 | + // 50ms(50k), 100ms(100k), 500ms(500k), 1s(1M). |
| 55 | + bounds := []float64{0, 100, 500, 1000, 2000, 5000, 10000, 50000, 100000, 500000, 1000000} |
| 56 | + |
| 57 | + pm.histogram, err = meter.Float64Histogram( |
| 58 | + "pacemaker_delays", |
| 59 | + metric.WithDescription("Distribution of delays between the scheduled time and actual execution time of the pacemaker goroutine."), |
| 60 | + metric.WithUnit("us"), |
| 61 | + metric.WithExplicitBucketBoundaries(bounds...), |
| 62 | + ) |
| 63 | + if err != nil { |
| 64 | + btopt.Debugf(logger, "bigtable_connpool: failed to create pacemaker metric: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + return pm |
| 68 | +} |
| 69 | + |
| 70 | +// Start begins the pacemaker ticker. |
| 71 | +func (p *Pacemaker) Start(ctx context.Context) { |
| 72 | + if p.histogram == nil { |
| 73 | + btopt.Debugf(p.logger, "bigtable_connpool: Pacemaker skipped (no histogram initialized)") |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + go func() { |
| 78 | + interval := 100 * time.Millisecond |
| 79 | + ticker := time.NewTicker(interval) |
| 80 | + defer ticker.Stop() |
| 81 | + |
| 82 | + lastTick := time.Now() |
| 83 | + |
| 84 | + for { |
| 85 | + select { |
| 86 | + case t := <-ticker.C: |
| 87 | + actualInterval := t.Sub(lastTick) |
| 88 | + |
| 89 | + delay := actualInterval - interval |
| 90 | + if delay < 0 { |
| 91 | + delay = 0 |
| 92 | + } |
| 93 | + |
| 94 | + delayUs := float64(delay.Nanoseconds()) / 1e3 |
| 95 | + p.histogram.Record(ctx, delayUs, p.attrs) |
| 96 | + |
| 97 | + lastTick = t |
| 98 | + |
| 99 | + case <-ctx.Done(): |
| 100 | + return |
| 101 | + } |
| 102 | + } |
| 103 | + }() |
| 104 | +} |
| 105 | + |
| 106 | +// Stop acts as a cleanup method. no-op |
| 107 | +func (p *Pacemaker) Stop() { |
| 108 | +} |
0 commit comments