hawkly tracer for javascript (opentracing)
** Work in progress **
This is a distrubuted tracing library that allows you to instrument your application.
Concepts
Briefly, at the top level we have a Trace
, which is comprised of Spans
. Spans
represent a single
unit of work. Exactly what a span is, is up to you. You want enough detail to understand your application,
but no so many that it's all noise.
To make a Trace
you first create a Span
. If the Span
has no relationship it's automatically set
as the root Span
. When you're ready to start another unit of work, you create another Span
and link
it to the parent Span
.
There are two types of links (also called references):
childOf
where the child Span
depends upon the parent Span
, a blocking function such as a request/response,followsFrom
where the child Span
does not depend unpon the parent Span
, a non blocking function like emitting an event.
Usage
hawkly tracer is built ontop of opentracing
, so all the instrumentation you do is not unqiue to hawkly,
and will work if you switch your tracer to a something else that is opentracing compatible.
In order to record spans
, you need to instiate a tracer.
import Tracer from 'hawkly';
const tracer = new Tracer({
accessToken: 'yourAccessToken',
componentName: 'serviceName/functionName',
sanitiseCallback:(report) => { },
recordCallback: (report) => { },
});
const span = tracer.startSpan('someOperation');
span.tag('key', 'value')
span.log({
event: 'read',
payload: {duration: 1000},
})
span.finish();
Related Spans
At it's most basic this is how to link spans together.
Remember to always .finish()
the Span
at the right time, otherwise the durations for the Spans
will not be accurate.
const parentSpan = tracer.startSpan('parentOperation');
const childSpan = tracer.startSpan('childOperation', {childOf: parentSpan});
childSpan.finish();
parentSpan.finish();
const parentSpan = tracer.startSpan('parentOperation');
const childSpan = tracer.startSpan('childOperation', {followsFrom: parentSpan});
parentSpan.finish();
childSpan.finish();
How to cross a process boundary / Distributed Tracing
In order to maintain a trace between processes we need to inject some information your carrier
on one side,
and extract it into a new Span
on the other side.
The carrier
refers to the message that is being sent between processes. It may be a HTTP request, response,
a message on a message queue, or something else.
At the moment, this tracer implementation only supports carriers that are JSON
objects.
We don't inject the whole Span
into the carrier
, it's way to large. Instead we inject what we call
the span's Context
. This is essentially a bunch of id's that allow us to link the spans together.
const carrier: any = {};
const span: any = tracer.startSpan('someOperation');
tracer.inject(span, 'text_map', carrier);
span.finish();
const carrier = JSON.parse(request);
const childSpan: any = tracer.join('childSpan', carrier, 'text_map');
childSpan.finish();
When you use tracer.join()
to create a new Span
from the Context
in the carrier
;
Typescript
If you're using Typescript you can import the source directly by using the following import:
import {Tracer} from 'hawkly/src';
Global Tracer
You can take advantage of a singleton in the opentracing
module as follows:
import Tracer from 'hawkly';
opentracing.initGlobalTracer(new Tracer());
const tracer = opentracing.globalTracer();
This allows you to initialise the hawkly tracer at the start of your application, and then just
import opentracing
everywhere else. This makes it easy for you to switch tracing implementations
at a later date if you decide.
Also, by default the opentracing
implementation is no-op
, meaning unless you supply it a tracer
with .initGlobalTracer()
it will basically not do anything. This means you can disable tracing if
you wish.
More information
API docs for opentracing
and more information on the javascript implementation can be found here:
https://github.com/opentracing/opentracing-javascript
hawkly.io
Owen Kelly