forked from apache/skywalking-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.rb
More file actions
151 lines (124 loc) · 3.75 KB
/
agent.rb
File metadata and controls
151 lines (124 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_relative 'configuration'
require_relative 'environment'
require_relative 'plugins_manager'
require_relative 'reporter/report'
require_relative 'reporter/log_buffer_trigger'
require_relative 'tracing/span_context'
require_relative 'tracing/carrier_item'
require_relative 'tracing/segment'
require_relative 'tracing/tag'
module Skywalking
# @api private
class Agent
LOCK = Mutex.new
class << self
def agent
defined?(@agent) && @agent
end
def start(config)
return @agent if @agent
LOCK.synchronize do
return @agent if @agent
config ||= {}
config = Configuration.new(config) unless config.is_a?(Configuration)
@logger = config.logger
@agent_config = config.agent_config
@agent = new(@agent_config).start!
config.freeze
end
end
def stop
LOCK.synchronize do
return unless @agent
@agent&.shutdown
@agent = nil
end
end
def started?
!!(defined?(@agent) && @agent)
end
attr_reader :logger, :agent_config
# Get the singleton instance
# @return [Agent, nil] the agent instance or nil if not started
def instance
@agent
end
end
attr_reader :plugins, :reporter, :log_buffer, :config
def initialize(config)
@config = config
@plugins = Plugins::PluginsManager.new(config)
@reporter = Reporter::Report.new(config)
@log_buffer = Reporter::LogBufferTrigger.new(config)
add_shutdown_hook
end
def environment
@environment ||= Skywalking::Environment.instance
end
def start!
@plugins.init_plugins
@reporter.init_reporter
# Start log reporting thread
start_log_reporting_thread if @config[:log_reporter_active]
self
end
def shutdown
@reporter.stop
end
def add_shutdown_hook
return unless environment.shutdown_handler_supported?
at_exit do
shutdown
end
end
# Check if log reporter is active
# @return [Boolean] true if log reporter is active
def log_reporter_active?
@config[:log_reporter_active]
end
private
# Start the log reporting thread
def start_log_reporting_thread
Thread.new do
loop do
break unless log_reporter_active?
process_log_queue
sleep @config[:log_report_period]
end
end
end
# Process the log queue and send data to the server
def process_log_queue
log_count = 0
enumerator = Enumerator.new do |yielder|
while (log_data = log_buffer.stream_data)
log_data.each do |data|
log_count += 1
yielder << data
end
end
end
enumerator.each_slice(10) do |batch|
begin
reporter.report_log(batch)
rescue => e
Agent.logger.warn "Failed to report log data: #{e.message}"
end
end
end
end
end