nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
tracing.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_TRACING_H_
2#define NANOMMO_SERVER_TRACING_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <string>
9#include <string_view>
10
11#include "absl/status/status.h"
12#include "absl/status/statusor.h"
13#include "absl/time/clock.h"
14#include "absl/time/time.h"
15#include "prometheus/registry.h"
16
17namespace nanommo {
18
19/// Where a span is: the trace it's part of, by its 16 random bytes, and the
20/// span itself, by its 8. A span id of zero is no span.
22 uint64_t trace_high = 0;
23 uint64_t trace_low = 0;
24 uint64_t span = 0;
25
26 bool valid() const { return span != 0; }
27 /// The trace's id, as 32 hex digits, as Tempo shows it.
28 std::string trace_id() const;
29
30 bool operator==(const SpanContext&) const = default;
31};
32
33/// The context as W3C Trace Context's traceparent writes it:
34/// 00-<trace id>-<span id>-01.
35std::string Traceparent(const SpanContext& context);
36
37/// The context a traceparent names, or an InvalidArgument error for one that
38/// isn't written as the standard says, or names no trace or no span.
39absl::StatusOr<SpanContext> ParseTraceparent(std::string_view traceparent);
40
41/// What a span's work is, numbered as OpenTelemetry numbers them: work inside
42/// a process, a server's answer to a request, a client's call to another
43/// service, and a message put on a queue, and taken off it.
44enum class SpanKind {
50};
51
52/// A piece of a trace's work, from when it starts to when it ends, or goes out
53/// of scope. While it's open, it's its thread's current span, which the next
54/// span started on the thread is part of. A span started while tracing is off
55/// records nothing.
56class Span {
57 public:
58 /// What a span records, which only tracing.cc knows.
59 struct Data;
60
61 /// A span that records nothing.
63 explicit Span(std::unique_ptr<Data> data);
64 ~Span();
65
66 /// The span it was, which the other doesn't record any more.
67 Span(Span&& other) noexcept;
68 Span(const Span&) = delete;
69 Span& operator=(const Span&) = delete;
70
71 bool recording() const { return data_ != nullptr; }
72 /// No trace and no span when it isn't recording.
73 SpanContext context() const;
74
75 void SetAttribute(std::string_view key, std::string_view value);
76 void SetAttribute(std::string_view key, int64_t value);
77 /// Marks it as failed, and why.
78 void SetError(std::string_view message);
79 /// Another trace's span that this one's work was for too, as a batch's span
80 /// is for each message in it, and has one parent.
81 void AddLink(const SpanContext& other);
82
83 /// Records it, and makes the span that was current before it current again.
84 /// Going out of scope does the same.
85 void End();
86
87 private:
88 std::unique_ptr<Data> data_;
89 SpanContext previous_;
90};
91
92/// Sends a batch of spans, written as OTLP's JSON, and says whether it went.
93using SendSpans = std::function<absl::Status(const std::string& json)>;
94
95/// Posts each batch to an OpenTelemetry collector at the endpoint, such as
96/// http://alloy:4318, at /v1/traces.
97SendSpans PostTo(const std::string& endpoint);
98
99/// The most spans that wait to be sent. A span that ends while that many are
100/// waiting is dropped.
101inline constexpr std::size_t kMostWaitingSpans = 4096;
102
103/// Turns tracing on for this process: from now on, its spans are the
104/// service's, and a thread of their own sends them, once a second, or as
105/// soon as there's a batch of them. With a registry, it counts the spans it
106/// drops there. Tracing is off until this is called, so tests, and a server
107/// run without a collector, record nothing.
108void StartTracing(std::string service, SendSpans send,
109 prometheus::Registry* registry = nullptr);
110
111/// Sends the spans that are left, within two seconds, drops the rest, and turns
112/// tracing off. Call it once the threads that start spans have stopped.
113void StopTracing();
114
115/// A span under the thread's current span, or the first of a new trace when
116/// there's none.
117Span StartSpan(std::string_view name, SpanKind kind);
118
119/// A span under one from another process, or the first of a new trace when
120/// the parent's no span, from start: a request's work can start before we
121/// know which trace it's part of.
122Span StartSpan(std::string_view name, SpanKind kind, const SpanContext& parent,
123 absl::Time start = absl::Now());
124
125/// " trace_id=" and the trace of the thread's current span, for the end of a
126/// log line, which Grafana links to the trace, or nothing when there's none.
127std::string TraceField();
128
129/// Whether the thread has a current span: whether a call made now is made for
130/// a traced request. The tick's calls aren't, and stop at this check.
131bool InTrace();
132
133/// A span under the thread's current span, or one that records nothing when
134/// there's none. For calls to Redis and MySQL, which are traced when they're
135/// made for a traced request, and not when the tick makes them.
136Span StartChildSpan(std::string_view name, SpanKind kind);
137
138} // namespace nanommo
139
140#endif // NANOMMO_SERVER_TRACING_H_
Span()
A span that records nothing.
void SetAttribute(std::string_view key, std::string_view value)
Definition tracing.cc:314
Span(const Span &)=delete
void SetError(std::string_view message)
Marks it as failed, and why.
Definition tracing.cc:326
void AddLink(const SpanContext &other)
Definition tracing.cc:333
SpanContext context() const
No trace and no span when it isn't recording.
Definition tracing.cc:310
Span & operator=(const Span &)=delete
Span(Span &&other) noexcept
The span it was, which the other doesn't record any more.
bool recording() const
Definition tracing.h:71
Definition chat.cc:9
std::string Traceparent(const SpanContext &context)
Definition tracing.cc:269
Span StartSpan(std::string_view name, SpanKind kind)
Definition tracing.cc:389
void StopTracing()
Definition tracing.cc:387
bool InTrace()
Definition tracing.cc:402
std::function< absl::Status(const std::string &json)> SendSpans
Sends a batch of spans, written as OTLP's JSON, and says whether it went.
Definition tracing.h:93
SendSpans PostTo(const std::string &endpoint)
Definition tracing.cc:351
void StartTracing(std::string service, SendSpans send, prometheus::Registry *registry)
Definition tracing.cc:371
absl::StatusOr< SpanContext > ParseTraceparent(std::string_view traceparent)
Definition tracing.cc:274
std::string TraceField()
Definition tracing.cc:398
Span StartChildSpan(std::string_view name, SpanKind kind)
Definition tracing.cc:404
constexpr std::size_t kMostWaitingSpans
Definition tracing.h:101
bool operator==(const SpanContext &) const =default
uint64_t trace_low
Definition tracing.h:23
bool valid() const
Definition tracing.h:26
uint64_t trace_high
Definition tracing.h:22
std::string trace_id() const
The trace's id, as 32 hex digits, as Tempo shows it.
Definition tracing.cc:265