|
15 | 15 | package badger |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "context" |
| 19 | + "log/slog" |
| 20 | + "os" |
| 21 | + |
18 | 22 | "go.opentelemetry.io/otel" |
19 | 23 | "go.opentelemetry.io/otel/attribute" |
| 24 | + "go.opentelemetry.io/otel/metric" |
20 | 25 | ) |
21 | 26 |
|
22 | 27 | const name = "github.com/transparency-dev/tessera/storage/posix/antispam" |
23 | 28 |
|
24 | 29 | var ( |
25 | 30 | tracer = otel.Tracer(name) |
| 31 | + meter = otel.Meter(name) |
26 | 32 | ) |
27 | 33 |
|
28 | 34 | var ( |
29 | 35 | followFromKey = attribute.Key("tessera.followFrom") |
30 | 36 | pushbackKey = attribute.Key("tessera.pushback") |
| 37 | + gcStatusKey = attribute.Key("status") |
| 38 | + hitKey = attribute.Key("hit") |
| 39 | +) |
| 40 | + |
| 41 | +var ( |
| 42 | + gcCounter metric.Int64Counter |
| 43 | + gcRunsPerTick metric.Int64Histogram |
| 44 | + gcDuration metric.Float64Histogram |
| 45 | + lookupCounter metric.Int64Counter |
| 46 | + lookupDuration metric.Float64Histogram |
| 47 | + followTxnEntriesCounter metric.Int64Histogram |
| 48 | + followTxnDuration metric.Float64Histogram |
| 49 | + |
| 50 | + // Histogram buckets for operations (latency in ms) |
| 51 | + histogramBuckets = []float64{0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000, 4000, 5000, 6000, 8000, 10000} |
| 52 | + |
| 53 | + // GC buckets for longer running operations. |
| 54 | + gcDurationBuckets = []float64{10, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 30000, 60000} |
31 | 55 | ) |
| 56 | + |
| 57 | +func generateBatchBuckets(max int) []float64 { |
| 58 | + bases := []float64{1, 2, 5} |
| 59 | + buckets := []float64{} |
| 60 | + for multiplier := 1.0; ; multiplier *= 10 { |
| 61 | + for _, b := range bases { |
| 62 | + v := b * multiplier |
| 63 | + if v >= float64(max) { |
| 64 | + buckets = append(buckets, float64(max)) |
| 65 | + return buckets |
| 66 | + } |
| 67 | + buckets = append(buckets, v) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func init() { |
| 73 | + var err error |
| 74 | + |
| 75 | + gcCounter, err = meter.Int64Counter( |
| 76 | + "tessera.antispam.badger.gc.count", |
| 77 | + metric.WithDescription("Number of BadgerDB Garbage Collection runs"), |
| 78 | + metric.WithUnit("{run}")) |
| 79 | + if err != nil { |
| 80 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.gc.count metric", slog.Any("error", err)) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + |
| 84 | + gcRunsPerTick, err = meter.Int64Histogram( |
| 85 | + "tessera.antispam.badger.gc.runs_per_tick", |
| 86 | + metric.WithDescription("Number of GC runs per tick"), |
| 87 | + metric.WithUnit("{run}"), |
| 88 | + metric.WithExplicitBucketBoundaries(generateBatchBuckets(500)...)) |
| 89 | + if err != nil { |
| 90 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.gc.runs_per_tick metric", slog.Any("error", err)) |
| 91 | + os.Exit(1) |
| 92 | + } |
| 93 | + |
| 94 | + gcDuration, err = meter.Float64Histogram( |
| 95 | + "tessera.antispam.badger.gc.duration", |
| 96 | + metric.WithDescription("Duration of BadgerDB Garbage Collection runs"), |
| 97 | + metric.WithUnit("ms"), |
| 98 | + metric.WithExplicitBucketBoundaries(gcDurationBuckets...)) |
| 99 | + if err != nil { |
| 100 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.gc.duration metric", slog.Any("error", err)) |
| 101 | + os.Exit(1) |
| 102 | + } |
| 103 | + |
| 104 | + lookupCounter, err = meter.Int64Counter( |
| 105 | + "tessera.antispam.badger.index.count", |
| 106 | + metric.WithDescription("Number of BadgerDB identity lookups"), |
| 107 | + metric.WithUnit("{lookup}")) |
| 108 | + if err != nil { |
| 109 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.index.count metric", slog.Any("error", err)) |
| 110 | + os.Exit(1) |
| 111 | + } |
| 112 | + |
| 113 | + lookupDuration, err = meter.Float64Histogram( |
| 114 | + "tessera.antispam.badger.index.duration", |
| 115 | + metric.WithDescription("Duration of BadgerDB identity lookups"), |
| 116 | + metric.WithUnit("ms"), |
| 117 | + metric.WithExplicitBucketBoundaries(histogramBuckets...)) |
| 118 | + if err != nil { |
| 119 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.index.duration metric", slog.Any("error", err)) |
| 120 | + os.Exit(1) |
| 121 | + } |
| 122 | + |
| 123 | + followTxnEntriesCounter, err = meter.Int64Histogram( |
| 124 | + "tessera.antispam.badger.follow_txn.entries.count", |
| 125 | + metric.WithDescription("Number of entries processed by BadgerDB antispam Follow transactions"), |
| 126 | + metric.WithUnit("{entry}"), |
| 127 | + metric.WithExplicitBucketBoundaries(generateBatchBuckets(int(DefaultMaxBatchSize))...)) |
| 128 | + if err != nil { |
| 129 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.follow_txn.entries.count metric", slog.Any("error", err)) |
| 130 | + os.Exit(1) |
| 131 | + } |
| 132 | + |
| 133 | + followTxnDuration, err = meter.Float64Histogram( |
| 134 | + "tessera.antispam.badger.FollowTxn.duration", |
| 135 | + metric.WithDescription("Duration of BadgerDB Follow transactions"), |
| 136 | + metric.WithUnit("ms"), |
| 137 | + metric.WithExplicitBucketBoundaries(histogramBuckets...)) |
| 138 | + if err != nil { |
| 139 | + slog.ErrorContext(context.Background(), "Failed to create tessera.antispam.badger.FollowTxn.duration metric", slog.Any("error", err)) |
| 140 | + os.Exit(1) |
| 141 | + } |
| 142 | +} |
0 commit comments