Class: Sumologic::Metadata::SourceMetadataDiscovery

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/sumologic/metadata/source_metadata_discovery.rb

Overview

Discovers source metadata from actual log data via Search API Useful for CloudWatch/ECS sources that use dynamic _sourceName values

Instance Method Summary collapse

Constructor Details

#initialize(http_client:, search_job:, config: nil) ⇒ SourceMetadataDiscovery

Returns a new instance of SourceMetadataDiscovery.



13
14
15
16
17
# File 'lib/sumologic/metadata/source_metadata_discovery.rb', line 13

def initialize(http_client:, search_job:, config: nil)
  @http = http_client
  @search_job = search_job
  @config = config || Configuration.new
end

Instance Method Details

#discover(from_time:, to_time:, time_zone: 'UTC', **options) ⇒ Object

Discover source metadata from logs Returns hash with ALL unique source names found

Parameters:

  • from_time (String)

    Start time (ISO 8601, unix timestamp, or relative)

  • to_time (String)

    End time

  • time_zone (String) (defaults to: 'UTC')

    Time zone (default: UTC)

  • options (Hash)

    Optional filters — :filter, :keyword, :limit



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sumologic/metadata/source_metadata_discovery.rb', line 26

def discover(from_time:, to_time:, time_zone: 'UTC', **options)
  filter = options[:filter]
  keyword = options[:keyword]
  limit = options[:limit]

  query = build_query(filter)
  log_info "Discovering source metadata with query: #{query}"
  log_info "Time range: #{from_time} to #{to_time} (#{time_zone})"

  # Fetch aggregated records to find all unique sources
  # Internal limit of 10K aggregation records balances performance vs completeness
  records = @search_job.execute_aggregation(
    query: query,
    from_time: from_time,
    to_time: to_time,
    time_zone: time_zone,
    limit: 10_000
  )

  source_models = parse_aggregation_results(records)
  source_models = filter_by_keyword(source_models, keyword) if keyword
  source_models = source_models.take(limit) if limit

  {
    'time_range' => {
      'from' => from_time,
      'to' => to_time,
      'time_zone' => time_zone
    },
    'filter' => filter,
    'keyword' => keyword,
    'total_sources' => source_models.size,
    'sources' => source_models.map(&:to_h)
  }
rescue StandardError => e
  raise Error, "Failed to discover source metadata: #{e.message}"
end