diff --git a/lib/ruby_lsp/executor.rb b/lib/ruby_lsp/executor.rb index 54b6b8ec8..09ecc2942 100644 --- a/lib/ruby_lsp/executor.rb +++ b/lib/ruby_lsp/executor.rb @@ -19,13 +19,13 @@ def execute(request) response = T.let(nil, T.untyped) error = T.let(nil, T.nilable(Exception)) - request_time = Benchmark.realtime do + begin response = run(request) rescue StandardError, LoadError => e error = e end - Result.new(response: response, error: error, request_time: request_time) + Result.new(response: response, error: error) end private diff --git a/lib/ruby_lsp/internal.rb b/lib/ruby_lsp/internal.rb index d39f6967a..941901823 100644 --- a/lib/ruby_lsp/internal.rb +++ b/lib/ruby_lsp/internal.rb @@ -4,7 +4,6 @@ require "sorbet-runtime" require "syntax_tree" require "language_server-protocol" -require "benchmark" require "bundler" require "ruby-lsp" diff --git a/lib/ruby_lsp/server.rb b/lib/ruby_lsp/server.rb index 6073f4999..63c8de502 100644 --- a/lib/ruby_lsp/server.rb +++ b/lib/ruby_lsp/server.rb @@ -139,48 +139,17 @@ def finalize_request(result, request) error: { code: Constant::ErrorCodes::INTERNAL_ERROR, message: error.inspect, - data: request.to_json, + data: { + errorClass: error.class.name, + errorMessage: error.message, + backtrace: error.backtrace&.map { |bt| bt.sub(/^#{Dir.home}/, "~") }&.join("\n"), + }, }, ) elsif response != VOID @writer.write(id: request[:id], result: response) end - - request_time = result.request_time - if request_time - @writer.write(method: "telemetry/event", params: telemetry_params(request, request_time, error)) - end end end - - sig do - params( - request: T::Hash[Symbol, T.untyped], - request_time: Float, - error: T.nilable(Exception), - ).returns(T::Hash[Symbol, T.any(String, Float)]) - end - def telemetry_params(request, request_time, error) - uri = request.dig(:params, :textDocument, :uri) - params = { - request: request[:method], - lspVersion: RubyLsp::VERSION, - requestTime: request_time, - } - - if error - params[:errorClass] = error.class.name - params[:errorMessage] = error.message - - log_params = request[:params] - params[:params] = log_params.reject { |k, _| k == :textDocument }.to_json if log_params - - backtrace = error.backtrace - params[:backtrace] = backtrace.map { |bt| bt.sub(/^#{Dir.home}/, "~") }.join("\n") if backtrace - end - - params[:uri] = uri.sub(%r{.*://#{Dir.home}}, "~") if uri - params - end end end diff --git a/lib/ruby_lsp/utils.rb b/lib/ruby_lsp/utils.rb index 37212fce2..499bb7976 100644 --- a/lib/ruby_lsp/utils.rb +++ b/lib/ruby_lsp/utils.rb @@ -41,20 +41,10 @@ class Result sig { returns(T.nilable(Exception)) } attr_reader :error - sig { returns(T.nilable(Float)) } - attr_reader :request_time - - sig do - params( - response: T.untyped, - error: T.nilable(Exception), - request_time: T.nilable(Float), - ).void - end - def initialize(response:, error: nil, request_time: nil) + sig { params(response: T.untyped, error: T.nilable(Exception)).void } + def initialize(response:, error: nil) @response = response @error = error - @request_time = request_time end end diff --git a/test/integration_test.rb b/test/integration_test.rb index 8e193757a..3185c8dbe 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -52,8 +52,6 @@ def test_document_symbol initialize_lsp(["documentSymbols"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/documentSymbol", { textDocument: { uri: "file://#{__FILE__}" } }) symbol = response[:result].first assert_equal("Foo", symbol[:name]) @@ -64,8 +62,6 @@ def test_document_highlight initialize_lsp(["documentHighlights"]) open_file_with("$foo = 1") - assert_telemetry("textDocument/didOpen") - response = make_request( "textDocument/documentHighlight", { textDocument: { uri: "file://#{__FILE__}" }, position: { line: 0, character: 1 } }, @@ -79,8 +75,6 @@ def test_hover initialize_lsp(["hover"]) open_file_with("$foo = 1") - assert_telemetry("textDocument/didOpen") - response = make_request( "textDocument/hover", { textDocument: { uri: "file://#{__FILE__}" }, position: { line: 0, character: 1 } }, @@ -99,7 +93,7 @@ def test_document_highlight_with_syntax_error { textDocument: { uri: "file://#{__FILE__}" }, position: { line: 0, character: 1 } }, ) - assert_nil(response[:result]) + assert_empty(response[:result]) assert_nil(response[:error]) end @@ -107,8 +101,6 @@ def test_semantic_highlighting initialize_lsp(["semanticHighlighting"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/semanticTokens/full", { textDocument: { uri: "file://#{__FILE__}" } }) assert_equal([0, 6, 3, 2, 1], response[:result][:data]) end @@ -121,8 +113,6 @@ def foo end DOC - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/documentLink", { textDocument: { uri: "file://#{__FILE__}" } }) assert_match(/syntax_tree/, response.dig(:result, 0, :target)) end @@ -131,8 +121,6 @@ def test_formatting initialize_lsp(["formatting"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/formatting", { textDocument: { uri: "file://#{__FILE__}" } }) assert_equal(<<~FORMATTED, response[:result].first[:newText]) # typed: true @@ -147,8 +135,6 @@ def test_on_type_formatting initialize_lsp(["onTypeFormatting"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request( "textDocument/onTypeFormatting", { textDocument: { uri: "file://#{__FILE__}", position: { line: 0, character: 0 }, character: "\n" } }, @@ -160,8 +146,6 @@ def test_code_actions initialize_lsp(["codeActions"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request( "textDocument/codeAction", { @@ -216,8 +200,6 @@ def test_code_action_resolve initialize_lsp(["codeActions"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request( "codeAction/resolve", { @@ -235,8 +217,6 @@ def test_document_did_close initialize_lsp([]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - assert(send_request("textDocument/didClose", { textDocument: { uri: "file://#{__FILE__}" } })) end @@ -260,8 +240,6 @@ def test_folding_ranges initialize_lsp(["foldingRanges"]) open_file_with("class Foo\n\nend") - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/foldingRange", { textDocument: { uri: "file://#{__FILE__}" } }) assert_equal({ startLine: 0, endLine: 1, kind: "region" }, response[:result].first) end @@ -270,8 +248,6 @@ def test_code_lens initialize_lsp(["codeLens"], experimental_features_enabled: true) open_file_with("class Foo\n\nend") - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/codeLens", { textDocument: { uri: "file://#{__FILE__}" } }) assert_empty(response[:result]) end @@ -282,19 +258,14 @@ def test_request_with_telemetry send_request("textDocument/foldingRange", { textDocument: { uri: "file://#{__FILE__}" } }) - assert_telemetry("textDocument/didOpen") - response = read_response("textDocument/foldingRange") assert_equal({ startLine: 0, endLine: 1, kind: "region" }, response[:result].first) - assert_telemetry("textDocument/foldingRange") end def test_selection_ranges initialize_lsp(["selectionRanges"]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request( "textDocument/selectionRange", { @@ -321,7 +292,7 @@ def test_selection_ranges_with_syntax_error }, ) - assert_nil(response[:result]) + assert_nil(response[:result].first) assert_nil(response[:error]) end @@ -329,8 +300,6 @@ def test_diagnostics initialize_lsp([]) open_file_with("class Foo\nend") - assert_telemetry("textDocument/didOpen") - response = make_request("textDocument/diagnostic", { textDocument: { uri: "file://#{__FILE__}" } }) assert_equal("full", response.dig(:result, :kind)) @@ -339,16 +308,6 @@ def test_diagnostics private - def assert_telemetry(request) - telemetry_response = read_response("telemetry/event") - expected_uri = __FILE__.sub(Dir.home, "~") - - assert_equal(expected_uri, telemetry_response.dig(:params, :uri)) - assert_equal(RubyLsp::VERSION, telemetry_response.dig(:params, :lspVersion)) - assert_equal(request, telemetry_response.dig(:params, :request)) - assert_in_delta(0.5, telemetry_response.dig(:params, :requestTime), 2) - end - def make_request(request, params = nil) send_request(request, params) read_response(request) @@ -404,6 +363,6 @@ def initialize_lsp(enabled_features, experimental_features_enabled: false) end def open_file_with(content) - make_request("textDocument/didOpen", { textDocument: { uri: "file://#{__FILE__}", text: content } }) + send_request("textDocument/didOpen", { textDocument: { uri: "file://#{__FILE__}", text: content } }) end end