Skip to content

Commit 11b31d3

Browse files
committed
feat(data-point): create tracing api
via this api we are now tracing implementation agnostic closes ViacomInc#416
1 parent dc81612 commit 11b31d3

21 files changed

+457
-415
lines changed

documentation/docs/playground.js

+1-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const DataPoint = require("@data-point/core");
44
const DPModel = require("@data-point/core/model");
55
const DPIfThenElse = require("@data-point/core/ifThenElse");
66
const DPMap = require("@data-point/core/map");
7-
const DPTracer = require("@data-point/tracer");
87

98
const fs = require("fs");
109

@@ -70,34 +69,9 @@ async function main() {
7069
}
7170
];
7271

73-
const tracer = new DPTracer();
74-
75-
const span = tracer.startSpan("data-point-request");
76-
7772
console.time("dp1");
78-
result = await datapoint.resolve(input, DPMap(myModel), {
79-
// tracer: span
80-
});
73+
result = await datapoint.resolve(input, DPMap(myModel));
8174
console.timeEnd("dp1");
82-
83-
span.finish();
84-
85-
console.time("dp2");
86-
result = await datapoint.resolve(input, DPMap(myModel), {
87-
// tracer: span
88-
});
89-
console.timeEnd("dp2");
90-
91-
// span2.finish();
92-
93-
// fs.writeFileSync(
94-
// "/Users/pacheca/Downloads/tracing.json",
95-
// JSON.stringify(tracer.report("chrome-tracing"))
96-
// );
97-
98-
console.log(tracer.report("chrome-tracing"));
99-
console.log(store);
100-
console.log(result);
10175
}
10276

10377
main();

packages/data-point-tracer/index.js

-1
This file was deleted.

packages/data-point-tracer/package.json

-24
This file was deleted.

packages/data-point-tracer/src/Span.js

-52
This file was deleted.

packages/data-point-tracer/src/Tracer.js

-30
This file was deleted.

packages/data-point-tracer/src/reporters.js

-49
This file was deleted.

packages/data-point/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@data-point/core",
3-
"version": "3.4.3",
3+
"version": "6.0.0",
44
"description": "Data Processing and Transformation Utility",
55
"main": "index.js",
66
"private": true,

packages/data-point/src/DataPoint.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const { resolve } = require("./resolve");
44
const { Cache } = require("./Cache");
55
const isPlainObject = require("./is-plain-object");
66

7+
const Tracer = require("./tracing/Tracer");
8+
79
/**
810
* Applies a reducer from an accumulator object.
911
*
@@ -13,7 +15,7 @@ const isPlainObject = require("./is-plain-object");
1315
*/
1416
async function resolveFromAccumulator(acc, reducer) {
1517
const parsedReducers = createReducer(reducer);
16-
return resolve(acc, parsedReducers);
18+
return resolve(acc, parsedReducers, true);
1719
}
1820

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

