Skip to content

Commit

Permalink
Support relative config file paths from cwd
Browse files Browse the repository at this point in the history
Fixes #198
  • Loading branch information
Matt Loring committed Jan 6, 2016
1 parent e27630f commit a160e16
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var SpanData = require('./lib/span-data.js');
var common = require('@google/cloud-diagnostics-common');
var semver = require('semver');
var constants = require('./lib/constants.js');
var path = require('path');

/**
* Phantom implementation of the trace agent. This allows API users to decouple
Expand Down Expand Up @@ -53,7 +54,7 @@ var initConfig = function(projectConfig) {
util._extend(config, require('./config.js'));
util._extend(config, projectConfig);
if (process.env.hasOwnProperty('GCLOUD_TRACE_CONFIG')) {
util._extend(config, require(process.env.GCLOUD_TRACE_CONFIG));
util._extend(config, require(path.resolve(process.env.GCLOUD_TRACE_CONFIG)));
}
if (process.env.hasOwnProperty('GCLOUD_TRACE_LOGLEVEL')) {
config.logLevel = process.env.GCLOUD_TRACE_LOGLEVEL;
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/test-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"logLevel": 4,

"stackTraceLimit": 1,

"flushDelaySeconds": 31
}

35 changes: 35 additions & 0 deletions test/standalone/test-config-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

var path = require('path');
var assert = require('assert');

// Default configuration:
// { logLevel: 1, stackTraceLimit: 0, flushDelaySeconds: 30, samplingRate: 10 };

// Fixtures configuration:
// { logLevel: 4, stackTraceLimit: 1 };
process.env.GCLOUD_TRACE_CONFIG = path.join('test', 'fixtures', 'test-config.json');

var agent = require('../..').start();

describe('json config', function() {
it('should load trace config from json file', function() {
var config = agent.private_().config_;
assert.equal(config.logLevel, 4);
});
});
6 changes: 3 additions & 3 deletions test/standalone/test-config-priority.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ var assert = require('assert');
// { logLevel: 1, stackTraceLimit: 0, flushDelaySeconds: 30, samplingRate: 10 };

// Fixtures configuration:
// { logLevel: 2, stackTraceLimit: 1 };
// { logLevel: 4, stackTraceLimit: 1 };
process.env.GCLOUD_TRACE_CONFIG =
path.join(__dirname, '..', 'fixtures', 'test-config.js');

process.env.GCLOUD_TRACE_LOGLEVEL = 4;
process.env.GCLOUD_TRACE_LOGLEVEL = 2;

var agent = require('../..').start({logLevel: 3, stackTraceLimit: 2,
flushDelaySeconds: 31});

describe('should respect config load order', function() {
it('should order Default -> start -> env config -> env specific', function() {
var config = agent.private_().config_;
assert.equal(config.logLevel, 4);
assert.equal(config.logLevel, 2);
assert.equal(config.stackTraceLimit, 1);
assert.equal(config.flushDelaySeconds, 31);
assert.equal(config.samplingRate, 10);
Expand Down
37 changes: 37 additions & 0 deletions test/standalone/test-config-relative-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

var path = require('path');
var assert = require('assert');

// Default configuration:
// { logLevel: 1, stackTraceLimit: 0, flushDelaySeconds: 30, samplingRate: 10 };

// Fixtures configuration:
// { logLevel: 4, stackTraceLimit: 1 };
process.env.GCLOUD_TRACE_CONFIG = path.join('fixtures', 'test-config.js');

process.chdir('test');

var agent = require('../..').start();

describe('relative config', function() {
it('should load trace config from relative path', function() {
var config = agent.private_().config_;
assert.equal(config.logLevel, 4);
});
});

0 comments on commit a160e16

Please sign in to comment.