hypertrace
Advanced tools
Comparing version 1.1.0 to 1.2.0
13
index.js
@@ -28,2 +28,13 @@ const objectState = new Map() | ||
setParent (parentTracer) { | ||
this.parentObject = !parentTracer | ||
? null | ||
: { | ||
className: parentTracer.className, | ||
id: parentTracer.objectId, | ||
props: { ...parentTracer.props }, | ||
ctx: parentTracer.ctx | ||
} | ||
} | ||
trace (...args) { | ||
@@ -91,2 +102,4 @@ const traceFunction = global[traceFunctionSymbol] | ||
trace () { /* noop */ } | ||
setParent () { /* noop */ } | ||
} | ||
@@ -93,0 +106,0 @@ |
{ | ||
"name": "hypertrace", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Add tracing and insights to classes. Supports Prometheus/Grafana.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -144,2 +144,8 @@ # hypertrace | ||
#### .setParent([parentTracer]) | ||
Sets the parent tracer of this one. This means that subsequent calls to `.trace()` will have the new `parentObject`. | ||
If `parentTracer` is null, then the current parent is removed and subsequent calls to `.trace()` will not have a `parentObject`. | ||
#### .enabled | ||
@@ -146,0 +152,0 @@ |
@@ -12,2 +12,6 @@ const { createTracer } = require('../../') | ||
callSetParent (parentTracer) { | ||
this.tracer.setParent(parentTracer) | ||
} | ||
getTracerObjectId () { | ||
@@ -14,0 +18,0 @@ return this.tracer.objectId |
105
test/test.js
@@ -545,1 +545,106 @@ const test = require('brittle') | ||
}) | ||
test('setParent changes the parent of the object', t => { | ||
t.teardown(teardown) | ||
t.plan(2) | ||
let calls = 0 | ||
setTraceFunction(({ object, parentObject }) => { | ||
calls += 1 | ||
if (calls === 1) t.alike(parentObject.props, originalProps) | ||
if (calls === 2) t.alike(parentObject.props, secondaryProps) | ||
}) | ||
class OriginalParent { | ||
constructor () { | ||
this.tracer = createTracer(this, { props: originalProps }) | ||
} | ||
createChild () { | ||
return new Child(this.tracer) | ||
} | ||
} | ||
class SecondaryParent { | ||
constructor () { | ||
this.tracer = createTracer(this, { props: secondaryProps }) | ||
} | ||
} | ||
class Child { | ||
constructor (parentTracer) { | ||
this.tracer = createTracer(this, { parent: parentTracer }) | ||
} | ||
callTrace () { | ||
this.tracer.trace() | ||
} | ||
} | ||
const originalProps = { | ||
some: 'val' | ||
} | ||
const secondaryProps = { | ||
anther: 'val' | ||
} | ||
const originalParent = new OriginalParent() | ||
const secondaryParent = new SecondaryParent() | ||
const child = originalParent.createChild() | ||
child.callTrace() | ||
child.tracer.setParent(secondaryParent.tracer) | ||
child.callTrace() | ||
}) | ||
test('setParent with null, removed the parent', t => { | ||
t.teardown(teardown) | ||
t.plan(2) | ||
let calls = 0 | ||
setTraceFunction(({ object, parentObject }) => { | ||
calls += 1 | ||
if (calls === 1) t.alike(parentObject.props, someProps) | ||
if (calls === 2) t.absent(parentObject) | ||
}) | ||
class Parent { | ||
constructor () { | ||
this.tracer = createTracer(this, { props: someProps }) | ||
} | ||
createChild () { | ||
return new Child(this.tracer) | ||
} | ||
} | ||
class Child { | ||
constructor (parentTracer) { | ||
this.tracer = createTracer(this, { parent: parentTracer }) | ||
} | ||
callTrace () { | ||
this.tracer.trace() | ||
} | ||
} | ||
const someProps = { | ||
some: 'val' | ||
} | ||
const parent = new Parent() | ||
const child = parent.createChild() | ||
child.callTrace() | ||
child.tracer.setParent() | ||
child.callTrace() | ||
}) | ||
test('setParent called when not tracing does not throw', t => { | ||
t.teardown(teardown) | ||
t.plan(1) | ||
const mod1 = new SomeModule() | ||
const mod2 = new SomeModule() | ||
t.execution(() => { | ||
mod2.callSetParent(mod1.tracer) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23171
649
184