-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from ONSdigital/add-tracing
Add opentracing instrumentation to GraphQL API
- Loading branch information
Showing
9 changed files
with
335 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { | ||
initTracerFromEnv, | ||
PrometheusMetricsFactory, | ||
} = require("jaeger-client"); | ||
const promClient = require("prom-client"); | ||
|
||
let tracer; | ||
|
||
const createTracer = logger => { | ||
if (!tracer) { | ||
const config = { | ||
serviceName: process.env.JAEGER_SERVICE_NAME, | ||
}; | ||
|
||
const namespace = config.serviceName; | ||
const metrics = new PrometheusMetricsFactory(promClient, namespace); | ||
|
||
const options = { | ||
tags: { | ||
"eq_author_api.version": process.env.EQ_AUTHOR_API_VERSION, | ||
}, | ||
metrics, | ||
logger, | ||
}; | ||
|
||
tracer = initTracerFromEnv(config, options); | ||
} | ||
|
||
return tracer; | ||
}; | ||
|
||
module.exports = logger => ({ | ||
localTracer: createTracer(logger), | ||
serverTracer: createTracer(logger), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const createTracer = require("./tracer"); | ||
|
||
describe("createTracer", () => { | ||
let logger; | ||
|
||
const SERVICE_NAME = "service_name"; | ||
const API_VERSION = "api_version"; | ||
|
||
beforeEach(() => { | ||
logger = { | ||
info: jest.fn(), | ||
}; | ||
process.env.JAEGER_SERVICE_NAME = SERVICE_NAME; | ||
process.env.EQ_AUTHOR_API_VERSION = API_VERSION; | ||
}); | ||
|
||
afterAll(() => { | ||
delete process.env.JAEGER_SERVICE_NAME; | ||
delete process.env.EQ_AUTHOR_API_VERSION; | ||
}); | ||
|
||
it("should be a function", () => { | ||
expect(createTracer).toEqual(expect.any(Function)); | ||
}); | ||
|
||
it("should return a local tracer", () => { | ||
expect(createTracer(logger).localTracer).toEqual(expect.any(Object)); | ||
}); | ||
|
||
it("should return a server tracer", () => { | ||
expect(createTracer(logger).serverTracer).toEqual(expect.any(Object)); | ||
}); | ||
|
||
it("should reuse the same tracer instance", () => { | ||
expect(createTracer(logger).localTracer).toBe( | ||
createTracer(logger).serverTracer | ||
); | ||
}); | ||
|
||
it("should set the tracer service name", () => { | ||
expect(createTracer(logger).localTracer).toHaveProperty( | ||
"_serviceName", | ||
SERVICE_NAME | ||
); | ||
}); | ||
|
||
it("should tag the trace with API version number", () => { | ||
expect(createTracer(logger).localTracer).toHaveProperty( | ||
"_tags", | ||
expect.objectContaining({ | ||
"eq_author_api.version": API_VERSION, | ||
}) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.