Skip to content

Commit 3cc2c69

Browse files
authored
Add Span API. (open-telemetry#35)
1 parent 573696f commit 3cc2c69

File tree

3 files changed

+240
-11
lines changed

3 files changed

+240
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace opentelemetry
6+
{
7+
namespace trace
8+
{
9+
enum class CanonicalCode : uint8_t
10+
{
11+
/**
12+
* The operation completed successfully.
13+
*/
14+
OK = 0,
15+
16+
/**
17+
* The operation was cancelled (typically by the caller).
18+
*/
19+
CANCELLED = 1,
20+
21+
/**
22+
* Unknown error. An example of where this error may be returned is if a Status value received
23+
* from another address space belongs to an error-space that is not known in this address space.
24+
* Also errors raised by APIs that do not return enough error information may be converted to
25+
* this error.
26+
*/
27+
UNKNOWN = 2,
28+
29+
/**
30+
* Client specified an invalid argument. Note that this differs from FAILED_PRECONDITION.
31+
* INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the
32+
* system (e.g., a malformed file name).
33+
*/
34+
INVALID_ARGUMENT = 3,
35+
36+
/**
37+
* Deadline expired before operation could complete. For operations that change the state of the
38+
* system, this error may be returned even if the operation has completed successfully. For
39+
* example, a successful response from a server could have been delayed long enough for the
40+
* deadline to expire.
41+
*/
42+
DEADLINE_EXCEEDED = 4,
43+
44+
/**
45+
* Some requested entity (e.g., file or directory) was not found.
46+
*/
47+
NOT_FOUND = 5,
48+
49+
/**
50+
* Some entity that we attempted to create (e.g., file or directory) already exists.
51+
*/
52+
ALREADY_EXISTS = 6,
53+
54+
/**
55+
* The caller does not have permission to execute the specified operation. PERMISSION_DENIED
56+
* must not be used for rejections caused by exhausting some resource (use RESOURCE_EXHAUSTED
57+
* instead for those errors). PERMISSION_DENIED must not be used if the caller cannot be
58+
* identified (use UNAUTHENTICATED instead for those errors).
59+
*/
60+
PERMISSION_DENIED = 7,
61+
62+
/**
63+
* Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system
64+
* is out of space.
65+
*/
66+
RESOURCE_EXHAUSTED = 8,
67+
68+
/**
69+
* Operation was rejected because the system is not in a state required for the operation's
70+
* execution. For example, directory to be deleted may be non-empty, an rmdir operation is
71+
* applied to a non-directory, etc.
72+
*
73+
* A litmus test that may help a service implementor in deciding between FAILED_PRECONDITION,
74+
* ABORTED, and UNAVAILABLE: (a) Use UNAVAILABLE if the client can retry just the failing call.
75+
* (b) Use ABORTED if the client should retry at a higher-level (e.g., restarting a
76+
* read-modify-write sequence). (c) Use FAILED_PRECONDITION if the client should not retry until
77+
* the system state has been explicitly fixed. E.g., if an "rmdir" fails because the directory
78+
* is non-empty, FAILED_PRECONDITION should be returned since the client should not retry unless
79+
* they have first fixed up the directory by deleting files from it.
80+
*/
81+
FAILED_PRECONDITION = 9,
82+
83+
/**
84+
* The operation was aborted, typically due to a concurrency issue like sequencer check
85+
* failures, transaction aborts, etc.
86+
*
87+
* See litmus test above for deciding between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE.
88+
*/
89+
ABORTED = 10,
90+
91+
/**
92+
* Operation was attempted past the valid range. E.g., seeking or reading past end of file.
93+
*
94+
* Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed if the system
95+
* state changes. For example, a 32-bit file system will generate INVALID_ARGUMENT if asked to
96+
* read at an offset that is not in the range [0,2^32-1], but it will generate OUT_OF_RANGE if
97+
* asked to read from an offset past the current file size.
98+
*
99+
* There is a fair bit of overlap between FAILED_PRECONDITION and OUT_OF_RANGE. We recommend
100+
* using OUT_OF_RANGE (the more specific error) when it applies so that callers who are
101+
* iterating through a space can easily look for an OUT_OF_RANGE error to detect when they are
102+
* done.
103+
*/
104+
OUT_OF_RANGE = 11,
105+
106+
/**
107+
* Operation is not implemented or not supported/enabled in this service.
108+
*/
109+
UNIMPLEMENTED = 12,
110+
111+
/**
112+
* Internal errors. Means some invariants expected by underlying system has been broken. If you
113+
* see one of these errors, something is very broken.
114+
*/
115+
INTERNAL = 13,
116+
117+
/**
118+
* The service is currently unavailable. This is a most likely a transient condition and may be
119+
* corrected by retrying with a backoff.
120+
*
121+
* See litmus test above for deciding between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE.
122+
*/
123+
UNAVAILABLE = 14,
124+
125+
/**
126+
* Unrecoverable data loss or corruption.
127+
*/
128+
DATA_LOSS = 15,
129+
130+
/**
131+
* The request does not have valid authentication credentials for the operation.
132+
*/
133+
UNAUTHENTICATED = 16,
134+
};
135+
136+
} // namespace trace
137+
} // namespace opentelemetry

api/include/opentelemetry/trace/noop.h

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#pragma once
2+
// Please refer to provider.h for documentation on how to obtain a Tracer object.
3+
//
4+
// This file is part of the internal implementation of OpenTelemetry. Nothing in this file should be
5+
// used directly. Please refer to span.h and tracer.h for documentation on these interfaces.
26

7+
#include "opentelemetry/nostd/string_view.h"
8+
#include "opentelemetry/nostd/unique_ptr.h"
9+
#include "opentelemetry/trace/span.h"
310
#include "opentelemetry/trace/tracer.h"
411

512
#include <memory>
@@ -9,22 +16,34 @@ namespace opentelemetry
916
namespace trace
1017
{
1118
/**
12-
* Noop implementation of Span.
19+
* No-op implementation of Span. This class should not be used directly.
1320
*/
1421
class NoopSpan final : public Span
1522
{
1623
public:
1724
explicit NoopSpan(const std::shared_ptr<Tracer> &tracer) noexcept : tracer_{tracer} {}
1825

19-
// Span
26+
void AddEvent(nostd::string_view name) noexcept override {}
27+
28+
void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override {}
29+
void AddEvent(nostd::string_view name, core::SteadyTimestamp timestamp) noexcept override {}
30+
31+
void SetStatus(CanonicalCode code, nostd::string_view description) noexcept override {}
32+
33+
void UpdateName(nostd::string_view name) noexcept override {}
34+
35+
void End() noexcept override {}
36+
37+
bool IsRecording() const noexcept override { return false; }
38+
2039
Tracer &tracer() const noexcept override { return *tracer_; }
2140

2241
private:
2342
std::shared_ptr<Tracer> tracer_;
2443
};
2544

2645
/**
27-
* Noop implementation of Tracer
46+
* No-op implementation of Tracer.
2847
*/
2948
class NoopTracer final : public Tracer, public std::enable_shared_from_this<NoopTracer>
3049
{

api/include/opentelemetry/trace/span.h

+81-8
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,111 @@
11
#pragma once
22

3+
#include <cstdint>
4+
35
#include "opentelemetry/core/timestamp.h"
6+
#include "opentelemetry/nostd/string_view.h"
7+
#include "opentelemetry/trace/canonical_code.h"
48

59
namespace opentelemetry
610
{
711
namespace trace
812
{
13+
enum class SpanKind
14+
{
15+
kInternal,
16+
kServer,
17+
kClient,
18+
kProducer,
19+
kConsumer,
20+
};
921
/**
10-
* StartSpanOptions provides options to set properties of span at the time of starting a new span.
22+
* StartSpanOptions provides options to set properties of a Span at the time of its creation
1123
*/
1224
struct StartSpanOptions
1325
{
14-
// Sets the start time of a span.
26+
// Optionally sets the start time of a Span.
1527
//
16-
// If the start time of a span is set, timestamps from both the system clock and steady clock
17-
// should be provided.
28+
// If the start time of a Span is set, timestamps from both the system clock and steady clock
29+
// must be provided.
1830
//
19-
// Timestamps from the steady clock can be used to most accurately measure a span's
20-
// duration, while timestamps from the system clock can be used to most accurately place a span's
21-
// time point relative to other spans collected across a distributed system.
31+
// Timestamps from the steady clock can be used to most accurately measure a Span's
32+
// duration, while timestamps from the system clock can be used to most accurately place a Span's
33+
// time point relative to other Spans collected across a distributed system.
2234
core::SystemTimestamp start_system_time;
2335
core::SteadyTimestamp start_steady_time;
36+
37+
// TODO:
38+
// Span(Context?) parent;
39+
// SpanContext remote_parent;
40+
// Links
41+
// Attributes
42+
SpanKind kind = SpanKind::kInternal;
2443
};
2544

2645
class Tracer;
2746

2847
/**
29-
* A span represents a single operation within a trace.
48+
* A Span represents a single operation within a Trace.
3049
*/
3150
class Span
3251
{
3352
public:
53+
// Note that Spans should be created using the Tracer class. Please refer to
54+
// tracer.h for documentation.
55+
Span() = default;
56+
57+
// The Span destructor End()s the Span, if it hasn't been ended already.
3458
virtual ~Span() = default;
3559

60+
// Not copiable or movable.
61+
Span(const Span &) = delete;
62+
Span(Span &&) = delete;
63+
Span &operator=(const Span &) = delete;
64+
Span &operator=(Span &&) = delete;
65+
66+
// TODO
67+
// Sets an attribute on the Span. If the Span previously contained a mapping for
68+
// the key, the old value is replaced.
69+
//
70+
// If an empty string is used as the value, the attribute will be silently
71+
// dropped. Note: this behavior could change in the future.
72+
// virtual void SetAttribute(nostd::string_view key, AttributeValue&& value) = 0;
73+
74+
// Adds an event to the Span.
75+
virtual void AddEvent(nostd::string_view name) noexcept = 0;
76+
77+
// Adds an event to the Span, with a custom timestamp.
78+
virtual void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept = 0;
79+
virtual void AddEvent(nostd::string_view name, core::SteadyTimestamp timestamp) noexcept = 0;
80+
81+
// TODO
82+
// Adds an event to the Span, with a custom timestamp, and attributes.
83+
// virtual void AddEvent(nostd::string_view name, core::SteadyTimestamp
84+
// timestamp, nostd::span<const std::pair<nostd::string_view name, AttributeValue
85+
// value>> attributes) noexcept = 0;
86+
87+
// Sets the status of the span. The default status is OK. Only the value of the last call will be
88+
// recorded, and implementations are free to ignore previous calls.
89+
virtual void SetStatus(CanonicalCode code, nostd::string_view description) noexcept = 0;
90+
91+
// Updates the name of the Span. If used, this will override the name provided
92+
// during creation.
93+
virtual void UpdateName(nostd::string_view name) noexcept = 0;
94+
95+
// Mark the end of the Span. Only the timing of the first End call for a given Span will
96+
// be recorded, and implementations are free to ignore all further calls.
97+
virtual void End() noexcept = 0;
98+
99+
// TODO
100+
// virtual void End(EndSpanOptions&& opts) noexcept = 0;
101+
102+
// TODO
103+
// SpanContext context() const noexcept = 0;
104+
105+
// Returns true if this Span is recording tracing events (e.g. SetAttribute,
106+
// AddEvent).
107+
virtual bool IsRecording() const noexcept = 0;
108+
36109
virtual Tracer &tracer() const noexcept = 0;
37110
};
38111
} // namespace trace

0 commit comments

Comments
 (0)