opentelemetry-logs-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +9 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +201 -0
- data/lib/opentelemetry/sdk/logs/configuration_patch.rb +71 -0
- data/lib/opentelemetry/sdk/logs/export/batch_log_record_processor.rb +218 -0
- data/lib/opentelemetry/sdk/logs/export/console_log_record_exporter.rb +39 -0
- data/lib/opentelemetry/sdk/logs/export/in_memory_log_record_exporter.rb +104 -0
- data/lib/opentelemetry/sdk/logs/export/log_record_exporter.rb +58 -0
- data/lib/opentelemetry/sdk/logs/export/simple_log_record_processor.rb +88 -0
- data/lib/opentelemetry/sdk/logs/export.rb +31 -0
- data/lib/opentelemetry/sdk/logs/log_record.rb +164 -0
- data/lib/opentelemetry/sdk/logs/log_record_data.rb +25 -0
- data/lib/opentelemetry/sdk/logs/log_record_limits.rb +43 -0
- data/lib/opentelemetry/sdk/logs/log_record_processor.rb +47 -0
- data/lib/opentelemetry/sdk/logs/logger.rb +92 -0
- data/lib/opentelemetry/sdk/logs/logger_provider.rb +164 -0
- data/lib/opentelemetry/sdk/logs/version.rb +14 -0
- data/lib/opentelemetry/sdk/logs.rb +24 -0
- data/lib/opentelemetry-logs-sdk.rb +9 -0
- metadata +220 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b95cb9f7e5a248dfe1943fb2c24f54e18b35d9360c26b64f7f93c4a1da3856eb
|
4
|
+
data.tar.gz: 3b0f1a5b3dc37087aae50dd0a99b7bad9709b55f28e7cdcfa9d35b3de38ddec4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0050dac60d1856bbb9e118a7ba95e2d66297500453581214b8c853433b4782bd923b57a18c08156fd91ab7c4966ca1805c2479b1612b39b29119c6f157b873fe
|
7
|
+
data.tar.gz: 05aec7579acd3164a78ea62db2cf5e7b1bc9e1fc45f40c835b168d72faf20b1a2cd60298239c240f042e4da8f4e974a43e31334faf8409c2a0fc953b518d7d06
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright The OpenTelemetry Authors
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright The OpenTelemetry Authors
|
4
|
+
#
|
5
|
+
# SPDX-License-Identifier: Apache-2.0
|
6
|
+
|
7
|
+
require 'opentelemetry/sdk/configurator'
|
8
|
+
|
9
|
+
module OpenTelemetry
|
10
|
+
module SDK
|
11
|
+
module Logs
|
12
|
+
# The ConfiguratorPatch implements a hook to configure the logs portion
|
13
|
+
# of the SDK.
|
14
|
+
module ConfiguratorPatch
|
15
|
+
def add_log_record_processor(log_record_processor)
|
16
|
+
@log_record_processors << log_record_processor
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
super
|
23
|
+
@log_record_processors = []
|
24
|
+
end
|
25
|
+
|
26
|
+
# The logs_configuration_hook method is where we define the setup
|
27
|
+
# process for logs SDK.
|
28
|
+
def logs_configuration_hook
|
29
|
+
OpenTelemetry.logger_provider = Logs::LoggerProvider.new(resource: @resource)
|
30
|
+
configure_log_record_processors
|
31
|
+
end
|
32
|
+
|
33
|
+
def configure_log_record_processors
|
34
|
+
processors = @log_record_processors.empty? ? wrapped_log_exporters_from_env.compact : @log_record_processors
|
35
|
+
processors.each { |p| OpenTelemetry.logger_provider.add_log_record_processor(p) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def wrapped_log_exporters_from_env
|
39
|
+
# TODO: set default to OTLP to match traces, default is console until other exporters merged
|
40
|
+
exporters = ENV.fetch('OTEL_LOGS_EXPORTER', 'console')
|
41
|
+
|
42
|
+
exporters.split(',').map do |exporter|
|
43
|
+
case exporter.strip
|
44
|
+
when 'none' then nil
|
45
|
+
when 'console' then Logs::Export::SimpleLogRecordProcessor.new(Logs::Export::ConsoleLogRecordExporter.new)
|
46
|
+
when 'otlp'
|
47
|
+
otlp_protocol = ENV['OTEL_EXPORTER_OTLP_LOGS_PROTOCOL'] || ENV['OTEL_EXPORTER_OTLP_PROTOCOL'] || 'http/protobuf'
|
48
|
+
|
49
|
+
if otlp_protocol != 'http/protobuf'
|
50
|
+
OpenTelemetry.logger.warn "The #{otlp_protocol} transport protocol is not supported by the OTLP exporter, log_records will not be exported."
|
51
|
+
nil
|
52
|
+
else
|
53
|
+
begin
|
54
|
+
Logs::Export::BatchLogRecordProcessor.new(OpenTelemetry::Exporter::OTLP::Logs::LogsExporter.new)
|
55
|
+
rescue NameError
|
56
|
+
OpenTelemetry.logger.warn 'The otlp logs exporter cannot be configured - please add opentelemetry-exporter-otlp-logs to your Gemfile. Logs will not be exported'
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
else
|
61
|
+
OpenTelemetry.logger.warn "The #{exporter} exporter is unknown and cannot be configured, log records will not be exported"
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
OpenTelemetry::SDK::Configurator.prepend(OpenTelemetry::SDK::Logs::ConfiguratorPatch)
|
@@ -0,0 +1,218 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright The OpenTelemetry Authors
|
4
|
+
#
|
5
|
+
# SPDX-License-Identifier: Apache-2.0
|
6
|
+
|
7
|
+
module OpenTelemetry
|
8
|
+
module SDK
|
9
|
+
module Logs
|
10
|
+
module Export
|
11
|
+
# WARNING - The spec has some differences from the LogRecord version of this processor
|
12
|
+
# Implementation of the duck type LogRecordProcessor that batches
|
13
|
+
# log records exported by the SDK then pushes them to the exporter
|
14
|
+
# pipeline.
|
15
|
+
#
|
16
|
+
# Typically, the BatchLogRecordProcessor will be more suitable for
|
17
|
+
# production environments than the SimpleLogRecordProcessor.
|
18
|
+
class BatchLogRecordProcessor < LogRecordProcessor # rubocop:disable Metrics/ClassLength
|
19
|
+
# Returns a new instance of the {BatchLogRecordProcessor}.
|
20
|
+
#
|
21
|
+
# @param [LogRecordExporter] exporter The (duck type) LogRecordExporter to where the
|
22
|
+
# recorded LogRecords are pushed after batching.
|
23
|
+
# @param [Numeric] exporter_timeout The maximum allowed time to export data.
|
24
|
+
# Defaults to the value of the OTEL_BLRP_EXPORT_TIMEOUT
|
25
|
+
# environment variable, if set, or 30,000 (30 seconds).
|
26
|
+
# @param [Numeric] schedule_delay the delay interval between two consecutive exports.
|
27
|
+
# Defaults to the value of the OTEL_BLRP_SCHEDULE_DELAY environment
|
28
|
+
# variable, if set, or 1,000 (1 second).
|
29
|
+
# @param [Integer] max_queue_size the maximum queue size in log records.
|
30
|
+
# Defaults to the value of the OTEL_BLRP_MAX_QUEUE_SIZE environment
|
31
|
+
# variable, if set, or 2048.
|
32
|
+
# @param [Integer] max_export_batch_size the maximum batch size in log records.
|
33
|
+
# Defaults to the value of the OTEL_BLRP_MAX_EXPORT_BATCH_SIZE environment
|
34
|
+
# variable, if set, or 512.
|
35
|
+
#
|
36
|
+
# @return a new instance of the {BatchLogRecordProcessor}.
|
37
|
+
def initialize(exporter,
|
38
|
+
exporter_timeout: Float(ENV.fetch('OTEL_BLRP_EXPORT_TIMEOUT', 30_000)),
|
39
|
+
schedule_delay: Float(ENV.fetch('OTEL_BLRP_SCHEDULE_DELAY', 1000)),
|
40
|
+
max_queue_size: Integer(ENV.fetch('OTEL_BLRP_MAX_QUEUE_SIZE', 2048)),
|
41
|
+
max_export_batch_size: Integer(ENV.fetch('OTEL_BLRP_MAX_EXPORT_BATCH_SIZE', 512)),
|
42
|
+
start_thread_on_boot: String(ENV['OTEL_RUBY_BLRP_START_THREAD_ON_BOOT']) !~ /false/i)
|
43
|
+
unless max_export_batch_size <= max_queue_size
|
44
|
+
raise ArgumentError,
|
45
|
+
'max_export_batch_size much be less than or equal to max_queue_size'
|
46
|
+
end
|
47
|
+
|
48
|
+
unless Common::Utilities.valid_exporter?(exporter)
|
49
|
+
raise ArgumentError,
|
50
|
+
"exporter #{exporter.inspect} does not appear to be a valid exporter"
|
51
|
+
end
|
52
|
+
|
53
|
+
@exporter = exporter
|
54
|
+
@exporter_timeout_seconds = exporter_timeout / 1000.0
|
55
|
+
@mutex = Mutex.new
|
56
|
+
@export_mutex = Mutex.new
|
57
|
+
@condition = ConditionVariable.new
|
58
|
+
@keep_running = true
|
59
|
+
@stopped = false
|
60
|
+
@delay_seconds = schedule_delay / 1000.0
|
61
|
+
@max_queue_size = max_queue_size
|
62
|
+
@batch_size = max_export_batch_size
|
63
|
+
@log_records = []
|
64
|
+
@pid = nil
|
65
|
+
@thread = nil
|
66
|
+
reset_on_fork(restart_thread: start_thread_on_boot)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Adds a log record to the batch. Thread-safe; may block on lock.
|
70
|
+
def on_emit(log_record, _context)
|
71
|
+
return if @stopped
|
72
|
+
|
73
|
+
lock do
|
74
|
+
reset_on_fork
|
75
|
+
n = log_records.size + 1 - max_queue_size
|
76
|
+
if n.positive?
|
77
|
+
log_records.shift(n)
|
78
|
+
report_dropped_log_records(n, reason: 'buffer-full')
|
79
|
+
end
|
80
|
+
log_records << log_record
|
81
|
+
@condition.signal if log_records.size > batch_size
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Export all emitted log records that have not yet been exported to
|
86
|
+
# the configured `Exporter`.
|
87
|
+
#
|
88
|
+
# This method should only be called in cases where it is absolutely
|
89
|
+
# necessary, such as when using some FaaS providers that may suspend
|
90
|
+
# the process after an invocation, but before the `Processor` exports
|
91
|
+
# the completed log records.
|
92
|
+
#
|
93
|
+
# @param [optional Numeric] timeout An optional timeout in seconds.
|
94
|
+
# @return [Integer] SUCCESS if no error occurred, FAILURE if a
|
95
|
+
# non-specific failure occurred, TIMEOUT if a timeout occurred.
|
96
|
+
def force_flush(timeout: nil)
|
97
|
+
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
|
98
|
+
|
99
|
+
snapshot = lock do
|
100
|
+
reset_on_fork if @keep_running
|
101
|
+
log_records.shift(log_records.size)
|
102
|
+
end
|
103
|
+
|
104
|
+
until snapshot.empty?
|
105
|
+
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
|
106
|
+
return TIMEOUT if remaining_timeout&.zero?
|
107
|
+
|
108
|
+
batch = snapshot.shift(batch_size).map!(&:to_log_record_data)
|
109
|
+
result_code = export_batch(batch, timeout: remaining_timeout)
|
110
|
+
return result_code unless result_code == SUCCESS
|
111
|
+
end
|
112
|
+
|
113
|
+
@exporter.force_flush(timeout: OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time))
|
114
|
+
ensure
|
115
|
+
# Unshift the remaining log records if we timed out. We drop excess
|
116
|
+
# log records from the snapshot because they're older than any
|
117
|
+
# records in the buffer.
|
118
|
+
lock do
|
119
|
+
n = log_records.size + snapshot.size - max_queue_size
|
120
|
+
|
121
|
+
if n.positive?
|
122
|
+
snapshot.shift(n)
|
123
|
+
report_dropped_log_records(n, reason: 'buffer-full')
|
124
|
+
end
|
125
|
+
|
126
|
+
log_records.unshift(*snapshot) unless snapshot.empty?
|
127
|
+
@condition.signal if log_records.size > max_queue_size / 2
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# Shuts the consumer thread down and flushes the current accumulated
|
132
|
+
# buffer will block until the thread is finished.
|
133
|
+
#
|
134
|
+
# @param [optional Numeric] timeout An optional timeout in seconds.
|
135
|
+
# @return [Integer] SUCCESS if no error occurred, FAILURE if a
|
136
|
+
# non-specific failure occurred, TIMEOUT if a timeout occurred.
|
137
|
+
def shutdown(timeout: nil)
|
138
|
+
return if @stopped
|
139
|
+
|
140
|
+
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
|
141
|
+
thread = lock do
|
142
|
+
@keep_running = false
|
143
|
+
@stopped = true
|
144
|
+
@condition.signal
|
145
|
+
@thread
|
146
|
+
end
|
147
|
+
|
148
|
+
thread&.join(timeout)
|
149
|
+
force_flush(timeout: OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time))
|
150
|
+
dropped_log_records = lock { log_records.size }
|
151
|
+
report_dropped_log_records(dropped_log_records, reason: 'terminating') if dropped_log_records.positive?
|
152
|
+
|
153
|
+
@exporter.shutdown(timeout: OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time))
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
attr_reader :log_records, :max_queue_size, :batch_size
|
159
|
+
|
160
|
+
def work
|
161
|
+
loop do
|
162
|
+
batch = lock do
|
163
|
+
@condition.wait(@mutex, @delay_seconds) if log_records.size < batch_size && @keep_running
|
164
|
+
@condition.wait(@mutex, @delay_seconds) while log_records.empty? && @keep_running
|
165
|
+
return unless @keep_running
|
166
|
+
|
167
|
+
fetch_batch
|
168
|
+
end
|
169
|
+
|
170
|
+
export_batch(batch)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def reset_on_fork(restart_thread: true)
|
175
|
+
pid = Process.pid
|
176
|
+
return if @pid == pid
|
177
|
+
|
178
|
+
@pid = pid
|
179
|
+
log_records.clear
|
180
|
+
@thread = restart_thread ? Thread.new { work } : nil
|
181
|
+
rescue ThreadError => e
|
182
|
+
OpenTelemetry.handle_error(exception: e, message: 'unexpected error in BatchLogRecordProcessor#reset_on_fork')
|
183
|
+
end
|
184
|
+
|
185
|
+
def export_batch(batch, timeout: @exporter_timeout_seconds)
|
186
|
+
result_code = @export_mutex.synchronize { @exporter.export(batch, timeout: timeout) }
|
187
|
+
report_result(result_code, batch)
|
188
|
+
result_code
|
189
|
+
rescue StandardError => e
|
190
|
+
report_result(FAILURE, batch)
|
191
|
+
OpenTelemetry.handle_error(exception: e, message: 'unexpected error in BatchLogRecordProcessor#export_batch')
|
192
|
+
end
|
193
|
+
|
194
|
+
def report_result(result_code, batch)
|
195
|
+
if result_code == SUCCESS
|
196
|
+
OpenTelemetry.logger.debug("Successfully exported #{batch.size} log records")
|
197
|
+
else
|
198
|
+
OpenTelemetry.handle_error(exception: ExportError.new("Unable to export #{batch.size} log records"))
|
199
|
+
OpenTelemetry.logger.error("Result code: #{result_code}")
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def report_dropped_log_records(count, reason:)
|
204
|
+
OpenTelemetry.logger.warn("#{count} log record(s) dropped. Reason: #{reason}")
|
205
|
+
end
|
206
|
+
|
207
|
+
def fetch_batch
|
208
|
+
log_records.shift(@batch_size).map!(&:to_log_record_data)
|
209
|
+
end
|
210
|
+
|
211
|
+
def lock(&block)
|
212
|
+
@mutex.synchronize(&block)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright The OpenTelemetry Authors
|
4
|
+
#
|
5
|
+
# SPDX-License-Identifier: Apache-2.0
|
6
|
+
|
7
|
+
module OpenTelemetry
|
8
|
+
module SDK
|
9
|
+
module Logs
|
10
|
+
module Export
|
11
|
+
# Outputs {LogRecordData} to the console.
|
12
|
+
#
|
13
|
+
# Potentially useful for exploratory purposes.
|
14
|
+
class ConsoleLogRecordExporter
|
15
|
+
def initialize
|
16
|
+
@stopped = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def export(log_records, timeout: nil)
|
20
|
+
return FAILURE if @stopped
|
21
|
+
|
22
|
+
Array(log_records).each { |s| pp s }
|
23
|
+
|
24
|
+
SUCCESS
|
25
|
+
end
|
26
|
+
|
27
|
+
def force_flush(timeout: nil)
|
28
|
+
SUCCESS
|
29
|
+
end
|
30
|
+
|
31
|
+
def shutdown(timeout: nil)
|
32
|
+
@stopped = true
|
33
|
+
SUCCESS
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright The OpenTelemetry Authors
|
4
|
+
#
|
5
|
+
# SPDX-License-Identifier: Apache-2.0
|
6
|
+
|
7
|
+
module OpenTelemetry
|
8
|
+
module SDK
|
9
|
+
module Logs
|
10
|
+
module Export
|
11
|
+
# A LogRecordExporter implementation that can be used to test OpenTelemetry integration.
|
12
|
+
#
|
13
|
+
# @example Usage in a test suite:
|
14
|
+
# class MyClassTest
|
15
|
+
# def setup
|
16
|
+
# @logger_provider = LoggerProvider.new
|
17
|
+
# @exporter = InMemoryLogRecordExporter.new
|
18
|
+
# @logger_provider.add_log_record_processor(SimpleLogRecordProcessor.new(@exporter))
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# def test_emitted_log_records
|
22
|
+
# log_record = OpenTelemetry::SDK::Logs::LogRecord.new(body: 'log')
|
23
|
+
# @logger_provider.logger.on_emit(log_record, context)
|
24
|
+
#
|
25
|
+
# log_records = @exporter.emitted_log_records
|
26
|
+
|
27
|
+
# refute_nil(log_records)
|
28
|
+
# assert_equal(1, log_records.size)
|
29
|
+
# assert_equal(log_records[0].body, 'log')
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
class InMemoryLogRecordExporter
|
33
|
+
# Returns a new instance of the {InMemoryLogRecordExporter}.
|
34
|
+
#
|
35
|
+
# @return a new instance of the {InMemoryLogRecordExporter}.
|
36
|
+
def initialize
|
37
|
+
@emitted_log_records = []
|
38
|
+
@stopped = false
|
39
|
+
@mutex = Mutex.new
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a frozen array of the emitted {LogRecordData}s, represented by
|
43
|
+
# {io.opentelemetry.proto.trace.v1.LogRecord}.
|
44
|
+
#
|
45
|
+
# @return [Array<LogRecordData>] a frozen array of the emitted {LogRecordData}s.
|
46
|
+
def emitted_log_records
|
47
|
+
@mutex.synchronize do
|
48
|
+
@emitted_log_records.clone.freeze
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Clears the internal collection of emitted {LogRecord}s.
|
53
|
+
#
|
54
|
+
# Does not reset the state of this exporter if already shutdown.
|
55
|
+
def reset
|
56
|
+
@mutex.synchronize do
|
57
|
+
@emitted_log_records.clear
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Called to export {LogRecordData}s.
|
62
|
+
#
|
63
|
+
# @param [Enumerable<LogRecordData>] log_record_datas the list of {LogRecordData}s to be
|
64
|
+
# exported.
|
65
|
+
# @param [optional Numeric] timeout An optional timeout in seconds.
|
66
|
+
# @return [Integer] the result of the export, SUCCESS or
|
67
|
+
# FAILURE
|
68
|
+
def export(log_record_datas, timeout: nil)
|
69
|
+
@mutex.synchronize do
|
70
|
+
return FAILURE if @stopped
|
71
|
+
|
72
|
+
@emitted_log_records.concat(log_record_datas.to_a)
|
73
|
+
end
|
74
|
+
SUCCESS
|
75
|
+
end
|
76
|
+
|
77
|
+
# Called when {LoggerProvider#force_flush} is called, if this exporter is
|
78
|
+
# registered to a {LoggerProvider} object.
|
79
|
+
#
|
80
|
+
# @param [optional Numeric] timeout An optional timeout in seconds.
|
81
|
+
# @return [Integer] SUCCESS if no error occurred, FAILURE if a
|
82
|
+
# non-specific failure occurred, TIMEOUT if a timeout occurred.
|
83
|
+
def force_flush(timeout: nil)
|
84
|
+
SUCCESS
|
85
|
+
end
|
86
|
+
|
87
|
+
# Called when {LoggerProvider#shutdown} is called, if this exporter is
|
88
|
+
# registered to a {LoggerProvider} object.
|
89
|
+
#
|
90
|
+
# @param [optional Numeric] timeout An optional timeout in seconds.
|
91
|
+
# @return [Integer] SUCCESS if no error occurred, FAILURE if a
|
92
|
+
# non-specific failure occurred, TIMEOUT if a timeout occurred.
|
93
|
+
def shutdown(timeout: nil)
|
94
|
+
@mutex.synchronize do
|
95
|
+
@emitted_log_records.clear
|
96
|
+
@stopped = true
|
97
|
+
end
|
98
|
+
SUCCESS
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|