Skip to content

Commit

Permalink
Adds service name (closes #14) (#20)
Browse files Browse the repository at this point in the history
* Adds service name (closes #14)

* modify environment variable

* generate docs

* update tests with lowercase env var
  • Loading branch information
steinbergkh authored and andyday committed Jul 18, 2017
1 parent 19c6366 commit 4ddf3f6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ Construct a new tracer for internal use only. Use [GlobalTracer#init](#globaltr
- `options.multiEvent` **bool?** true for multi-event mode; otherwise, single-event mode
- `options.debug` **bool?** true for debug; otherwise, it is disabled
- `options.propagators` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [Propagators](#propagators)>?** optional propagators
- `options.serviceName` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** allows the configuration of the "service" tag for the entire Tracer if not
specified here, can also be set using env variable "ctrace_service_name"

### startSpan

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ tracer.init({
}
}
]
}
},
serviceName: "ExampleService" // can set service name for entire tracer
})
```

Expand Down
9 changes: 9 additions & 0 deletions src/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default class Tracer {
* @param {bool} [options.multiEvent] - true for multi-event mode; otherwise, single-event mode
* @param {bool} [options.debug] - true for debug; otherwise, it is disabled
* @param {object.<string, Propagators>} [options.propagators] - optional propagators
* @param {string} [options.serviceName] - allows the configuration of the "service" tag for the entire Tracer if not
* specified here, can also be set using env variable "ctrace_service_name"
*/
constructor (options = {}) {
this._reporter = options.reporter || new Reporter(Encoder, options.stream)
Expand All @@ -50,6 +52,8 @@ export default class Tracer {
this._propagation[key] = (list || []).concat(props[key])
}
}
// Can specify "service" tag for the entire Tracer using options or environment variable "ctrace_service_name"
this.serviceName = options.serviceName || process.env.ctrace_service_name
}

// ---------------------------------------------------------------------- //
Expand Down Expand Up @@ -127,6 +131,11 @@ export default class Tracer {
f.tags = options.tags
}

if (this.serviceName) {
f.tags = f.tags || {}
f.tags["service"] = this.serviceName
}

if (baggage) {
f.baggage = baggage
}
Expand Down
26 changes: 25 additions & 1 deletion test/tracer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,31 @@ describe('tracer', () => {
}), ['timestamp'])
debugEvent2.should.eql({ event: 'Debug Log', level: 'debug', debug: true })
})

describe('with service name', function () {
const serviceName = "TestService"
describe('from environment variable', function () {
beforeEach(() => {
process.env.ctrace_service_name = serviceName
tracer.init({stream})
})
afterEach(() => { delete process.env.ctrace_service_name })
it('tags should include service name', function () {
let span = tracer.startSpan('originating')
let fields = span._fields
fields.tags.service.should.equal(serviceName)
})
})
describe('from tracer options', function () {
beforeEach(() => {
tracer.init({stream, serviceName})
})
it('tags should include service name', function () {
let span = tracer.startSpan('originating')
let fields = span._fields
fields.tags.service.should.equal("TestService")
})
})
})
})

describe('with custom propagators', () => {
Expand Down

0 comments on commit 4ddf3f6

Please sign in to comment.