pinpoint-node-agent
Version:
Pinpoint APM agent for Node.js — distributed tracing, error analysis, and URI statistics
81 lines (65 loc) • 2.16 kB
JavaScript
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const SpanRecorder = require('./span-recorder')
const CallStack = require('./call-stack')
const StackId = require('./stack-id')
class Trace {
/**
* Creates an instance of the Trace class.
*
* @constructor
* @param {SpanBuilder} spanBuilder - The builder for creating spans.
* @param {Repository} repository - The repository for storing trace data.
* @param {SpanRecorder} spanRecorder - The recorder for root span attributes.
* @param {SpanEventRecorderFactory} spanEventRecorderFactory - Factory for creating span event recorders.
*/
constructor(spanBuilder, repository, spanRecorder, spanEventRecorderFactory) {
this.spanBuilder = spanBuilder
this.repository = repository
this.spanRecorder = spanRecorder
this.callStack = new CallStack(spanBuilder.getTraceRoot(), spanEventRecorderFactory)
this.closed = false
}
// DefaultTrace.java: traceBlockEnd
traceBlockBegin(stackId = StackId.default) {
if (this.closed) {
return this.callStack.makeNullSpanEventRecorder()
}
return this.callStack.makeSpanEventRecorder(stackId)
}
traceBlockEnd(spanEventRecorder) {
if (this.closed) {
return
}
this.endSpanEventBuilder(spanEventRecorder.getSpanEventBuilder())
}
endSpanEventBuilder(builder) {
const spanEventBuilder = this.callStack.pop(builder)
spanEventBuilder?.markAfterTime()
this.repository.storeSpanEvent(spanEventBuilder)
}
getSpanRecorder() {
return this.spanRecorder
}
getTraceRoot() {
return this.spanBuilder.getTraceRoot()
}
getTraceId() {
return this.spanBuilder.getTraceRoot().getTraceId()
}
canSampled() {
return true
}
close() {
this.closed = true
const afterTime = Date.now()
this.spanBuilder.markAfterTime(afterTime)
this.repository.storeSpan(this.spanBuilder)
return afterTime
}
}
module.exports = Trace