Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Tag opentelemetry request

Oliver Güntherbf93b827b355
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 1d470310bd4..dcafc202460 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -161,6 +161,7 @@ class ApplicationController < ActionController::Base
include Redmine::Search::Controller
include Redmine::MenuManager::MenuController
+
helper Redmine::MenuManager::MenuHelper
# set http headers so that the browser does not store any
@@ -179,7 +180,9 @@ class ApplicationController < ActionController::Base
end
def tag_request
- ::OpenProject::Appsignal.tag_request(controller: self, request:)
+ context = { controller: self, request: }
+ ::OpenProject::Appsignal.tag_request(context)
+ ::OpenProject::OpenTelemetry.tag_request(context)
end
def reload_mailer_settings!
diff --git a/config/initializers/opentelemetry.rb b/config/initializers/opentelemetry.rb
index f763e9d33e9..a69a756c976 100644
--- a/config/initializers/opentelemetry.rb
+++ b/config/initializers/opentelemetry.rb
@@ -30,16 +30,22 @@
Rails.application.configure do
if OpenProject::Configuration.opentelemetry_enabled?
+
+ require "opentelemetry/sdk"
+ require "opentelemetry-exporter-otlp"
+ require "opentelemetry-instrumentation-all"
+ require_relative "../../lib_static/open_project/opentelemetry"
+
# add log tags for log correlation
config.log_tags += [
->(*) { "trace_id=#{OpenTelemetry::Trace.current_span.context.hex_trace_id}" },
->(*) { "span_id=#{OpenTelemetry::Trace.current_span.context.hex_span_id}" }
]
- require "opentelemetry/sdk"
- require "opentelemetry-exporter-otlp"
- require "opentelemetry-instrumentation-all"
-
OpenTelemetry::SDK.configure(&:use_all)
+
+ # Extend the core log delegator
+ handler = OpenProject::OpenTelemetry.method(:exception_handler)
+ OpenProject::Logging::LogDelegator.register(:opentelemetry, handler)
end
end
diff --git a/lib_static/open_project/opentelemetry.rb b/lib_static/open_project/opentelemetry.rb
new file mode 100644
index 00000000000..b853c33f8dd
--- /dev/null
+++ b/lib_static/open_project/opentelemetry.rb
@@ -0,0 +1,136 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+module OpenProject
+ module OpenTelemetry
+ module_function
+
+ def enabled?
+ OpenProject::Configuration.opentelemetry_enabled?
+ end
+
+ def exception_handler(_message, log_context = {})
+ return unless enabled?
+
+ exception = log_context[:exception]
+ return if exception.nil?
+
+ current_span = ::OpenTelemetry::Trace.current_span
+ if current_span.context.valid?
+ # Add exception to current span
+ current_span.record_exception(exception)
+ set_span_attributes(current_span, tags(log_context))
+ else
+ # Create a new span for the exception if no current span
+ tracer = ::OpenTelemetry.tracer_provider.tracer("openproject")
+ tracer.in_span("exception") do |span|
+ span.record_exception(exception)
+ set_span_attributes(span, tags(log_context))
+ end
+ end
+ end
+
+ ##
+ # Add current user and other stateful attributes to the current span
+ # @param context A hash of context, such as passing in the current controller or request
+ def tag_request(context = {})
+ return unless enabled?
+
+ current_span = ::OpenTelemetry::Trace.current_span
+ return unless current_span.context.valid?
+
+ attributes = tags(context)
+ set_span_attributes(current_span, attributes)
+ end
+
+ ##
+ # Tags to be added for OpenTelemetry spans
+ def tags(context)
+ OpenProject::Logging.extend_payload!(default_payload, context)
+ end
+
+ ##
+ # Default payload to add for OpenTelemetry spans
+ def default_payload
+ {
+ "openproject.locale" => I18n.locale.to_s,
+ "openproject.version" => OpenProject::VERSION.to_semver,
+ "openproject.core_hash" => OpenProject::VERSION.revision,
+ "openproject.core_version" => OpenProject::VERSION.core_sha,
+ "openproject.product_version" => OpenProject::VERSION.product_sha
+ }.compact
+ end
+
+ ##
+ # Create a new span with attributes
+ # @param name The span name
+ # @param attributes Hash of attributes to set on the span
+ def with_span(name, attributes = {}, &)
+ return yield unless enabled?
+
+ tracer = ::OpenTelemetry.tracer_provider.tracer("openproject")
+ tracer.in_span(name, attributes: attributes, &)
+ end
+
+ ##
+ # Add attributes to the current span
+ # @param attributes Hash of attributes to add
+ def add_attributes(attributes)
+ return unless enabled?
+
+ current_span = ::OpenTelemetry::Trace.current_span
+ return unless current_span.context.valid?
+
+ set_span_attributes(current_span, attributes)
+ end
+
+ ##
+ # Add an event to the current span
+ # @param name Event name
+ # @param attributes Hash of attributes for the event
+ def add_event(name, attributes = {})
+ return unless enabled?
+
+ current_span = ::OpenTelemetry::Trace.current_span
+ return unless current_span.context.valid?
+
+ current_span.add_event(name, attributes)
+ end
+
+ ##
+ # Helper method to set multiple attributes on a span
+ # OpenTelemetry uses set_attribute (singular) not set_attributes (plural)
+ def set_span_attributes(span, attributes)
+ attributes.each do |key, value|
+ span.set_attribute(key, value)
+ end
+ end
+ end
+end