From e83ecb1ccb13b487ea2b24f82855a96e0ea41afc Mon Sep 17 00:00:00 2001 From: Jiren Patel Date: Mon, 3 Feb 2020 09:20:58 +0530 Subject: [PATCH 1/4] allow custom lib name and version for telemetry purpose --- .../lib/google-cloud-spanner.rb | 28 +++- .../lib/google/cloud/spanner.rb | 21 ++- .../lib/google/cloud/spanner/service.rb | 19 +-- .../test/google/cloud/spanner_test.rb | 120 +++++++++++++++++- 4 files changed, 167 insertions(+), 21 deletions(-) diff --git a/google-cloud-spanner/lib/google-cloud-spanner.rb b/google-cloud-spanner/lib/google-cloud-spanner.rb index 5c26fd2166c3..fd59b3523fe5 100644 --- a/google-cloud-spanner/lib/google-cloud-spanner.rb +++ b/google-cloud-spanner/lib/google-cloud-spanner.rb @@ -45,6 +45,12 @@ module Cloud # @param [Integer] timeout Default timeout to use in requests. Optional. # @param [Hash] client_config A hash of values to override the default # behavior of the API client. Optional. + # @param [String] lib_name Library name. This will override the default + # name of the library in the api call request header + # for telemetry. Optional. + # @param [String] lib_version Library version. This will override the + # default version of the library in the api call request header + # for telemetry. Optional. # # @return [Google::Cloud::Spanner::Project] # @@ -61,10 +67,13 @@ module Cloud # platform_scope = "https://www.googleapis.com/auth/cloud-platform" # spanner = gcloud.spanner scope: platform_scope # - def spanner scope: nil, timeout: nil, client_config: nil + def spanner scope: nil, timeout: nil, client_config: nil, lib_name: nil, + lib_version: nil Google::Cloud.spanner @project, @keyfile, scope: scope, timeout: (timeout || @timeout), - client_config: client_config + client_config: client_config, + lib_name: lib_name, + lib_version: lib_version end ## @@ -92,7 +101,12 @@ def spanner scope: nil, timeout: nil, client_config: nil # @param [Integer] timeout Default timeout to use in requests. Optional. # @param [Hash] client_config A hash of values to override the default # behavior of the API client. Optional. - # + # @param [String] lib_name Library name. This will override the default + # name of the library in the api call request header + # for telemetry. Optional. + # @param [String] lib_version Library version. This will override the + # default version of the library in the api call request header + # for telemetry. Optional. # @return [Google::Cloud::Spanner::Project] # # @example @@ -101,12 +115,14 @@ def spanner scope: nil, timeout: nil, client_config: nil # spanner = Google::Cloud.spanner # def self.spanner project_id = nil, credentials = nil, scope: nil, - timeout: nil, client_config: nil + timeout: nil, client_config: nil, lib_name: nil, + lib_version: nil require "google/cloud/spanner" Google::Cloud::Spanner.new project_id: project_id, credentials: credentials, scope: scope, timeout: timeout, - client_config: client_config + client_config: client_config, + lib_name: lib_name, lib_version: lib_version end end end @@ -137,4 +153,6 @@ def self.spanner project_id = nil, credentials = nil, scope: nil, config.add_field! :client_config, nil, match: Hash config.add_field! :endpoint, nil, match: String config.add_field! :emulator_host, default_emulator, match: String, allow_nil: true + config.add_field! :lib_name, nil, match: String, allow_nil: true + config.add_field! :lib_version, nil, match: String, allow_nil: true end diff --git a/google-cloud-spanner/lib/google/cloud/spanner.rb b/google-cloud-spanner/lib/google/cloud/spanner.rb index da4241334b0b..aef7fa2b39ab 100644 --- a/google-cloud-spanner/lib/google/cloud/spanner.rb +++ b/google-cloud-spanner/lib/google/cloud/spanner.rb @@ -34,7 +34,7 @@ module Cloud # See {file:OVERVIEW.md Spanner Overview}. # module Spanner - # rubocop:disable Metrics/MethodLength + # rubocop:disable Metrics/MethodLength,Metrics/AbcSize ## # Creates a new object for connecting to the Spanner service. @@ -68,6 +68,12 @@ module Spanner # Deprecated. # @param [String] emulator_host Spanner emulator host. Optional. # If the param is nil, uses the value of the `emulator_host` config. + # @param [String] lib_name Library name. This will override the default + # name of the library in the api call request header + # for telemetry. Optional. + # @param [String] lib_version Library version. This will override the + # default version of the library in the api call request header + # for telemetry. Optional. # # @return [Google::Cloud::Spanner::Project] # @@ -78,7 +84,7 @@ module Spanner # def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil, client_config: nil, endpoint: nil, project: nil, keyfile: nil, - emulator_host: nil + emulator_host: nil, lib_name: nil, lib_version: nil project_id ||= (project || default_project_id) scope ||= configure.scope timeout ||= configure.timeout @@ -86,6 +92,8 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil, endpoint ||= configure.endpoint credentials ||= (keyfile || default_credentials(scope: scope)) emulator_host ||= configure.emulator_host + lib_name ||= configure.lib_name + lib_version ||= configure.lib_version if emulator_host credentials = :this_channel_is_insecure @@ -106,12 +114,13 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil, Spanner::Project.new( Spanner::Service.new( project_id, credentials, - host: endpoint, timeout: timeout, client_config: client_config + host: endpoint, timeout: timeout, client_config: client_config, + lib_name: lib_name, lib_version: lib_version ) ) end - # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/MethodLength,Metrics/AbcSize ## # Configure the Google Cloud Spanner library. @@ -133,6 +142,10 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil, # to use the default endpoint. # * `emulator_host` - (String) Host name of the emulator. Defaults to # `ENV["SPANNER_EMULATOR_HOST"]`. + # * `lib_name` - (String) Override the lib name , or `nil` + # to use the default lib name. + # * `lib_version` - (String) Override the lib version , or `nil` + # to use the default version. # # @return [Google::Cloud::Config] The configuration object the # Google::Cloud::Spanner library uses. diff --git a/google-cloud-spanner/lib/google/cloud/spanner/service.rb b/google-cloud-spanner/lib/google/cloud/spanner/service.rb index 1f758e370b3d..aec5c7063d84 100644 --- a/google-cloud-spanner/lib/google/cloud/spanner/service.rb +++ b/google-cloud-spanner/lib/google/cloud/spanner/service.rb @@ -29,17 +29,20 @@ module Spanner # @private Represents the gRPC Spanner service, including all the API # methods. class Service - attr_accessor :project, :credentials, :timeout, :client_config, :host + attr_accessor :project, :credentials, :timeout, :client_config, :host, + :lib_name, :lib_version ## # Creates a new Service instance. def initialize project, credentials, host: nil, timeout: nil, - client_config: nil + client_config: nil, lib_name: nil, lib_version: nil @project = project @credentials = credentials @host = host || V1::SpannerClient::SERVICE_ADDRESS @timeout = timeout @client_config = client_config || {} + @lib_name = lib_name || "gccl" + @lib_version = lib_version || Google::Cloud::Spanner::VERSION end def channel @@ -67,8 +70,8 @@ def service client_config: client_config, service_address: service_address, service_port: service_port, - lib_name: "gccl", - lib_version: Google::Cloud::Spanner::VERSION + lib_name: lib_name, + lib_version: lib_version ) end attr_accessor :mocked_service @@ -82,8 +85,8 @@ def instances client_config: client_config, service_address: service_address, service_port: service_port, - lib_name: "gccl", - lib_version: Google::Cloud::Spanner::VERSION + lib_name: lib_name, + lib_version: lib_version ) end attr_accessor :mocked_instances @@ -97,8 +100,8 @@ def databases client_config: client_config, service_address: service_address, service_port: service_port, - lib_name: "gccl", - lib_version: Google::Cloud::Spanner::VERSION + lib_name: lib_name, + lib_version: lib_version ) end attr_accessor :mocked_databases diff --git a/google-cloud-spanner/test/google/cloud/spanner_test.rb b/google-cloud-spanner/test/google/cloud/spanner_test.rb index 77433c2e1fcd..6a0b3bc9672c 100644 --- a/google-cloud-spanner/test/google/cloud/spanner_test.rb +++ b/google-cloud-spanner/test/google/cloud/spanner_test.rb @@ -18,13 +18,15 @@ describe "#spanner" do it "calls out to Google::Cloud.spanner" do gcloud = Google::Cloud.new - stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil) { + stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil, lib_name: nil, lib_version: nil) { project.must_be :nil? keyfile.must_be :nil? scope.must_be :nil? timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? "spanner-project-object-empty" } Google::Cloud.stub :spanner, stubbed_spanner do @@ -35,13 +37,15 @@ it "passes project and keyfile to Google::Cloud.spanner" do gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil) { + stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil, lib_name: nil, lib_version: nil) { project.must_equal "project-id" keyfile.must_equal "keyfile-path" scope.must_be :nil? timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? "spanner-project-object" } Google::Cloud.stub :spanner, stubbed_spanner do @@ -52,13 +56,15 @@ it "passes project and keyfile and options to Google::Cloud.spanner" do gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil) { + stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil, lib_name: nil, lib_version: nil) { project.must_equal "project-id" keyfile.must_equal "keyfile-path" scope.must_equal "http://example.com/scope" timeout.must_equal 60 host.must_be :nil? client_config.must_equal({ "gax" => "options" }) + lib_name.must_be :nil? + lib_version.must_be :nil? "spanner-project-object-scoped" } Google::Cloud.stub :spanner, stubbed_spanner do @@ -66,6 +72,25 @@ project.must_equal "spanner-project-object-scoped" end end + + it "passes lib name and version to Google::Cloud.spanner" do + gcloud = Google::Cloud.new + stubbed_spanner = ->(project, keyfile, scope: nil, timeout: nil, host: nil, client_config: nil, lib_name: nil, lib_version: nil) { + project.must_be :nil? + keyfile.must_be :nil? + scope.must_be :nil? + timeout.must_be :nil? + host.must_be :nil? + client_config.must_be :nil? + lib_name.must_equal "spanner-ruby" + lib_version.must_equal "1.0.0" + "spanner-project-object-with-lib-version-name" + } + Google::Cloud.stub :spanner, stubbed_spanner do + project = gcloud.spanner lib_name: "spanner-ruby", lib_version: "1.0.0" + project.must_equal "spanner-project-object-with-lib-version-name" + end + end end describe ".spanner" do @@ -105,6 +130,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -136,7 +163,7 @@ def creds.is_a? target end let(:found_credentials) { "{}" } - it "gets defaults for project_id and keyfile" do + it "gets defaults for project_id, keyfile, lib_name and lib_version" do # Clear all environment variables ENV.stub :[], nil do # Get project_id from Google Compute Engine @@ -146,6 +173,8 @@ def creds.is_a? target spanner.must_be_kind_of Google::Cloud::Spanner::Project spanner.project.must_equal "project-id" spanner.service.credentials.must_equal default_credentials + spanner.service.lib_name.must_equal "gccl" + spanner.service.lib_version.must_equal Google::Cloud::Spanner::VERSION end end end @@ -163,6 +192,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -191,6 +222,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_equal endpoint client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -217,6 +250,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -250,6 +285,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } empty_env = OpenStruct.new @@ -307,6 +344,25 @@ def creds.is_a? target end end end + + it "uses provided lib name and lib version" do + lib_name = "spanner-ruby" + lib_version = "1.0.0" + + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud.stub :env, OpenStruct.new(project_id: "project-id") do + Google::Cloud::Spanner::Credentials.stub :default, default_credentials do + spanner = Google::Cloud::Spanner.new lib_name: lib_name, lib_version: lib_version + spanner.must_be_kind_of Google::Cloud::Spanner::Project + spanner.project.must_equal "project-id" + spanner.service.lib_name.must_equal lib_name + spanner.service.lib_version.must_equal lib_version + end + end + end + end end describe "Spanner.configure" do @@ -333,6 +389,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -371,6 +429,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -409,6 +469,8 @@ def creds.is_a? target timeout.must_equal 42 host.must_be :nil? client_config.must_equal spanner_client_config + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -449,6 +511,8 @@ def creds.is_a? target timeout.must_equal 42 host.must_be :nil? client_config.must_equal spanner_client_config + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -490,6 +554,8 @@ def creds.is_a? target timeout.must_be :nil? host.must_equal endpoint client_config.must_be :nil? + lib_name.must_be :nil? + lib_version.must_be :nil? OpenStruct.new project: project } @@ -534,5 +600,51 @@ def creds.is_a? target spanner.service.host.must_equal "localhost:4567" end end + + it "uses spanner config for custom lib name and version" do + custom_lib_name = "spanner-ruby" + custom_lib_version = "1.0.0" + + stubbed_credentials = ->(keyfile, scope: nil) { + scope.must_be :nil? + "spanner-credentials" + } + stubbed_service = ->(project, credentials, timeout: nil, host: nil, client_config: nil, lib_name: nil, lib_version: nil) { + project.must_equal "project-id" + credentials.must_equal "spanner-credentials" + timeout.must_be :nil? + host.must_be :nil? + client_config.must_be :nil? + lib_name.must_equal custom_lib_name + lib_version.must_equal custom_lib_version + OpenStruct.new project: project, lib_name: lib_name, lib_version: lib_version + } + + # Clear all environment variables + ENV.stub :[], nil do + # Set new configuration + Google::Cloud::Spanner.configure do |config| + config.project = "project-id" + config.keyfile = "path/to/keyfile.json" + config.lib_name = custom_lib_name + config.lib_version = custom_lib_version + end + + File.stub :file?, true, ["path/to/keyfile.json"] do + File.stub :read, found_credentials, ["path/to/keyfile.json"] do + Google::Cloud::Spanner::Credentials.stub :new, stubbed_credentials do + Google::Cloud::Spanner::Service.stub :new, stubbed_service do + spanner = Google::Cloud::Spanner.new + spanner.must_be_kind_of Google::Cloud::Spanner::Project + spanner.project.must_equal "project-id" + spanner.service.must_be_kind_of OpenStruct + spanner.service.lib_name.must_equal custom_lib_name + spanner.service.lib_version.must_equal custom_lib_version + end + end + end + end + end + end end end From 4077449f1f140f2b8a9fa14f9cc8b23735de77af Mon Sep 17 00:00:00 2001 From: Jiren Patel Date: Wed, 5 Feb 2020 11:40:56 +0530 Subject: [PATCH 2/4] format docs --- google-cloud-spanner/lib/google-cloud-spanner.rb | 4 ++-- google-cloud-spanner/lib/google/cloud/spanner.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-spanner/lib/google-cloud-spanner.rb b/google-cloud-spanner/lib/google-cloud-spanner.rb index fd59b3523fe5..23bafe3d8f52 100644 --- a/google-cloud-spanner/lib/google-cloud-spanner.rb +++ b/google-cloud-spanner/lib/google-cloud-spanner.rb @@ -46,8 +46,8 @@ module Cloud # @param [Hash] client_config A hash of values to override the default # behavior of the API client. Optional. # @param [String] lib_name Library name. This will override the default - # name of the library in the api call request header - # for telemetry. Optional. + # name of the library in the api call request header for telemetry. + # Optional. # @param [String] lib_version Library version. This will override the # default version of the library in the api call request header # for telemetry. Optional. diff --git a/google-cloud-spanner/lib/google/cloud/spanner.rb b/google-cloud-spanner/lib/google/cloud/spanner.rb index aef7fa2b39ab..2ea92e1c4783 100644 --- a/google-cloud-spanner/lib/google/cloud/spanner.rb +++ b/google-cloud-spanner/lib/google/cloud/spanner.rb @@ -69,8 +69,8 @@ module Spanner # @param [String] emulator_host Spanner emulator host. Optional. # If the param is nil, uses the value of the `emulator_host` config. # @param [String] lib_name Library name. This will override the default - # name of the library in the api call request header - # for telemetry. Optional. + # name of the library in the api call request header for telemetry. + # Optional. # @param [String] lib_version Library version. This will override the # default version of the library in the api call request header # for telemetry. Optional. From 63349f441f970dc546e5b3b14d794643c92e1f21 Mon Sep 17 00:00:00 2001 From: Jiren Patel Date: Thu, 6 Feb 2020 19:51:11 +0530 Subject: [PATCH 3/4] added custom lib name and version prefix - updated doc and tests --- .../lib/google-cloud-spanner.rb | 41 +++++++++++++------ .../lib/google/cloud/spanner.rb | 26 ++++++++---- .../lib/google/cloud/spanner/service.rb | 24 +++++++---- .../test/google/cloud/spanner_test.rb | 25 ++++++++++- 4 files changed, 86 insertions(+), 30 deletions(-) diff --git a/google-cloud-spanner/lib/google-cloud-spanner.rb b/google-cloud-spanner/lib/google-cloud-spanner.rb index 23bafe3d8f52..40dbc926c7ee 100644 --- a/google-cloud-spanner/lib/google-cloud-spanner.rb +++ b/google-cloud-spanner/lib/google-cloud-spanner.rb @@ -45,12 +45,20 @@ module Cloud # @param [Integer] timeout Default timeout to use in requests. Optional. # @param [Hash] client_config A hash of values to override the default # behavior of the API client. Optional. - # @param [String] lib_name Library name. This will override the default - # name of the library in the api call request header for telemetry. - # Optional. - # @param [String] lib_version Library version. This will override the - # default version of the library in the api call request header - # for telemetry. Optional. + # @param [String] lib_name Library name. This will be added as a prefix + # to the API call tracking header `x-goog-api-client` with provided + # lib version for telemetry. Optional. For example prefix looks like + # `spanner-activerecord/0.0.1 gccl/1.13.1`. Here, + # `spanner-activerecord/0.0.1` is provided custom library name and + # version and `gccl/1.13.1` represents the Cloud Spanner Ruby library + # with version. + # @param [String] lib_version Library version. This will be added as a + # prefix to the API call tracking header `x-goog-api-client` with + # provided lib name for telemetry. Optional. For example prefix look like + # `spanner-activerecord/0.0.1 gccl/1.13.1`. Here, + # `spanner-activerecord/0.0.1` is provided custom library name and + # version and `gccl/1.13.1` represents the Cloud Spanner Ruby library + # with version. # # @return [Google::Cloud::Spanner::Project] # @@ -101,12 +109,21 @@ def spanner scope: nil, timeout: nil, client_config: nil, lib_name: nil, # @param [Integer] timeout Default timeout to use in requests. Optional. # @param [Hash] client_config A hash of values to override the default # behavior of the API client. Optional. - # @param [String] lib_name Library name. This will override the default - # name of the library in the api call request header - # for telemetry. Optional. - # @param [String] lib_version Library version. This will override the - # default version of the library in the api call request header - # for telemetry. Optional. + # @param [String] lib_name Library name. This will be added as a prefix + # to the API call tracking header `x-goog-api-client` with provided + # lib version for telemetry. Optional. For example prefix looks like + # `spanner-activerecord/0.0.1 gccl/1.13.1`. Here, + # `spanner-activerecord/0.0.1` is provided custom library name and + # version and `gccl/1.13.1` represents the Cloud Spanner Ruby library + # with version. + # @param [String] lib_version Library version. This will be added as a + # prefix to the API call tracking header `x-goog-api-client` with + # provided lib name for telemetry. Optional. For example prefix look like + # `spanner-activerecord/0.0.1 gccl/1.13.1`. Here, + # `spanner-activerecord/0.0.1` is provided custom library name and + # version and `gccl/1.13.1` represents the Cloud Spanner Ruby library + # with version. + # # @return [Google::Cloud::Spanner::Project] # # @example diff --git a/google-cloud-spanner/lib/google/cloud/spanner.rb b/google-cloud-spanner/lib/google/cloud/spanner.rb index 2ea92e1c4783..f6405aeb40b4 100644 --- a/google-cloud-spanner/lib/google/cloud/spanner.rb +++ b/google-cloud-spanner/lib/google/cloud/spanner.rb @@ -68,12 +68,20 @@ module Spanner # Deprecated. # @param [String] emulator_host Spanner emulator host. Optional. # If the param is nil, uses the value of the `emulator_host` config. - # @param [String] lib_name Library name. This will override the default - # name of the library in the api call request header for telemetry. - # Optional. - # @param [String] lib_version Library version. This will override the - # default version of the library in the api call request header - # for telemetry. Optional. + # @param [String] lib_name Library name. This will be added as a prefix + # to the API call tracking header `x-goog-api-client` with provided + # lib version for telemetry. Optional. For example prefix looks like + # `spanner-activerecord/0.0.1 gccl/1.13.1`. Here, + # `spanner-activerecord/0.0.1` is provided custom library name and + # version and `gccl/1.13.1` represents the Cloud Spanner Ruby library + # with version. + # @param [String] lib_version Library version. This will be added as a + # prefix to the API call tracking header `x-goog-api-client` with + # provided lib name for telemetry. Optional. For example prefix look like + # `spanner-activerecord/0.0.1 gccl/1.13.1`. Here, + # `spanner-activerecord/0.0.1` is provided custom library name and + # version and `gccl/1.13.1` represents the Cloud Spanner Ruby library + # with version. # # @return [Google::Cloud::Spanner::Project] # @@ -143,9 +151,11 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil, # * `emulator_host` - (String) Host name of the emulator. Defaults to # `ENV["SPANNER_EMULATOR_HOST"]`. # * `lib_name` - (String) Override the lib name , or `nil` - # to use the default lib name. + # to use the default lib name without prefix in agent tracking + # header. # * `lib_version` - (String) Override the lib version , or `nil` - # to use the default version. + # to use the default version lib name without prefix in agent + # tracking header. # # @return [Google::Cloud::Config] The configuration object the # Google::Cloud::Spanner library uses. diff --git a/google-cloud-spanner/lib/google/cloud/spanner/service.rb b/google-cloud-spanner/lib/google/cloud/spanner/service.rb index aec5c7063d84..80381ce0bc0c 100644 --- a/google-cloud-spanner/lib/google/cloud/spanner/service.rb +++ b/google-cloud-spanner/lib/google/cloud/spanner/service.rb @@ -41,8 +41,8 @@ def initialize project, credentials, host: nil, timeout: nil, @host = host || V1::SpannerClient::SERVICE_ADDRESS @timeout = timeout @client_config = client_config || {} - @lib_name = lib_name || "gccl" - @lib_version = lib_version || Google::Cloud::Spanner::VERSION + @lib_name = lib_name + @lib_version = lib_version end def channel @@ -70,8 +70,8 @@ def service client_config: client_config, service_address: service_address, service_port: service_port, - lib_name: lib_name, - lib_version: lib_version + lib_name: lib_name_with_prefix, + lib_version: Google::Cloud::Spanner::VERSION ) end attr_accessor :mocked_service @@ -85,8 +85,8 @@ def instances client_config: client_config, service_address: service_address, service_port: service_port, - lib_name: lib_name, - lib_version: lib_version + lib_name: lib_name_with_prefix, + lib_version: Google::Cloud::Spanner::VERSION ) end attr_accessor :mocked_instances @@ -100,8 +100,8 @@ def databases client_config: client_config, service_address: service_address, service_port: service_port, - lib_name: lib_name, - lib_version: lib_version + lib_name: lib_name_with_prefix, + lib_version: Google::Cloud::Spanner::VERSION ) end attr_accessor :mocked_databases @@ -462,6 +462,14 @@ def service_port URI.parse("//#{host}").port end + def lib_name_with_prefix + return "gccl" if [nil, "gccl"].include? lib_name + + value = lib_name.dup + value << "/#{lib_version}" if lib_version + value << " gccl" + end + def default_options_from_session session_name default_prefix = session_name.split("/sessions/").first Google::Gax::CallOptions.new kwargs: \ diff --git a/google-cloud-spanner/test/google/cloud/spanner_test.rb b/google-cloud-spanner/test/google/cloud/spanner_test.rb index 6a0b3bc9672c..0736a17231a0 100644 --- a/google-cloud-spanner/test/google/cloud/spanner_test.rb +++ b/google-cloud-spanner/test/google/cloud/spanner_test.rb @@ -173,8 +173,9 @@ def creds.is_a? target spanner.must_be_kind_of Google::Cloud::Spanner::Project spanner.project.must_equal "project-id" spanner.service.credentials.must_equal default_credentials - spanner.service.lib_name.must_equal "gccl" - spanner.service.lib_version.must_equal Google::Cloud::Spanner::VERSION + spanner.service.lib_name.must_be :nil? + spanner.service.lib_version.must_be :nil? + spanner.service.send(:lib_name_with_prefix).must_equal "gccl" end end end @@ -359,6 +360,26 @@ def creds.is_a? target spanner.project.must_equal "project-id" spanner.service.lib_name.must_equal lib_name spanner.service.lib_version.must_equal lib_version + spanner.service.send(:lib_name_with_prefix).must_equal "#{lib_name}/#{lib_version} gccl" + end + end + end + end + + it "uses provided lib name only" do + lib_name = "spanner-ruby" + + # Clear all environment variables + ENV.stub :[], nil do + # Get project_id from Google Compute Engine + Google::Cloud.stub :env, OpenStruct.new(project_id: "project-id") do + Google::Cloud::Spanner::Credentials.stub :default, default_credentials do + spanner = Google::Cloud::Spanner.new lib_name: lib_name + spanner.must_be_kind_of Google::Cloud::Spanner::Project + spanner.project.must_equal "project-id" + spanner.service.lib_name.must_equal lib_name + spanner.service.lib_version.must_be :nil? + spanner.service.send(:lib_name_with_prefix).must_equal "#{lib_name} gccl" end end end From 7fb280046296016b3081623569ef4051dfd4436d Mon Sep 17 00:00:00 2001 From: Jiren Patel Date: Wed, 12 Feb 2020 14:20:20 +0530 Subject: [PATCH 4/4] update test cases for lib name and version --- .../test/google/cloud/spanner_test.rb | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/google-cloud-spanner/test/google/cloud/spanner_test.rb b/google-cloud-spanner/test/google/cloud/spanner_test.rb index 0736a17231a0..c3c33fa71a28 100644 --- a/google-cloud-spanner/test/google/cloud/spanner_test.rb +++ b/google-cloud-spanner/test/google/cloud/spanner_test.rb @@ -125,13 +125,16 @@ def creds.is_a? target "spanner-credentials" } stubbed_service = ->(project, credentials, timeout: nil, host: nil, client_config: nil, **keyword_args) { + project.must_equal "project-id" credentials.must_equal "spanner-credentials" timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -193,8 +196,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -223,8 +228,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_equal endpoint client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -251,8 +258,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -286,8 +295,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } empty_env = OpenStruct.new @@ -410,8 +421,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -450,8 +463,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -490,8 +505,10 @@ def creds.is_a? target timeout.must_equal 42 host.must_be :nil? client_config.must_equal spanner_client_config - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -532,8 +549,10 @@ def creds.is_a? target timeout.must_equal 42 host.must_be :nil? client_config.must_equal spanner_client_config - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -575,8 +594,10 @@ def creds.is_a? target timeout.must_be :nil? host.must_equal endpoint client_config.must_be :nil? - lib_name.must_be :nil? - lib_version.must_be :nil? + keyword_args.key?(:lib_name).must_equal true + keyword_args.key?(:lib_version).must_equal true + keyword_args[:lib_name].must_be :nil? + keyword_args[:lib_version].must_be :nil? OpenStruct.new project: project } @@ -630,15 +651,15 @@ def creds.is_a? target scope.must_be :nil? "spanner-credentials" } - stubbed_service = ->(project, credentials, timeout: nil, host: nil, client_config: nil, lib_name: nil, lib_version: nil) { + stubbed_service = ->(project, credentials, timeout: nil, host: nil, client_config: nil, **keyword_args) { project.must_equal "project-id" credentials.must_equal "spanner-credentials" timeout.must_be :nil? host.must_be :nil? client_config.must_be :nil? - lib_name.must_equal custom_lib_name - lib_version.must_equal custom_lib_version - OpenStruct.new project: project, lib_name: lib_name, lib_version: lib_version + keyword_args[:lib_name].must_equal custom_lib_name + keyword_args[:lib_version].must_equal custom_lib_version + OpenStruct.new project: project, lib_name: keyword_args[:lib_name], lib_version: keyword_args[:lib_version] } # Clear all environment variables