Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-point): create tracing api #417

Merged
merged 2 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions documentation/docs/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const DataPoint = require("@data-point/core");
const DPModel = require("@data-point/core/model");
const DPIfThenElse = require("@data-point/core/ifThenElse");
const DPMap = require("@data-point/core/map");
const DPTracer = require("@data-point/tracer");

const fs = require("fs");

Expand Down Expand Up @@ -70,34 +69,9 @@ async function main() {
}
];

const tracer = new DPTracer();

const span = tracer.startSpan("data-point-request");

console.time("dp1");
result = await datapoint.resolve(input, DPMap(myModel), {
// tracer: span
});
result = await datapoint.resolve(input, DPMap(myModel));
console.timeEnd("dp1");

span.finish();

console.time("dp2");
result = await datapoint.resolve(input, DPMap(myModel), {
// tracer: span
});
console.timeEnd("dp2");

// span2.finish();

// fs.writeFileSync(
// "/Users/pacheca/Downloads/tracing.json",
// JSON.stringify(tracer.report("chrome-tracing"))
// );

console.log(tracer.report("chrome-tracing"));
console.log(store);
console.log(result);
}

main();
1 change: 0 additions & 1 deletion packages/data-point-tracer/index.js

This file was deleted.

24 changes: 0 additions & 24 deletions packages/data-point-tracer/package.json

This file was deleted.

52 changes: 0 additions & 52 deletions packages/data-point-tracer/src/Span.js

This file was deleted.

30 changes: 0 additions & 30 deletions packages/data-point-tracer/src/Tracer.js

This file was deleted.

49 changes: 0 additions & 49 deletions packages/data-point-tracer/src/reporters.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/data-point/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-point/core",
"version": "3.4.3",
"version": "6.0.0",
"description": "Data Processing and Transformation Utility",
"main": "index.js",
"private": true,
Expand Down
53 changes: 27 additions & 26 deletions packages/data-point/src/DataPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { resolve } = require("./resolve");
const { Cache } = require("./Cache");
const isPlainObject = require("./is-plain-object");

const Tracer = require("./tracing/Tracer");

/**
* Applies a reducer from an accumulator object.
*
Expand All @@ -13,7 +15,7 @@ const isPlainObject = require("./is-plain-object");
*/
async function resolveFromAccumulator(acc, reducer) {
const parsedReducers = createReducer(reducer);
return resolve(acc, parsedReducers);
return resolve(acc, parsedReducers, true);
}

