Class: Prometheus::Client::DataStores::DirectFileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus/client/data_stores/direct_file_store.rb

Overview

Stores data in binary files, one file per process and per metric. This is generally the recommended store to use to deal with pre-fork servers and other “multi-process” scenarios.

Each process will get a file for a metric, and it will manage its contents by storing keys next to binary-encoded Floats, and keeping track of the offsets of those Floats, to be able to update them directly as they increase.

When exporting metrics, the process that gets scraped by Prometheus will find all the files that apply to a metric, read their contents, and aggregate them (generally that means SUMming the values for each labelset).

In order to do this, each Metric needs an ‘:aggregation` setting, specifying how to aggregate the multiple possible values we can get for each labelset. By default, Counters, Histograms and Summaries get `SUM`med, and Gauges will report `ALL` values, tagging each one with a `pid` label. For Gauges, it’s also possible to set ‘SUM`, MAX` or `MIN` as aggregation, to get the highest / lowest value / or the sum of all the processes / threads.

Before using this Store, please read the “‘DirectFileStore` caveats and things to keep in mind” section of the main README in this repository. It includes a number of important things to keep in mind.

Defined Under Namespace

Classes: FileMappedDict, InvalidStoreSettingsError

Constant Summary collapse

AGGREGATION_MODES =
[MAX = :max, MIN = :min, SUM = :sum, ALL = :all, MOST_RECENT = :most_recent]
DEFAULT_METRIC_SETTINGS =
{ aggregation: SUM }
DEFAULT_GAUGE_SETTINGS =
{ aggregation: ALL }

Instance Method Summary collapse

Constructor Details

#initialize(dir:) ⇒ DirectFileStore

Returns a new instance of DirectFileStore.



36
37
38
39
# File 'lib/prometheus/client/data_stores/direct_file_store.rb', line 36

def initialize(dir:)
  @store_settings = { dir: dir }
  FileUtils.mkdir_p(dir)
end

Instance Method Details

#for_metric(metric_name, metric_type:, metric_settings: {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/prometheus/client/data_stores/direct_file_store.rb', line 41

def for_metric(metric_name, metric_type:, metric_settings: {})
  default_settings = DEFAULT_METRIC_SETTINGS
  if metric_type == :gauge
    default_settings = DEFAULT_GAUGE_SETTINGS
  end

  settings = default_settings.merge(metric_settings)
  validate_metric_settings(metric_type, settings)

  MetricStore.new(metric_name: metric_name,
                  store_settings: @store_settings,
                  metric_settings: settings)
end