@@ -17,6 +17,7 @@ import (
1717 "github.com/grafana/dskit/multierror"
1818 dskittenant "github.com/grafana/dskit/tenant"
1919 "github.com/oklog/ulid/v2"
20+ "github.com/prometheus/client_golang/prometheus"
2021 "github.com/prometheus/prometheus/model/histogram"
2122 "github.com/prometheus/prometheus/model/labels"
2223 "github.com/prometheus/prometheus/storage"
@@ -35,10 +36,12 @@ import (
3536type TSDBBuilder struct {
3637 dataDir string
3738
38- logger log.Logger
39- limits * validation.Overrides
40- blocksStorageCfg mimir_tsdb.BlocksStorageConfig
41- metrics tsdbBuilderMetrics
39+ logger log.Logger
40+ limits * validation.Overrides
41+ blocksStorageCfg mimir_tsdb.BlocksStorageConfig
42+ tsdbBuilderMetrics tsdbBuilderMetrics
43+ tsdbMetrics * mimir_tsdb.TSDBMetrics
44+
4245 applyMaxGlobalSeriesPerUserBelow int // inclusive
4346
4447 partitionID int32
@@ -65,13 +68,23 @@ type tsdbTenant struct {
6568 tenantID string
6669}
6770
68- func NewTSDBBuilder (logger log.Logger , dataDir string , partitionID int32 , blocksStorageCfg mimir_tsdb.BlocksStorageConfig , limits * validation.Overrides , metrics tsdbBuilderMetrics , applyMaxGlobalSeriesPerUserBelow int ) * TSDBBuilder {
71+ func NewTSDBBuilder (
72+ logger log.Logger ,
73+ dataDir string ,
74+ partitionID int32 ,
75+ blocksStorageCfg mimir_tsdb.BlocksStorageConfig ,
76+ limits * validation.Overrides ,
77+ tsdbBuilderMetrics tsdbBuilderMetrics ,
78+ tsdbMetrics * mimir_tsdb.TSDBMetrics ,
79+ applyMaxGlobalSeriesPerUserBelow int ,
80+ ) * TSDBBuilder {
6981 return & TSDBBuilder {
7082 dataDir : dataDir ,
7183 logger : logger ,
7284 limits : limits ,
7385 blocksStorageCfg : blocksStorageCfg ,
74- metrics : metrics ,
86+ tsdbBuilderMetrics : tsdbBuilderMetrics ,
87+ tsdbMetrics : tsdbMetrics ,
7588 applyMaxGlobalSeriesPerUserBelow : applyMaxGlobalSeriesPerUserBelow ,
7689 partitionID : partitionID ,
7790 tsdbs : make (map [tsdbTenant ]* userTSDB ),
@@ -252,7 +265,7 @@ func (b *TSDBBuilder) PushToStorageAndReleaseRequest(ctx context.Context, req *m
252265
253266 if discardedSamples > 0 {
254267 partitionStr := fmt .Sprintf ("%d" , tenant .partitionID )
255- b .metrics .processSamplesDiscarded .WithLabelValues (partitionStr ).Add (float64 (discardedSamples ))
268+ b .tsdbBuilderMetrics .processSamplesDiscarded .WithLabelValues (partitionStr ).Add (float64 (discardedSamples ))
256269 }
257270
258271 return app .Commit ()
@@ -292,6 +305,8 @@ func (b *TSDBBuilder) getOrCreateTSDB(tenant tsdbTenant) (*userTSDB, error) {
292305}
293306
294307func (b * TSDBBuilder ) newTSDB (tenant tsdbTenant ) (* userTSDB , error ) {
308+ tsdbPromReg := prometheus .NewRegistry ()
309+
295310 udir := filepath .Join (b .dataDir , strconv .Itoa (int (tenant .partitionID )), tenant .tenantID )
296311 // Remove any previous TSDB dir. We don't need it.
297312 if err := os .RemoveAll (udir ); err != nil {
@@ -318,7 +333,7 @@ func (b *TSDBBuilder) newTSDB(tenant tsdbTenant) (*userTSDB, error) {
318333 udb .maxGlobalSeries = userLimit
319334 }
320335
321- db , err := tsdb .Open (udir , util_log .SlogFromGoKit (userLogger ), nil , & tsdb.Options {
336+ db , err := tsdb .Open (udir , util_log .SlogFromGoKit (userLogger ), tsdbPromReg , & tsdb.Options {
322337 RetentionDuration : 0 ,
323338 MinBlockDuration : 2 * time .Hour .Milliseconds (),
324339 MaxBlockDuration : 2 * time .Hour .Milliseconds (),
@@ -334,18 +349,20 @@ func (b *TSDBBuilder) newTSDB(tenant tsdbTenant) (*userTSDB, error) {
334349 OutOfOrderCapMax : int64 (b .blocksStorageCfg .TSDB .OutOfOrderCapacityMax ),
335350 SecondaryHashFunction : nil , // TODO(codesome): May needed when applying limits. Used to determine the owned series by an ingesters
336351 SeriesLifecycleCallback : udb ,
337- HeadPostingsForMatchersCacheMetrics : tsdb .NewPostingsForMatchersCacheMetrics (nil ),
338- BlockPostingsForMatchersCacheMetrics : tsdb .NewPostingsForMatchersCacheMetrics (nil ),
352+ HeadPostingsForMatchersCacheMetrics : tsdb .NewPostingsForMatchersCacheMetrics (nil ), // No need for these metrics; no one queries tsdb through block-builder
353+ BlockPostingsForMatchersCacheMetrics : tsdb .NewPostingsForMatchersCacheMetrics (nil ), // No need for these metrics; no one queries tsdb through block-builder
339354 PostingsClonerFactory : tsdb.DefaultPostingsClonerFactory {},
340355 }, nil )
341356 if err != nil {
342357 return nil , err
343358 }
344359
345- db .DisableCompactions ()
360+ db .DisableCompactions () // we compact on our own schedule
346361
347362 udb .DB = db
348363
364+ b .tsdbMetrics .SetRegistryForTenant (userID , tsdbPromReg )
365+
349366 return udb , nil
350367}
351368
@@ -377,6 +394,12 @@ func (b *TSDBBuilder) CompactAndUpload(ctx context.Context, uploadBlocks blockUp
377394 }
378395 merr .Add (os .RemoveAll (db .Dir ()))
379396 }
397+
398+ // Remove all registered per-tenant TSDB metrics. Their local DBs are wiped out from the block-builder no-matter what.
399+ for tenant := range b .tsdbs {
400+ b .tsdbMetrics .RemoveRegistryForTenant (tenant .tenantID )
401+ }
402+
380403 // Clear the map so that it can be released from the memory. Not setting to nil in case we want to reuse the TSDBBuilder.
381404 clear (b .tsdbs )
382405 b .tsdbsMu .Unlock ()
@@ -403,11 +426,11 @@ func (b *TSDBBuilder) CompactAndUpload(ctx context.Context, uploadBlocks blockUp
403426 }
404427 partitionStr := strconv .Itoa (int (tenant .partitionID ))
405428 if err != nil {
406- b .metrics .compactAndUploadFailed .WithLabelValues (partitionStr ).Inc ()
429+ b .tsdbBuilderMetrics .compactAndUploadFailed .WithLabelValues (partitionStr ).Inc ()
407430 return
408431 }
409- b .metrics .compactAndUploadDuration .WithLabelValues (partitionStr ).Observe (time .Since (t ).Seconds ())
410- b .metrics .lastSuccessfulCompactAndUploadTime .WithLabelValues (partitionStr ).SetToCurrentTime ()
432+ b .tsdbBuilderMetrics .compactAndUploadDuration .WithLabelValues (partitionStr ).Observe (time .Since (t ).Seconds ())
433+ b .tsdbBuilderMetrics .lastSuccessfulCompactAndUploadTime .WithLabelValues (partitionStr ).SetToCurrentTime ()
411434 }(time .Now ())
412435
413436 if err := db .compactEverything (ctx ); err != nil {
@@ -451,10 +474,12 @@ func (b *TSDBBuilder) Close() error {
451474 defer b .tsdbsMu .Unlock ()
452475
453476 var merr multierror.MultiError
454- for _ , db := range b .tsdbs {
477+ for tenant , db := range b .tsdbs {
455478 dbDir := db .Dir ()
456479 merr .Add (db .Close ())
457480 merr .Add (os .RemoveAll (dbDir ))
481+
482+ b .tsdbMetrics .RemoveRegistryForTenant (tenant .tenantID )
458483 }
459484
460485 // Clear the map so that it can be released from the memory. Not setting to nil in case
0 commit comments