Skip to content

Commit

Permalink
Remove server telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Apr 21, 2023
1 parent 31361a2 commit 7d4468a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 95 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/ruby_lsp/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require "sorbet-runtime"
require "syntax_tree"
require "language_server-protocol"
require "benchmark"
require "bundler"

require "ruby-lsp"
Expand Down
41 changes: 5 additions & 36 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 2 additions & 12 deletions lib/ruby_lsp/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 3 additions & 44 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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 } },
Expand All @@ -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 } },
Expand All @@ -99,16 +93,14 @@ 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

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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" } },
Expand All @@ -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",
{
Expand Down Expand Up @@ -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",
{
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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",
{
Expand All @@ -321,16 +292,14 @@ def test_selection_ranges_with_syntax_error
},
)

assert_nil(response[:result])
assert_nil(response[:result].first)
assert_nil(response[:error])
end

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))
Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 7d4468a

Please sign in to comment.