Class: NewRelic::TelemetrySdk::Harvester

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/newrelic/telemetry_sdk/harvester.rb

Overview

This class handles sending data to New Relic automatically at configured intervals.

Examples:

harvester = NewRelic::TelemetrySdk::Harvester.new 
trace_client = NewRelic::TelemetrySdk::TraceClient.new
buffer = NewRelic::TelemetrySdk::Buffer.new
harvester.register 'external_spans', buffer, trace_client
harvester.start

Instance Method Summary collapse

Methods included from Logger

#clear_already_logged, #log_error, #log_once, logger, #logger, logger=, #logger=

Constructor Details

#initializeHarvester

Returns a new instance of Harvester.



21
22
23
24
25
26
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 21

def initialize
  @pipelines = {}
  @shutdown = false
  @running = false
  @lock = Mutex.new
end

Instance Method Details

#[](name) ⇒ Object



54
55
56
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 54

def [] name
  @pipelines[name]
end

#intervalObject



58
59
60
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 58

def interval
  TelemetrySdk.config.harvest_interval
end

#register(name, buffer, client) ⇒ Object

Register a pipeline (i.e. a buffer from which data can be harvested via a flush method and a client that can be used to send that data).

Parameters:

  • name (String)

    A unique name for the type of data associated with this pipeline. Examples: ‘spans’, ‘external_spans’

  • buffer (Buffer)

    An instance of NewRelic::TelemetrySdk::Buffer in which data can be stored for harvest.

  • client (Client)

    An instance of a NewRelic::TelemetrySdk::Client subclass which will send harvested data to the correct New Relic backend (e.g. TraceClient for spans).



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 42

def register name, buffer, client
  logger.info "Registering pipeline #{name}"
  @lock.synchronize do
    @pipelines[name] = {
      buffer: buffer,
      client: client
    }
  end
rescue => e
  log_error "Encountered error while registering buffer #{name}.", e
end

#running?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 62

def running?
  @running
end

#startObject

Start scheduled harvests via this harvester.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 69

def start
  logger.info "Harvesting every #{interval} seconds"
  @running = true
  @harvest_thread = Thread.new do
    begin
      while !@shutdown do
        sleep interval
        harvest
      end
      harvest
      @running = false
    rescue => e
      log_error "Encountered error in harvester", e
    end
  end
end

#stopObject

Stop scheduled harvests via this harvester. Any remaining buffered data will be sent before the harvest thread is stopped.



90
91
92
93
94
95
96
# File 'lib/newrelic/telemetry_sdk/harvester.rb', line 90

def stop
  logger.info "Stopping harvester"
  @shutdown = true
  @harvest_thread.join if @running
rescue => e
  log_error "Encountered error stopping harvester", e
end