From 371d748d3bc2c221b54dce07ffef0534c93e8a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ka=C5=BAmierczak?= Date: Tue, 17 Dec 2024 17:04:09 +0100 Subject: [PATCH] a better approach towards client testing --- .../grpc/interceptors/client_tracer_test.rb | 73 +++++++++++++++++++ .../instrumentation/grpc_test.rb | 42 ----------- 2 files changed, 73 insertions(+), 42 deletions(-) create mode 100644 instrumentation/grpc/test/opentelemetry/instrumentation/grpc/interceptors/client_tracer_test.rb delete mode 100644 instrumentation/grpc/test/opentelemetry/instrumentation/grpc_test.rb diff --git a/instrumentation/grpc/test/opentelemetry/instrumentation/grpc/interceptors/client_tracer_test.rb b/instrumentation/grpc/test/opentelemetry/instrumentation/grpc/interceptors/client_tracer_test.rb new file mode 100644 index 000000000..537c5e38f --- /dev/null +++ b/instrumentation/grpc/test/opentelemetry/instrumentation/grpc/interceptors/client_tracer_test.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative '../../../../test_helper' +require_relative '../../../../support/grpc_server_runner' + +describe OpenTelemetry::Instrumentation::Grpc::Interceptors::ClientTracer do + let(:instrumentation) { OpenTelemetry::Instrumentation::Grpc::Instrumentation.instance } + let(:exporter) { EXPORTER } + let(:request) { Support::Proto::PingRequest.new(value: 'Ping!') } + let(:server_runner) { Support::GrpcServerRunner.new } + + before do + instrumentation.install + exporter.reset + + # use a real gRPC server to avoid non-trivial mocks + server_port = server_runner.start + # create a client stub to interact with the server + @stub = Support::Proto::PingServer::Stub.new( + "localhost:#{server_port}", + :this_channel_is_insecure + ) + end + after do + server_runner.stop + end + + describe '#request_response' do + it 'registers a span' do + _(exporter.finished_spans.size).must_equal 0 + + @stub.request_response_ping(request) + + _(exporter.finished_spans.size).must_equal 1 + end + + it 'sets expected kind' do + @stub.request_response_ping(request) + + span = exporter.finished_spans.first + + _(span).wont_be_nil + _(span.kind).must_equal(:client) + end + + it 'sets expected name' do + @stub.request_response_ping(request) + + span = exporter.finished_spans.first + + _(span).wont_be_nil + _(span.name).must_equal('support.proto.PingServer/RequestResponsePing') + end + + it 'sets expected attributes' do + @stub.request_response_ping(request) + + span = exporter.finished_spans.first + + _(span).wont_be_nil + _(span.attributes['rpc.system']).must_equal('grpc') + _(span.attributes['rpc.service']).must_equal('support.proto.PingServer') + _(span.attributes['rpc.method']).must_equal('RequestResponsePing') + _(span.attributes['rpc.type']).must_equal('request_response') + _(span.attributes['net.sock.peer.addr']).wont_be_empty + _(span.attributes['rpc.grpc.status_code']).must_equal(0) + end + end +end diff --git a/instrumentation/grpc/test/opentelemetry/instrumentation/grpc_test.rb b/instrumentation/grpc/test/opentelemetry/instrumentation/grpc_test.rb deleted file mode 100644 index d99452f3f..000000000 --- a/instrumentation/grpc/test/opentelemetry/instrumentation/grpc_test.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require_relative '../../test_helper' -require_relative '../../support/proto/ping_services_pb' -require_relative '../../support/grpc_server_runner' - -describe OpenTelemetry::Instrumentation::Grpc do - let(:instrumentation) { OpenTelemetry::Instrumentation::Grpc::Instrumentation.instance } - let(:exporter) { EXPORTER } - let(:server_runner) { Support::GrpcServerRunner.new } - - before do - instrumentation.install - exporter.reset - - server_port = server_runner.start - @stub = Support::Proto::PingServer::Stub.new( - "localhost:#{server_port}", - :this_channel_is_insecure, - ) - end - after do - server_runner.stop - end - - describe 'tracing' do - it 'before request' do - _(exporter.finished_spans.size).must_equal 0 - end - - it 'after request' do - request = Support::Proto::PingRequest.new(value: 'Ping!') - @stub.request_response_ping(request) - - _(exporter.finished_spans.size).must_equal 1 - end - end -end