Skip to content

Commit 5437fb5

Browse files
authored
Merge pull request #5511 from cloudflare/felix/111125-stw-simplify
[o11y][nfc] Deduplicate user tracing observer with internal version
2 parents 91275d4 + 13725c7 commit 5437fb5

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

src/workerd/io/tracer.c++

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,17 @@ void WorkerTracer::setJsRpcInfo(const tracing::InvocationSpanContext& context,
504504
}
505505
}
506506

507+
kj::Own<SpanObserver> UserSpanObserver::newChild() {
508+
return kj::refcounted<UserSpanObserver>(kj::addRef(*submitter), spanId);
509+
}
510+
511+
void UserSpanObserver::report(const Span& span) {
512+
submitter->submitSpan(spanId, parentSpanId, span);
513+
}
514+
515+
// Provide I/O time to the tracing system for user spans.
516+
kj::Date UserSpanObserver::getTime() {
517+
return IoContext::current().now();
518+
}
519+
507520
} // namespace workerd

src/workerd/io/tracer.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,36 @@ class WorkerTracer final: public BaseTracer {
178178

179179
kj::Maybe<kj::Own<tracing::TailStreamWriter>> maybeTailStreamWriter;
180180
};
181+
182+
class SpanSubmitter: public kj::Refcounted {
183+
public:
184+
virtual void submitSpan(tracing::SpanId context, tracing::SpanId spanId, const Span& span) = 0;
185+
virtual tracing::SpanId makeSpanId() = 0;
186+
};
187+
188+
// The user tracing observer
189+
class UserSpanObserver final: public SpanObserver {
190+
public:
191+
// constructor for top-level observer
192+
UserSpanObserver(kj::Own<SpanSubmitter> submitter)
193+
: submitter(kj::mv(submitter)),
194+
spanId(tracing::SpanId::nullId),
195+
parentSpanId(tracing::SpanId::nullId) {}
196+
// constructor for subsequent observers attached to a span
197+
UserSpanObserver(kj::Own<SpanSubmitter> submitter, tracing::SpanId parentSpanId)
198+
: submitter(kj::mv(submitter)),
199+
spanId(this->submitter->makeSpanId()),
200+
parentSpanId(parentSpanId) {}
201+
KJ_DISALLOW_COPY(UserSpanObserver);
202+
203+
kj::Own<SpanObserver> newChild() override;
204+
void report(const Span& span) override;
205+
kj::Date getTime() override;
206+
207+
private:
208+
kj::Own<SpanSubmitter> submitter;
209+
tracing::SpanId spanId;
210+
tracing::SpanId parentSpanId;
211+
};
212+
181213
} // namespace workerd

src/workerd/server/server.c++

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,10 +1699,10 @@ class RequestObserverWithTracer final: public RequestObserver, public WorkerInte
16991699
kj::uint fetchStatus = 0;
17001700
};
17011701

1702-
class SpanSubmitter final: public kj::Refcounted {
1702+
class SequentialSpanSubmitter final: public SpanSubmitter {
17031703
public:
1704-
SpanSubmitter(kj::Own<WorkerTracer> workerTracer): workerTracer(kj::mv(workerTracer)) {}
1705-
void submitSpan(tracing::SpanId spanId, tracing::SpanId parentSpanId, const Span& span) {
1704+
SequentialSpanSubmitter(kj::Own<WorkerTracer> workerTracer): workerTracer(kj::mv(workerTracer)) {}
1705+
void submitSpan(tracing::SpanId spanId, tracing::SpanId parentSpanId, const Span& span) override {
17061706
// We largely recreate the span here which feels inefficient, but is hard to avoid given the
17071707
// mismatch between the Span type and the full span information required for OTel.
17081708
tracing::CompleteSpan span2(
@@ -1718,45 +1718,16 @@ class SpanSubmitter final: public kj::Refcounted {
17181718
workerTracer->addSpan(kj::mv(span2));
17191719
}
17201720

1721-
tracing::SpanId makeSpanId() {
1722-
return tracing::SpanId(predictableSpanId++);
1721+
tracing::SpanId makeSpanId() override {
1722+
return tracing::SpanId(nextSpanId++);
17231723
}
1724-
KJ_DISALLOW_COPY_AND_MOVE(SpanSubmitter);
1724+
KJ_DISALLOW_COPY_AND_MOVE(SequentialSpanSubmitter);
17251725

17261726
private:
1727-
uint64_t predictableSpanId = 0;
1727+
uint64_t nextSpanId = 1;
17281728
kj::Own<WorkerTracer> workerTracer;
17291729
};
17301730

1731-
class WorkerTracerSpanObserver: public SpanObserver {
1732-
public:
1733-
WorkerTracerSpanObserver(
1734-
kj::Own<SpanSubmitter> spanSubmitter, tracing::SpanId parentSpanId = tracing::SpanId::nullId)
1735-
: spanSubmitter(kj::mv(spanSubmitter)),
1736-
spanId(this->spanSubmitter->makeSpanId()),
1737-
parentSpanId(parentSpanId) {}
1738-
1739-
KJ_DISALLOW_COPY_AND_MOVE(WorkerTracerSpanObserver);
1740-
1741-
[[nodiscard]] kj::Own<SpanObserver> newChild() override {
1742-
return kj::refcounted<WorkerTracerSpanObserver>(kj::addRef(*spanSubmitter), spanId);
1743-
}
1744-
1745-
void report(const Span& span) override {
1746-
spanSubmitter->submitSpan(spanId, parentSpanId, span);
1747-
}
1748-
1749-
// Provide I/O time to the tracing system for user spans.
1750-
kj::Date getTime() override {
1751-
return IoContext::current().now();
1752-
}
1753-
1754-
private:
1755-
kj::Own<SpanSubmitter> spanSubmitter;
1756-
tracing::SpanId spanId;
1757-
tracing::SpanId parentSpanId;
1758-
};
1759-
17601731
// IsolateLimitEnforcer that enforces no limits.
17611732
class NullIsolateLimitEnforcer final: public IsolateLimitEnforcer {
17621733
public:
@@ -2201,8 +2172,8 @@ class Server::WorkerService final: public Service,
22012172

22022173
KJ_IF_SOME(w, workerTracer) {
22032174
w->setMakeUserRequestSpanFunc([&w = *w]() {
2204-
return SpanParent(
2205-
kj::refcounted<WorkerTracerSpanObserver>(kj::refcounted<SpanSubmitter>(kj::addRef(w))));
2175+
return SpanParent(kj::refcounted<UserSpanObserver>(
2176+
kj::refcounted<SequentialSpanSubmitter>(kj::addRef(w))));
22062177
});
22072178
}
22082179
kj::Own<RequestObserver> observer =

0 commit comments

Comments
 (0)