Class: Datadog::Statsd::Schema::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd/schema/analyzer.rb

Overview

Analyzes schema instances to provide comprehensive metrics statistics

Since:

  • 0.1.0

Defined Under Namespace

Classes: BaseFormatter, JSONFormatter, TextFormatter, YAMLFormatter

Constant Summary collapse

METRIC_EXPANSIONS =

Metric suffixes for different metric types that create multiple metrics

Since:

  • 0.1.0

{
  gauge: %w[count min max sum avg],
  distribution: %w[count min max sum avg p50 p75 p90 p95 p99],
  histogram: %w[count min max sum avg]
}.freeze
SUPPORTED_FORMATS =

Since:

  • 0.1.0

%i[text json yaml].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schemas, stdout: $stdout, stderr: $stderr, color: true, format: SUPPORTED_FORMATS.first) ⇒ Analyzer

Initialize analyzer with schema(s)

Parameters:

Raises:

  • (ArgumentError)

Since:

  • 0.1.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datadog/statsd/schema/analyzer.rb', line 63

def initialize(
  schemas,
  stdout: $stdout,
  stderr: $stderr,
  color: true,
  format: SUPPORTED_FORMATS.first
)
  @schemas = Array(schemas)
  @stdout = stdout
  @stderr = stderr
  @color = color
  @format = format.to_sym

  raise ArgumentError, "Unsupported format: #{format}. Supported formats are: #{SUPPORTED_FORMATS.join(", ")}" unless SUPPORTED_FORMATS.include?(format)

  if color
    Colored2.enable!
  else
    Colored2.disable!
  end

  @analysis_result = analyze
end

Instance Attribute Details

#analysis_resultObject (readonly)

Since:

  • 0.1.0



56
57
58
# File 'lib/datadog/statsd/schema/analyzer.rb', line 56

def analysis_result
  @analysis_result
end

#colorObject (readonly)

Since:

  • 0.1.0



56
57
58
# File 'lib/datadog/statsd/schema/analyzer.rb', line 56

def color
  @color
end

#formatObject (readonly)

Since:

  • 0.1.0



56
57
58
# File 'lib/datadog/statsd/schema/analyzer.rb', line 56

def format
  @format
end

#schemasObject (readonly)

Since:

  • 0.1.0



56
57
58
# File 'lib/datadog/statsd/schema/analyzer.rb', line 56

def schemas
  @schemas
end

#stderrObject (readonly)

Since:

  • 0.1.0



56
57
58
# File 'lib/datadog/statsd/schema/analyzer.rb', line 56

def stderr
  @stderr
end

#stdoutObject (readonly)

Since:

  • 0.1.0



56
57
58
# File 'lib/datadog/statsd/schema/analyzer.rb', line 56

def stdout
  @stdout
end

Instance Method Details

#analyzeAnalysisResult

Perform comprehensive analysis of the schemas

Returns:

Since:

  • 0.1.0



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/datadog/statsd/schema/analyzer.rb', line 89

def analyze
  all_metrics = collect_all_metrics
  metrics_analysis = analyze_metrics(all_metrics).map(&:to_h)

  total_unique_metrics = metrics_analysis.sum { |analysis| analysis[:expanded_names].size }
  total_possible_custom_metrics = metrics_analysis.sum { |e| e[:total_combinations] }

  AnalysisResult.new(
    total_unique_metrics:,
    metrics_analysis:,
    total_possible_custom_metrics:
  )
end

#renderObject

Since:

  • 0.1.0



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/datadog/statsd/schema/analyzer.rb', line 103

def render
  case format
  when :text
    TextFormatter.new(stdout:, stderr:, color:, analysis_result:).render
  when :json
    JSONFormatter.new(stdout:, stderr:, color:, analysis_result:).render
  when :yaml
    YAMLFormatter.new(stdout:, stderr:, color:, analysis_result:).render
  else
    raise ArgumentError, "Unsupported format: #{format}. Supported formats are: #{SUPPORTED_FORMATS.join(", ")}"
  end
end