Class: OpticsAgent::Agent
- Inherits:
-
Object
- Object
- OpticsAgent::Agent
show all
- Includes:
- Reporting, Singleton
- Defined in:
- lib/optics-agent/agent.rb
Overview
XXX: this is a class but acts as a singleton right now. Need to figure out how to pass the agent into the middleware
(for instance we could dynamically generate a middleware class,
or ask the user to pass the agent as an option) to avoid it
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_query(query, rack_env, start_time, end_time) ⇒ Object
-
#clear_query_queue ⇒ Object
-
#debug(message = nil) ⇒ Object
-
#graphql_middleware ⇒ Object
-
#initialize ⇒ Agent
constructor
-
#instrument_schema(schema) ⇒ Object
-
#log(message = nil) ⇒ Object
-
#rack_middleware ⇒ Object
-
#schedule_report ⇒ Object
-
#send_message(path, message) ⇒ Object
-
#set_options(debug: false, disable_reporting: false, print_reports: false, schema_report_delay_ms: 10 * 1000, report_interval_ms: 60 * 1000, api_key: , endpoint_url: ENV['OPTICS_ENDPOINT_URL'] || 'https://optics-report.apollodata.com') ⇒ Object
Methods included from Reporting
#add_latency, #client_info, #duration_nanos, #generate_report_header, #generate_timestamp
Constructor Details
#initialize ⇒ Agent
Returns a new instance of Agent.
20
21
22
23
24
25
26
|
# File 'lib/optics-agent/agent.rb', line 20
def initialize
@query_queue = []
@semaphone = Mutex.new
self.set_options
end
|
Instance Attribute Details
#schema ⇒ Object
Returns the value of attribute schema.
18
19
20
|
# File 'lib/optics-agent/agent.rb', line 18
def schema
@schema
end
|
Instance Method Details
#add_query(query, rack_env, start_time, end_time) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/optics-agent/agent.rb', line 76
def add_query(query, rack_env, start_time, end_time)
@semaphone.synchronize {
debug { "adding query to queue, queue length was #{@query_queue.length}" }
@query_queue << [query, rack_env, start_time, end_time]
}
end
|
#clear_query_queue ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/optics-agent/agent.rb', line 83
def clear_query_queue
@semaphone.synchronize {
debug { "clearing query queue, queue length was #{@query_queue.length}" }
queue = @query_queue
@query_queue = []
queue
}
end
|
#debug(message = nil) ⇒ Object
127
128
129
130
131
132
|
# File 'lib/optics-agent/agent.rb', line 127
def debug(message = nil)
if @debug
message = yield unless message
log "DEBUG: #{message} <#{Process.pid} | #{Thread.current.object_id}>"
end
end
|
#graphql_middleware ⇒ Object
96
97
98
99
|
# File 'lib/optics-agent/agent.rb', line 96
def graphql_middleware
OpticsAgent::GraphqlMiddleware.new
end
|
#instrument_schema(schema) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/optics-agent/agent.rb', line 46
def instrument_schema(schema)
@schema = schema
debug "adding middleware to schema"
schema.middleware << graphql_middleware
unless @disable_reporting
debug "spawning schema thread"
Thread.new do
debug "schema thread spawned"
sleep @schema_report_delay_ms / 1000
debug "running schema job"
SchemaJob.new.perform(self)
end
schedule_report
end
end
|
#log(message = nil) ⇒ Object
122
123
124
125
|
# File 'lib/optics-agent/agent.rb', line 122
def log(message = nil)
message = yield unless message
puts "optics-agent: #{message}"
end
|
#rack_middleware ⇒ Object
92
93
94
|
# File 'lib/optics-agent/agent.rb', line 92
def rack_middleware
OpticsAgent::RackMiddleware
end
|
#schedule_report ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/optics-agent/agent.rb', line 64
def schedule_report
debug "spawning reporting thread"
Thread.new do
debug "reporting thread spawned"
while true
sleep @report_interval_ms / 1000
debug "running reporting job"
ReportJob.new.perform(self)
end
end
end
|
#send_message(path, message) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/optics-agent/agent.rb', line 101
def send_message(path, message)
req = Net::HTTP::Post.new(path)
req['x-api-key'] = @api_key
req['user-agent'] = "optics-agent-rb"
req.body = message.class.encode(message)
if @debug || @print_reports
log "sending message: #{message.class.encode_json(message)}"
end
uri = URI.parse(@endpoint_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
res = http.request(req)
if @debug || @print_reports
log "got response: #{res.inspect}"
log "response body: #{res.body.inspect}"
end
end
|
#set_options(debug: false, disable_reporting: false, print_reports: false, schema_report_delay_ms: 10 * 1000, report_interval_ms: 60 * 1000, api_key: , endpoint_url: ENV['OPTICS_ENDPOINT_URL'] || 'https://optics-report.apollodata.com') ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/optics-agent/agent.rb', line 28
def set_options(
debug: false,
disable_reporting: false,
print_reports: false,
schema_report_delay_ms: 10 * 1000,
report_interval_ms: 60 * 1000,
api_key: ENV['OPTICS_API_KEY'],
endpoint_url: ENV['OPTICS_ENDPOINT_URL'] || 'https://optics-report.apollodata.com'
)
@debug = debug
@disable_reporting = disable_reporting || !endpoint_url || endpoint_url.nil?
@print_reports = print_reports
@schema_report_delay_ms = schema_report_delay_ms
@report_interval_ms = report_interval_ms
@api_key = api_key
@endpoint_url = endpoint_url
end
|