@@ -4,6 +4,8 @@ const { resolve } = require("./resolve");
4
4
const { Cache } = require ( "./Cache" ) ;
5
5
const isPlainObject = require ( "./is-plain-object" ) ;
6
6
7
+ const Tracer = require ( "./tracing/Tracer" ) ;
8
+
7
9
/**
8
10
* Applies a reducer from an accumulator object.
9
11
*
@@ -13,7 +15,7 @@ const isPlainObject = require("./is-plain-object");
13
15
*/
14
16
async function resolveFromAccumulator ( acc , reducer ) {
15
17
const parsedReducers = createReducer ( reducer ) ;
16
- return resolve ( acc , parsedReducers ) ;
18
+ return resolve ( acc , parsedReducers , true ) ;
17
19
}
18
20
19
21
/**
@@ -25,8 +27,8 @@ async function resolveFromAccumulator(acc, reducer) {
25
27
* @param {Cache|undefined } options.cache cache manager, see Cache for options.
26
28
* @param {Object|undefined } options.locals persistent object that is
27
29
* 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.
30
32
* @returns {Promise<any> } resolved value
31
33
*/
32
34
async function resolveFromInput ( input , reducer , options = { } ) {
@@ -55,29 +57,22 @@ function validateLocals(locals) {
55
57
}
56
58
57
59
/**
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`.
62
63
*/
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" ) ;
69
68
}
70
69
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" ) ;
75
72
}
76
73
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" ) ;
81
76
}
82
77
}
83
78
}
@@ -100,18 +95,24 @@ class DataPoint {
100
95
* @param {Object } options
101
96
* @param {Object|undefined } options.locals persistent object that is
102
97
* 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.
105
100
* @returns {Promise<any> } result from running the input thru the
106
101
* provided reducer.
107
102
*/
108
103
async resolve ( input , reducer , options = { } ) {
109
104
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
+ }
111
111
112
112
return resolveFromInput ( input , reducer , {
113
113
...options ,
114
- cache : this . cache
114
+ cache : this . cache ,
115
+ tracer
115
116
} ) ;
116
117
}
117
118
}
@@ -121,5 +122,5 @@ module.exports = {
121
122
resolveFromInput,
122
123
DataPoint,
123
124
validateLocals,
124
- validateTracingSpan
125
+ validateTracer
125
126
} ;
0 commit comments