5759
/**
58-
* @param {OpenTrace.Span} span when provided it should
59-
* comply with the **opentracing** Span API.
60-
* @throws Error if the object does not expose the methods `startSpan`,
61-
* `setTag`, `log`.
60+
* @param {Tracer} options.tracer when provided it should
61+
* comply with the DataPoint Tracing API.
62+
* @throws Error if the object does not expose the methods `start`.
6263
*/
63-
function validateTracingSpan(span) {
64-
if (span) {
65-
if (typeof span.startSpan !== "function") {
66-
throw new Error(
67-
"tracer.startSpan must be a function, tracer expects opentracing API (see https://opentracing.io)"
68-
);
64+
function validateTracer(tracer) {
65+
if (tracer) {
66+
if (typeof tracer.start !== "function") {
67+
throw new Error("tracer.start must be a function");
6968
}
7069

71-
if (typeof span.setTag !== "function") {
72-
throw new Error(
73-
"tracer.setTag must be a function, tracer expects opentracing API (see https://opentracing.io)"
74-
);
70+
if (tracer.error && typeof tracer.error !== "function") {
71+
throw new Error("tracer.error must be a function");
7572
}
7673

77-
if (typeof span.log !== "function") {
78-
throw new Error(
79-
"tracer.log must be a function, tracer expects opentracing API (see https://opentracing.io)"
80-
);
74+
if (tracer.finish && typeof tracer.finish !== "function") {
75+
throw new Error("tracer.finish must be a function");
8176
}
8277
}
8378
}
@@ -100,18 +95,24 @@ class DataPoint {
10095
* @param {Object} options
10196
* @param {Object|undefined} options.locals persistent object that is
10297
* accessible via the Accumulator object on every reducer.
103-
* @param {OpenTrace.Span|undefined} options.tracer when provided it should
104-
* comply with the **opentracing** Span API.
98+
* @param {Tracer} options.tracer when provided it should
99+
* comply with the DataPoint Tracing API.
105100
* @returns {Promise<any>} result from running the input thru the
106101
* provided reducer.
107102
*/
108103
async resolve(input, reducer, options = {}) {
109104
validateLocals(options.locals);
110-
validateTracingSpan(options.tracer);
105+
validateTracer(options.tracer);
106+
107+
let tracer;
108+
if (options.tracer) {
109+
tracer = new Tracer(options.tracer);
110+
}
111111

112112
return resolveFromInput(input, reducer, {
113113
...options,
114-
cache: this.cache
114+
cache: this.cache,
115+
tracer
115116
});
116117
}
117118
}
@@ -121,5 +122,5 @@ module.exports = {
121122
resolveFromInput,
122123
DataPoint,
123124
validateLocals,
124-
validateTracingSpan
125+
validateTracer
125126
};

packages/data-point/src/DataPoint.test.js

+17-24
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ const sayHello = value => `Hello ${value}`;
66
const getAccumulator = (input, acc) => acc;
77

88
const shallowTracer = {
9-
// resolve will call this method, which expects a Span object in return
10-
startSpan: () => shallowTracer,
11-
setTag: () => true,
12-
log: () => true
9+
start: () => shallowTracer,
10+
error: () => true,
11+
finish: () => true
1312
};
1413

1514
describe("resolveFromAccumulator", () => {
@@ -34,8 +33,7 @@ describe("resolveFromInput", () => {
3433
cache: {
3534
get: () => true,
3635
set: () => true
37-
},
38-
tracer: "tracer"
36+
}
3937
};
4038
it("should pass locals object", async () => {
4139
const result = await dataPoint.resolveFromInput(
@@ -113,40 +111,35 @@ describe("validateLocals", () => {
113111
});
114112
});
115113

116-
describe("validateTracingSpan", () => {
114+
describe("validateTracer", () => {
117115
it("should only allow undefined or well defined tracer span API", () => {
118116
expect(() => {
119-
dataPoint.validateTracingSpan();
117+
dataPoint.validateTracer();
120118
}).not.toThrow();
121119

122120
expect(() => {
123-
dataPoint.validateTracingSpan(shallowTracer);
121+
dataPoint.validateTracer(shallowTracer);
124122
}).not.toThrow();
125123
});
126124

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

134130
expect(() => {
135-
dataPoint.validateTracingSpan({
136-
startSpan: () => true
131+
dataPoint.validateTracer({
132+
start: () => true,
133+
error: "invalid"
137134
});
138-
}).toThrowErrorMatchingInlineSnapshot(
139-
`"tracer.setTag must be a function, tracer expects opentracing API (see https://opentracing.io)"`
140-
);
135+
}).toThrowErrorMatchingInlineSnapshot(`"tracer.error must be a function"`);
141136

142137
expect(() => {
143-
dataPoint.validateTracingSpan({
144-
startSpan: () => true,
145-
setTag: () => true
138+
dataPoint.validateTracer({
139+
start: () => true,
140+
finish: "invalid"
146141
});
147-
}).toThrowErrorMatchingInlineSnapshot(
148-
`"tracer.log must be a function, tracer expects opentracing API (see https://opentracing.io)"`
149-
);
142+
}).toThrowErrorMatchingInlineSnapshot(`"tracer.finish must be a function"`);
150143
});
151144
});
152145

0 commit comments

Comments
 (0)