Top Level Namespace
Defined Under Namespace
Modules: AsyncTaskOTELPatch, PrometheusParser, RackHelpers, StackServiceBase Classes: FiberConnectionPool
Constant Summary collapse
- QUIET =
ENV.fetch('QUIET', 'false') == 'true'
- PERFORMANCE =
ENV.fetch('PERFORMANCE', 'false') == 'true'
- CONSOLE_LOGGER =
ENV ||= ‘XTerm’ # JSON,Text,XTerm,Default
Class.new { def <<(...) = Console.logger.info(...) def info(...) = Console.logger.info(...) def debug(...) = Console.logger.debug(...) def debug1(...) = Console.logger.debug(...) def debug2(...) = Console.logger.debug(...) def debug3(...) = Console.logger.debug(...) def warn(...) = Console.logger.warn(...) def error(...) = Console.logger.error(...) def fatal(...) = Console.logger.fatal(...) }.new
- LOGGER =
Class.new { def initialize = @context_list ||= {} def add_context(name) task = Async::Task.current? @context_list[task.__id__] = name end def find_context return '' unless Thread.current[:async_task] t_stack = [Async::Task.current] t_stack << t_stack.last.parent while t_stack.last.parent task = t_stack.find { @context_list[_1.__id__] } task ? " [#{@context_list[task.__id__]}] " : '' end def <<(...) = do_log(:info, ...) def info(...) = do_log(:info, ...) def debug(...) = do_log(:debug, ...) def debug1(...) = do_log(:debug1, ...) def debug2(...) = do_log(:debug2, ...) def debug3(...) = do_log(:debug3, ...) def warn(...) = do_log(:warn, ...) def error(...) = do_log(:error, ...) def fatal(...) = do_log(:fatal, ...) def do_log(name, *args) debug_level = name[/(\d+)/, 1].to_i unless debug_level > LOG_DEPTH if TRACE_METHODS call_stack = Thread.current[:call_stack] ||= {} call_stack_fiber = call_stack[Fiber.current.__id__] ||= [] last = call_stack_fiber[-3] ? call_stack_fiber[-3].join('.').gsub('Class:', '').gsub(/[#<>]/, '') : '' last += find_context msg = "\e[33m#{last}:\e[0m \e[38;5;254m" + args.flatten.map(&:inspect).join(', ') else msg = args.flatten.map(&:inspect).join(', ') end _name = name.to_s.gsub(/\d/, '') _name = 'info' if _name == '<<' Console.logger.send _name.to_s.gsub(/\d/, ''), msg end end }.new
- LOGGER_GRAPE =
Class.new { def method_missing(name, d) Console.logger.send name, "REST_API: #{d[:method]} #{d[:path]} #{d[:params]} - #{d[:status]} host:#{d[:host]} time:#{d[:time]}" end }.new
- OTEL_ENABLED =
!ENV['OTEL_EXPORTER_OTLP_ENDPOINT'].to_s.empty?
- STACK_NAME =
ENV['STACK_NAME'] || 'undefined_stack'
- SERVICE_NAME =
ENV['STACK_SERVICE_NAME'] || 'undefined_service'
Instance Method Summary collapse
- #flatten_hash(hash, path = [], result = {}) ⇒ Object
- #otel_initialize ⇒ Object
- #otl_current_span {|OpenTelemetry::Trace.current_span| ... } ⇒ Object
- #otl_def(name) ⇒ Object
- #otl_span(name, attributes = {}) ⇒ Object
-
#otl_traceparent_id ⇒ Object
OpenTelemetry::Trace.current_span.tap do |span| span.record_exception(e) span.status = OpenTelemetry::Trace::Status.error(“Unhandled exception of type: #ee.class”) end.
Instance Method Details
#flatten_hash(hash, path = [], result = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/stack-service-base/open_telemetry.rb', line 43 def flatten_hash(hash, path = [], result = {}) hash.each do |k, v| path += [k] result[path.join('.')] = v.to_s if v.is_a?(String) || v.is_a?(Numeric) flatten_hash(v, path, result) if v.is_a?(Hash) || v.is_a?(Array) path.pop end result end |
#otel_initialize ⇒ Object
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 |
# File 'lib/stack-service-base/open_telemetry.rb', line 57 def otel_initialize $stdout.puts "otl_configure: OTEL_ENABLED: #{OTEL_ENABLED}" return unless OTEL_ENABLED OpenTelemetry::SDK.configure do |c| OpenTelemetry::Instrumentation::Rack # OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware # Rack::Builder OpenTelemetry::Instrumentation::Sinatra c.use_all({ 'OpenTelemetry::Instrumentation::Sinatra' => { install_rack: false }, # instlled by stack-service-base.rb#24 (use *OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args) 'OpenTelemetry::Instrumentation::Rack' => { use_rack_events: false, # TODO: doesnt work with Websoket requests # /home/user/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/opentelemetry-instrumentation-rack-0.26.0/lib/opentelemetry/instrumentation/rack/instrumentation.rb#43 url_quantization: ->(path, env) { "HTTP #{env['REQUEST_METHOD']} #{path}" }, untraced_requests: ->(env) { env['HTTP_UPGRADE'] == 'websocket' } } }) # c.service_name = SERVICE_NAME end at_exit do OpenTelemetry.tracer_provider.force_flush OpenTelemetry.tracer_provider.shutdown end $tracer_ = OpenTelemetry.tracer_provider.tracer(SERVICE_NAME) otl_span "#{SERVICE_NAME} start", { 'stack.name': ENV['STACK_NAME'], 'stack.service.name': ENV['STACK_SERVICE_NAME'], 'org.opencontainers.image.title': ENV['ORG_OPENCONTAINERS_IMAGE_TITLE'], 'org.opencontainers.image.url': ENV['ORG_OPENCONTAINERS_IMAGE_URL'], 'org.opencontainers.image.source': ENV['ORG_OPENCONTAINERS_IMAGE_SOURCE'], 'org.opencontainers.image.created': ENV['ORG_OPENCONTAINERS_IMAGE_CREATED'], 'com.gitlab.ci.commt.timestamp': ENV['COM_GITLAB_CI_COMMIT_TIMESTAMP'], 'com.gitlab.ci.tag': ENV['COM_GITLAB_CI_TAG'], RACK_ENV: ENV['RACK_ENV'], NODE_ENV: ENV['NODE_ENV'], SERVER_ENV: ENV['SERVER_ENV'], } do |span| span.add_event("not-working in kibana APM", attributes:{ event: 'Success', message: 'Get data from elastic Success' }.transform_keys(&:to_s) ) # span.status = OpenTelemetry::Trace::Status.error("error message here!") end end |
#otl_current_span {|OpenTelemetry::Trace.current_span| ... } ⇒ Object
136 137 138 139 |
# File 'lib/stack-service-base/open_telemetry.rb', line 136 def otl_current_span return unless OTEL_ENABLED yield OpenTelemetry::Trace.current_span end |
#otl_def(name) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/stack-service-base/open_telemetry.rb', line 156 def otl_def(name) original_method = self.respond_to?(:instance_method) ? instance_method(name) : method(name) self.respond_to?(:remove_method) ? remove_method(name) : Object.send(:remove_method, name) original_method = original_method.respond_to?(:unbind) ? original_method.unbind : original_method define_method(name) do |*args, **kwargs, &block| klass = self.respond_to?(:class_name) ? self.class_name : (self.respond_to?(:name) ? self.name : 'main') otl_span("method: #{klass}.#{name}", {args: args.to_s, kwargs: kwargs.to_s}) do |span| original_method.bind(self).call(*args, **kwargs, &block) end end end |
#otl_span(name, attributes = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/stack-service-base/open_telemetry.rb', line 126 def otl_span(name, attributes = {}) # span_ = OpenTelemetry::Trace.current_span return yield(nil) unless OTEL_ENABLED return yield(nil) unless $tracer_ $tracer_&.in_span(name, attributes: flatten_hash(attributes.transform_keys(&:to_s).transform_values{_1 || 'n/a'}) ) do |span| yield span end end |
#otl_traceparent_id ⇒ Object
OpenTelemetry::Trace.current_span.tap do |span|
span.record_exception(e)
span.status = OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{e.class}")
end
146 147 148 149 150 151 152 153 154 |
# File 'lib/stack-service-base/open_telemetry.rb', line 146 def otl_traceparent_id return nil unless OTEL_ENABLED span_context = OpenTelemetry::Trace.current_span.context trace_id = span_context.trace_id.unpack1('H*') span_id = span_context.span_id.unpack1('H*') trace_flags = format('%02x', span_context.trace_flags.instance_eval{ @flags }) # Two-digit hex for trace flags (e.g., sampled) "00-#{trace_id}-#{span_id}-#{trace_flags}" end |