Skip to content

Commit

Permalink
added span size gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
mamunto committed Nov 20, 2024
1 parent d57d5ab commit b41c887
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ private class BatchWorker: Thread {
var queue: OperationQueue

private var queueSizeGauge: ObservableLongGauge?
private var spanGaugeObserver: ObservableLongGauge?

private var processedSpansCounter: LongCounter
private let droppedAttrs: [String: AttributeValue]
private let exportedAttrs: [String: AttributeValue]
private let spanGaugeBuilder: LongGaugeBuilder
init(
spanExporter: SpanExporter,
meterProvider: StableMeterProvider,
Expand Down Expand Up @@ -123,6 +126,9 @@ private class BatchWorker: Thread {
)
}

self.spanGaugeBuilder = meter.gaugeBuilder(name: "spanSize")
.ofLongs()

processedSpansCounter = meter.counterBuilder(name: "processedSpans")
.setUnit("1")
.setDescription("The number of spans processed by the BatchSpanProcessor. [dropped=true if they were dropped due to high throughput]")
Expand All @@ -137,6 +143,12 @@ private class BatchWorker: Thread {
]
}

deinit {
// Cleanup all gauge observer
self.queueSizeGauge?.close()
self.spanGaugeObserver?.close()
}

func addSpan(span: ReadableSpan) {
cond.lock()
defer { cond.unlock() }
Expand All @@ -145,8 +157,21 @@ private class BatchWorker: Thread {
processedSpansCounter.add(value: 1, attribute: droppedAttrs)
return
}
// TODO: Record a gauge for referenced spans.
spanList.append(span)

// If there is any observer before, let's close it
self.spanGaugeObserver?.close()
// Subscribe to new gauge observer
self.spanGaugeObserver = self.spanGaugeBuilder
.buildWithCallback { [count = spanList.count] result in
result.record(
value: count,
attributes: [
BatchSpanProcessor.SPAN_PROCESSOR_TYPE_LABEL: .string(BatchSpanProcessor.SPAN_PROCESSOR_TYPE_VALUE)
]
)
}

// Notify the worker thread that at half of the queue is available. It will take
// time anyway for the thread to wake up.
if spanList.count >= halfMaxQueueSize {
Expand Down

0 comments on commit b41c887

Please sign in to comment.