Class: ZuoraConnect::Telegraf
- Inherits:
-
Object
- Object
- ZuoraConnect::Telegraf
- Defined in:
- app/models/zuora_connect/telegraf.rb
Constant Summary collapse
- OUTBOUND_METRICS =
true
- OUTBOUND_METRICS_NAME =
"request-outbound"
- INBOUND_METRICS =
true
- INBOUND_METRICS_NAME =
"request-inbound"
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
Instance Method Summary collapse
- #connect ⇒ Object
- #format_metric_log(message, dump = nil) ⇒ Object
-
#initialize ⇒ Telegraf
constructor
A new instance of Telegraf.
- #write(direction: 'Unknown', tags: {}, values: {}) ⇒ Object
- #write_udp(series: '', tags: {}, values: {}) ⇒ Object
Constructor Details
#initialize ⇒ Telegraf
Returns a new instance of Telegraf.
10 11 12 |
# File 'app/models/zuora_connect/telegraf.rb', line 10 def initialize self.connect end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
3 4 5 |
# File 'app/models/zuora_connect/telegraf.rb', line 3 def host @host end |
Instance Method Details
#connect ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'app/models/zuora_connect/telegraf.rb', line 14 def connect ZuoraConnect.logger.debug(self.format_metric_log('Telegraf','Need new connection')) if ZuoraConnect.configuration.telegraf_debug uri = URI.parse(ZuoraConnect.configuration.telegraf_endpoint) self.host = UDPSocket.new.tap do |socket| socket.connect uri.host, uri.port end rescue => ex self.host = nil ZuoraConnect.logger.warn(self.format_metric_log('Telegraf', "Failed to connect: #{ex.class}")) if Rails.env.to_s != 'production' end |
#format_metric_log(message, dump = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/zuora_connect/telegraf.rb', line 62 def format_metric_log(, dump = nil) , dump_color = "1;91", "0;1" log_entry = " \e[#{}m#{}\e[0m " log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump if Rails.env == :development log_entry else [, dump].compact.join(' - ') end end |
#write(direction: 'Unknown', tags: {}, values: {}) ⇒ Object
25 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 |
# File 'app/models/zuora_connect/telegraf.rb', line 25 def write(direction: 'Unknown', tags: {}, values: {}) time = Benchmark.measure do |bench| # To avoid writing metrics from rspec tests if Rails.env.to_sym != :test app_instance = Thread.current[:appinstance].present? ? Thread.current[:appinstance].id : 0 = { app_name: ZuoraObservability::Env.app_name, process_type: ZuoraObservability::Env.process_type, app_instance: app_instance, pod_name: ZuoraObservability::Env.pod_name}.merge() if direction == :inbound if INBOUND_METRICS && !Thread.current[:inbound_metric].to_bool self.write_udp(series: INBOUND_METRICS_NAME, tags: , values: values) Thread.current[:inbound_metric] = true else return end elsif direction == :outbound self.write_udp(series: OUTBOUND_METRICS_NAME, tags: , values: values) if OUTBOUND_METRICS else self.write_udp(series: direction, tags: , values: values) end end end if ZuoraConnect.configuration.telegraf_debug ZuoraConnect.logger.debug(self.format_metric_log('Telegraf', .to_s)) ZuoraConnect.logger.debug(self.format_metric_log('Telegraf', values.to_s)) ZuoraConnect.logger.debug(self.format_metric_log('Telegraf', "Writing '#{direction.capitalize}': #{time.real.round(5)} ms")) end end |
#write_udp(series: '', tags: {}, values: {}) ⇒ Object
54 55 56 57 58 59 60 |
# File 'app/models/zuora_connect/telegraf.rb', line 54 def write_udp(series: '', tags: {}, values: {}) return if !values.present? self.host.write InfluxDB::PointValue.new({series: series, tags: , values: values}).dump rescue => ex self.connect ZuoraConnect.logger.warn(self.format_metric_log('Telegraf',"Failed to write udp: #{ex.class}")) if Rails.env.to_s != 'production' end |