/**
Expand All @@ -25,8 +27,8 @@ async function resolveFromAccumulator(acc, reducer) {
* @param {Cache|undefined} options.cache cache manager, see Cache for options.
* @param {Object|undefined} options.locals persistent object that is
* accessible via the Accumulator object on every reducer.
* @param {OpenTrace.Span|undefined} options.tracer when provided it should
* comply with the **opentracing** Span API.
* @param {Tracer} options.tracer when provided it should
* comply with the DataPoint Tracing API.
* @returns {Promise<any>} resolved value
*/
async function resolveFromInput(input, reducer, options = {}) {
Expand Down Expand Up @@ -55,29 +57,22 @@ function validateLocals(locals) {
}

/**
* @param {OpenTrace.Span} span when provided it should
* comply with the **opentracing** Span API.
* @throws Error if the object does not expose the methods `startSpan`,
* `setTag`, `log`.
* @param {Tracer} options.tracer when provided it should
* comply with the DataPoint Tracing API.
* @throws Error if the object does not expose the methods `start`.
*/
function validateTracingSpan(span) {
if (span) {
if (typeof span.startSpan !== "function") {
throw new Error(
"tracer.startSpan must be a function, tracer expects opentracing API (see https://opentracing.io)"
);
function validateTracer(tracer) {
if (tracer) {
if (typeof tracer.start !== "function") {
throw new Error("tracer.start must be a function");
}

if (typeof span.setTag !== "function") {
throw new Error(
"tracer.setTag must be a function, tracer expects opentracing API (see https://opentracing.io)"
);
if (tracer.error && typeof tracer.error !== "function") {
throw new Error("tracer.error must be a function");
}

if (typeof span.log !== "function") {
throw new Error(
"tracer.log must be a function, tracer expects opentracing API (see https://opentracing.io)"
);
if (tracer.finish && typeof tracer.finish !== "function") {
throw new Error("tracer.finish must be a function");
}
}
}
Expand All @@ -100,18 +95,24 @@ class DataPoint {
* @param {Object} options
* @param {Object|undefined} options.locals persistent object that is
* accessible via the Accumulator object on every reducer.
* @param {OpenTrace.Span|undefined} options.tracer when provided it should
* comply with the **opentracing** Span API.
* @param {Tracer} options.tracer when provided it should
* comply with the DataPoint Tracing API.
* @returns {Promise<any>} result from running the input thru the
* provided reducer.
*/
async resolve(input, reducer, options = {}) {
validateLocals(options.locals);
validateTracingSpan(options.tracer);
validateTracer(options.tracer);

let tracer;
if (options.tracer) {
tracer = new Tracer(options.tracer);
}

return resolveFromInput(input, reducer, {
...options,
cache: this.cache
cache: this.cache,
tracer
});
}
}
Expand All @@ -121,5 +122,5 @@ module.exports = {
resolveFromInput,
DataPoint,
validateLocals,
validateTracingSpan
validateTracer
};
41 changes: 17 additions & 24 deletions packages/data-point/src/DataPoint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const sayHello = value => `Hello ${value}`;
const getAccumulator = (input, acc) => acc;

const shallowTracer = {
// resolve will call this method, which expects a Span object in return
startSpan: () => shallowTracer,
setTag: () => true,
log: () => true
start: () => shallowTracer,
error: () => true,
finish: () => true
};

describe("resolveFromAccumulator", () => {
Expand All @@ -34,8 +33,7 @@ describe("resolveFromInput", () => {
cache: {
get: () => true,
set: () => true
},
tracer: "tracer"
}
};
it("should pass locals object", async () => {
const result = await dataPoint.resolveFromInput(
Expand Down Expand Up @@ -113,40 +111,35 @@ describe("validateLocals", () => {
});
});

describe("validateTracingSpan", () => {
describe("validateTracer", () => {
it("should only allow undefined or well defined tracer span API", () => {
expect(() => {
dataPoint.validateTracingSpan();
dataPoint.validateTracer();
}).not.toThrow();

expect(() => {
dataPoint.validateTracingSpan(shallowTracer);
dataPoint.validateTracer(shallowTracer);
}).not.toThrow();
});

it("should throw error on any un-valid value", () => {
expect(() => {
dataPoint.validateTracingSpan({});
}).toThrowErrorMatchingInlineSnapshot(
`"tracer.startSpan must be a function, tracer expects opentracing API (see https://opentracing.io)"`
);
dataPoint.validateTracer({});
}).toThrowErrorMatchingInlineSnapshot(`"tracer.start must be a function"`);

expect(() => {
dataPoint.validateTracingSpan({
startSpan: () => true
dataPoint.validateTracer({
start: () => true,
error: "invalid"
});
}).toThrowErrorMatchingInlineSnapshot(
`"tracer.setTag must be a function, tracer expects opentracing API (see https://opentracing.io)"`
);
}).toThrowErrorMatchingInlineSnapshot(`"tracer.error must be a function"`);

expect(() => {
dataPoint.validateTracingSpan({
startSpan: () => true,
setTag: () => true
dataPoint.validateTracer({
start: () => true,
finish: "invalid"
});
}).toThrowErrorMatchingInlineSnapshot(
`"tracer.log must be a function, tracer expects opentracing API (see https://opentracing.io)"`
);
}).toThrowErrorMatchingInlineSnapshot(`"tracer.finish must be a function"`);
});
});

Expand Down
Loading