Skip to content

Commit

Permalink
Creates the new x-goog-api-client header. (#2020)
Browse files Browse the repository at this point in the history
This will send x-goog-api-client with the following format:
gl-node/(node version) gccl/(library version) grpc/(grpc version)

We will use this information for future tracking.

The GAPIC autogen files will have a slightly different format,
and that will be addressed later. This PR only changes the
APIs using gRPC without the GAPIC.
  • Loading branch information
jmuk authored and stephenplusplus committed Feb 24, 2017
1 parent e392643 commit 8d0a6ad
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/bigtable/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ function Bigtable(options) {
return new Bigtable(options);
}

options = extend({}, options, {
libVersion: require('../package.json').version
});

var baseUrl = 'bigtable.googleapis.com';
var adminBaseUrl = 'bigtableadmin.googleapis.com';

Expand Down
6 changes: 6 additions & 0 deletions packages/common-grpc/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var duplexify = require('duplexify');
var extend = require('extend');
var googleProtoFiles = require('google-proto-files');
var grpc = require('grpc');
var grpcVersion = require('grpc/package.json').version;
var is = require('is');
var nodeutil = require('util');
var path = require('path');
Expand Down Expand Up @@ -157,6 +158,11 @@ function GrpcService(config, options) {
}

this.grpcMetadata = new grpc.Metadata();
this.grpcMetadata.add('x-goog-api-client', [
'gl-node/' + process.versions.node,
'gccl/' + (options.libVersion || ''),
'grpc/' + grpcVersion
].join(' '));

if (config.grpcMetadata) {
for (var prop in config.grpcMetadata) {
Expand Down
55 changes: 42 additions & 13 deletions packages/common-grpc/test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var duplexify = require('duplexify');
var extend = require('extend');
var googleProtoFiles = require('google-proto-files');
var grpc = require('grpc');
var grpcVersion = require('grpc/package.json').version;
var is = require('is');
var path = require('path');
var proxyquire = require('proxyquire');
Expand Down Expand Up @@ -259,34 +260,62 @@ describe('GrpcService', function() {
assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure');
});

it('should default grpcMetadata to empty metadata', function() {
var fakeGrpcMetadata = {};
it('should default grpcMetadata to contain x-goog-api-client', function() {
var expectedMetadata = {
'x-goog-api-client':
'gl-node/' + process.versions.node + ' gccl/ grpc/' + grpcVersion
};

GrpcMetadataOverride = function() {
return fakeGrpcMetadata;
GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, value) {
this[prop] = value;
};

var config = extend({}, CONFIG);
delete config.grpcMetadata;

var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
assert.deepEqual(grpcService.grpcMetadata, expectedMetadata);
});

it('should customize library version', function() {
var expectedMetadata = {
'x-goog-api-client':
'gl-node/' + process.versions.node + ' gccl/0.1.2 grpc/' +
grpcVersion
};

GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, value) {
this[prop] = value;
};

var config = extend({}, CONFIG);
delete config.grpcMetadata;

var options = extend({}, OPTIONS, {
libVersion: '0.1.2'
});
var grpcService = new GrpcService(config, options);
assert.deepEqual(grpcService.grpcMetadata, expectedMetadata);
});

it('should create and localize grpcMetadata', function() {
var fakeGrpcMetadata = {
add: function(prop, value) {
assert.strictEqual(prop, Object.keys(CONFIG.grpcMetadata)[0]);
assert.strictEqual(value, CONFIG.grpcMetadata[prop]);
}
var expectedMetadata = {
'x-goog-api-client':
'gl-node/' + process.versions.node + ' gccl/ grpc/' + grpcVersion
};
Object.keys(CONFIG.grpcMetadata).forEach(function(prop) {
expectedMetadata[prop] = CONFIG.grpcMetadata[prop];
});

GrpcMetadataOverride = function() {
return fakeGrpcMetadata;
GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, value) {
this[prop] = value;
};

var grpcService = new GrpcService(CONFIG, OPTIONS);
assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
assert.deepEqual(grpcService.grpcMetadata, expectedMetadata);
});

it('should localize maxRetries', function() {
Expand Down
5 changes: 5 additions & 0 deletions packages/datastore/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var arrify = require('arrify');
var common = require('@google-cloud/common');
var commonGrpc = require('@google-cloud/common-grpc');
var extend = require('extend');
var is = require('is');
var modelo = require('modelo');

Expand Down Expand Up @@ -300,6 +301,10 @@ function Datastore(options) {
return new Datastore(options);
}

options = extend({}, options, {
libVersion: require('../package.json').version
});

this.defaultBaseUrl_ = 'datastore.googleapis.com';
this.determineBaseUrl_(options.apiEndpoint);

Expand Down
4 changes: 4 additions & 0 deletions packages/logging/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function Logging(options) {
return new Logging(options);
}

options = extend({}, options, {
libVersion: require('../package.json').version
});

var config = {
baseUrl: 'logging.googleapis.com',
service: 'logging',
Expand Down
4 changes: 4 additions & 0 deletions packages/pubsub/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ function PubSub(options) {
return new PubSub(options);
}

options = extend({}, options, {
libVersion: require('../package.json').version
});

this.defaultBaseUrl_ = 'pubsub.googleapis.com';
this.determineBaseUrl_();

Expand Down
5 changes: 4 additions & 1 deletion packages/pubsub/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ describe('PubSub', function() {
});

it('should localize the options provided', function() {
assert.strictEqual(pubsub.options, OPTIONS);
var expected = extend({}, OPTIONS, {
libVersion: require('../package.json').version
});
assert.deepStrictEqual(pubsub.options, expected);
});
});

Expand Down

0 comments on commit 8d0a6ad

Please sign in to comment.