summaryrefslogtreecommitdiffstats
path: root/mocktracer/include
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:44:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:44:33 +0000
commitb196c6498d22e47bb9d0da0153068ec54eac7956 (patch)
tree1a994a492581e93224a7ee6455f5d4e9d2ec8e59 /mocktracer/include
parentInitial commit. (diff)
downloadopentracing-cpp-b196c6498d22e47bb9d0da0153068ec54eac7956.tar.xz
opentracing-cpp-b196c6498d22e47bb9d0da0153068ec54eac7956.zip
Adding upstream version 1.6.0.upstream/1.6.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mocktracer/include')
-rw-r--r--mocktracer/include/opentracing/mocktracer/in_memory_recorder.h34
-rw-r--r--mocktracer/include/opentracing/mocktracer/json.h18
-rw-r--r--mocktracer/include/opentracing/mocktracer/json_recorder.h35
-rw-r--r--mocktracer/include/opentracing/mocktracer/recorder.h87
-rw-r--r--mocktracer/include/opentracing/mocktracer/symbols.h21
-rw-r--r--mocktracer/include/opentracing/mocktracer/tracer.h86
-rw-r--r--mocktracer/include/opentracing/mocktracer/tracer_factory.h22
7 files changed, 303 insertions, 0 deletions
diff --git a/mocktracer/include/opentracing/mocktracer/in_memory_recorder.h b/mocktracer/include/opentracing/mocktracer/in_memory_recorder.h
new file mode 100644
index 0000000..31ff4df
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/in_memory_recorder.h
@@ -0,0 +1,34 @@
+#ifndef OPENTRACING_MOCKTRACER_IN_MEMORY_RECORDER_H
+#define OPENTRACING_MOCKTRACER_IN_MEMORY_RECORDER_H
+
+#include <opentracing/mocktracer/recorder.h>
+#include <opentracing/mocktracer/symbols.h>
+#include <mutex>
+#include <vector>
+
+namespace opentracing {
+BEGIN_OPENTRACING_ABI_NAMESPACE
+namespace mocktracer {
+// InMemoryRecorder stores finished spans and provides accessors to them.
+class OPENTRACING_MOCK_TRACER_API InMemoryRecorder : public Recorder {
+ public:
+ void RecordSpan(SpanData&& span_data) noexcept override;
+
+ // Returns a vector of all finished spans.
+ std::vector<SpanData> spans() const;
+
+ // Returns the number of finished spans.
+ size_t size() const;
+
+ // Returns the last finished span. Throws if no spans have been finished.
+ SpanData top() const;
+
+ private:
+ mutable std::mutex mutex_;
+ std::vector<SpanData> spans_;
+};
+} // namespace mocktracer
+END_OPENTRACING_ABI_NAMESPACE
+} // namespace opentracing
+
+#endif // OPENTRACING_MOCKTRACER_IN_MEMORY_RECORDER_H
diff --git a/mocktracer/include/opentracing/mocktracer/json.h b/mocktracer/include/opentracing/mocktracer/json.h
new file mode 100644
index 0000000..0d35e90
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/json.h
@@ -0,0 +1,18 @@
+#ifndef OPENTRACING_MOCKTRACER_JSON_H
+#define OPENTRACING_MOCKTRACER_JSON_H
+
+#include <opentracing/mocktracer/recorder.h>
+#include <opentracing/mocktracer/symbols.h>
+#include <vector>
+
+namespace opentracing {
+BEGIN_OPENTRACING_ABI_NAMESPACE
+namespace mocktracer {
+// Serialize provided spans to JSON.
+OPENTRACING_MOCK_TRACER_API void ToJson(std::ostream& writer,
+ const std::vector<SpanData>& spans);
+} // namespace mocktracer
+END_OPENTRACING_ABI_NAMESPACE
+} // namespace opentracing
+
+#endif // OPENTRACING_MOCKTRACER_JSON_H
diff --git a/mocktracer/include/opentracing/mocktracer/json_recorder.h b/mocktracer/include/opentracing/mocktracer/json_recorder.h
new file mode 100644
index 0000000..fa5ef3c
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/json_recorder.h
@@ -0,0 +1,35 @@
+#ifndef OPENTRACING_MOCKTRACER_JSON_RECORDER_H
+#define OPENTRACING_MOCKTRACER_JSON_RECORDER_H
+
+#include <opentracing/mocktracer/recorder.h>
+#include <opentracing/mocktracer/symbols.h>
+#include <iosfwd>
+#include <memory>
+#include <mutex>
+#include <vector>
+
+namespace opentracing {
+BEGIN_OPENTRACING_ABI_NAMESPACE
+namespace mocktracer {
+// JsonRecorder serializes finished spans to a provided std::ostream in a JSON
+// format.
+//
+// See also FromJson.
+class OPENTRACING_MOCK_TRACER_API JsonRecorder : public Recorder {
+ public:
+ explicit JsonRecorder(std::unique_ptr<std::ostream>&& out);
+
+ void RecordSpan(SpanData&& span_data) noexcept override;
+
+ void Close() noexcept override;
+
+ private:
+ std::mutex mutex_;
+ std::unique_ptr<std::ostream> out_;
+ std::vector<SpanData> spans_;
+};
+} // namespace mocktracer
+END_OPENTRACING_ABI_NAMESPACE
+} // namespace opentracing
+
+#endif // OPENTRACING_MOCKTRACER_JSON_RECORDER_H
diff --git a/mocktracer/include/opentracing/mocktracer/recorder.h b/mocktracer/include/opentracing/mocktracer/recorder.h
new file mode 100644
index 0000000..d6b8554
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/recorder.h
@@ -0,0 +1,87 @@
+#ifndef OPENTRACING_MOCKTRACER_RECORDER_H
+#define OPENTRACING_MOCKTRACER_RECORDER_H
+
+#include <opentracing/mocktracer/symbols.h>
+#include <opentracing/tracer.h>
+
+#include <cstdint>
+#include <iosfwd>
+#include <map>
+
+namespace opentracing {
+BEGIN_OPENTRACING_ABI_NAMESPACE
+namespace mocktracer {
+struct SpanContextData {
+ uint64_t trace_id;
+ uint64_t span_id;
+ std::map<std::string, std::string> baggage;
+};
+
+inline bool operator==(const SpanContextData& lhs, const SpanContextData& rhs) {
+ return lhs.trace_id == rhs.trace_id && lhs.span_id == rhs.span_id &&
+ lhs.baggage == rhs.baggage;
+}
+
+inline bool operator!=(const SpanContextData& lhs, const SpanContextData& rhs) {
+ return !(lhs == rhs);
+}
+
+std::ostream& operator<<(std::ostream& out,
+ const SpanContextData& span_context_data);
+
+struct SpanReferenceData {
+ SpanReferenceType reference_type;
+ uint64_t trace_id;
+ uint64_t span_id;
+};
+
+inline bool operator==(const SpanReferenceData& lhs,
+ const SpanReferenceData& rhs) {
+ return lhs.reference_type == rhs.reference_type &&
+ lhs.trace_id == rhs.trace_id && lhs.span_id == rhs.span_id;
+}
+
+inline bool operator!=(const SpanReferenceData& lhs,
+ const SpanReferenceData& rhs) {
+ return !(lhs == rhs);
+}
+
+struct SpanData {
+ SpanContextData span_context;
+ std::vector<SpanReferenceData> references;
+ std::string operation_name;
+ SystemTime start_timestamp;
+ SteadyClock::duration duration;
+ std::map<std::string, Value> tags;
+ std::vector<LogRecord> logs;
+};
+
+inline bool operator==(const SpanData& lhs, const SpanData& rhs) {
+ return lhs.span_context == rhs.span_context &&
+ lhs.references == rhs.references &&
+ lhs.operation_name == rhs.operation_name &&
+ lhs.start_timestamp == rhs.start_timestamp &&
+ lhs.duration == rhs.duration && lhs.tags == rhs.tags &&
+ lhs.logs == rhs.logs;
+}
+
+inline bool operator!=(const SpanData& lhs, const SpanData& rhs) {
+ return !(lhs == rhs);
+}
+
+std::ostream& operator<<(std::ostream& out, const SpanData& span_data);
+
+class OPENTRACING_MOCK_TRACER_API Recorder {
+ public:
+ virtual ~Recorder() = default;
+
+ virtual void RecordSpan(SpanData&& span_data) noexcept = 0;
+
+ virtual void Close() noexcept {}
+};
+
+} // namespace mocktracer
+END_OPENTRACING_ABI_NAMESPACE
+} // namespace opentracing
+
+#endif // OPENTRACING_MOCKTRACER_RECORDER_H
diff --git a/mocktracer/include/opentracing/mocktracer/symbols.h b/mocktracer/include/opentracing/mocktracer/symbols.h
new file mode 100644
index 0000000..9c63628
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/symbols.h
@@ -0,0 +1,21 @@
+#ifndef OPENTRACING_MOCK_TRACER_SYMBOLS_H
+#define OPENTRACING_MOCK_TRACER_SYMBOLS_H
+
+#include <opentracing/config.h>
+
+#ifdef _MSC_VER
+// Export if this is our own source, otherwise import:
+#ifndef OPENTRACING_MOCK_TRACER_STATIC
+#ifdef OPENTRACING_MOCK_TRACER_EXPORTS
+#define OPENTRACING_MOCK_TRACER_API __declspec(dllexport)
+#else
+#define OPENTRACING_MOCK_TRACER_API __declspec(dllimport)
+#endif
+#endif
+#endif // _MSC_VER
+
+#ifndef OPENTRACING_MOCK_TRACER_API
+#define OPENTRACING_MOCK_TRACER_API
+#endif
+
+#endif // OPENTRACING_SYMBOLS_H
diff --git a/mocktracer/include/opentracing/mocktracer/tracer.h b/mocktracer/include/opentracing/mocktracer/tracer.h
new file mode 100644
index 0000000..a6d1c5f
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/tracer.h
@@ -0,0 +1,86 @@
+#ifndef OPENTRACING_MOCKTRACER_TRACER_H
+#define OPENTRACING_MOCKTRACER_TRACER_H
+
+#include <opentracing/mocktracer/recorder.h>
+#include <opentracing/mocktracer/symbols.h>
+#include <opentracing/tracer.h>
+#include <map>
+#include <memory>
+#include <mutex>
+
+namespace opentracing {
+BEGIN_OPENTRACING_ABI_NAMESPACE
+namespace mocktracer {
+
+struct PropagationOptions {
+ // Specifies what key to use when injecting and extracting span context.
+ std::string propagation_key = "x-ot-span-context";
+
+ // If inject_error_code is non-zero, MockTracer::Inject fails with
+ // inject_error_code.
+ std::error_code inject_error_code;
+
+ // If extract_error_code is non-zero, MockTracer::Extract fails with
+ // extract_error_code.
+ std::error_code extract_error_code;
+};
+
+struct MockTracerOptions {
+ // Recorder is sent spans when they are finished. If nullptr, all finished
+ // spans are dropped.
+ std::unique_ptr<Recorder> recorder;
+
+ // PropagationOptions allows you to customize how the mocktracer's SpanContext
+ // is propagated.
+ PropagationOptions propagation_options;
+};
+
+// MockTracer provides implements the OpenTracing Tracer API. It provides
+// convenient access to finished spans in such a way as to support testing.
+class OPENTRACING_MOCK_TRACER_API MockTracer
+ : public Tracer,
+ public std::enable_shared_from_this<MockTracer> {
+ public:
+ explicit MockTracer(MockTracerOptions&& options);
+
+ std::unique_ptr<Span> StartSpanWithOptions(
+ string_view operation_name, const StartSpanOptions& options) const
+ noexcept override;
+
+ void Close() noexcept override;
+
+ const std::vector<SpanData>& spans() const noexcept { return spans_; }
+
+ using Tracer::Extract;
+ using Tracer::Inject;
+
+ expected<void> Inject(const SpanContext& sc,
+ std::ostream& writer) const override;
+
+ expected<void> Inject(const SpanContext& sc,
+ const TextMapWriter& writer) const override;
+
+ expected<void> Inject(const SpanContext& sc,
+ const HTTPHeadersWriter& writer) const override;
+
+ expected<std::unique_ptr<SpanContext>> Extract(
+ std::istream& reader) const override;
+
+ expected<std::unique_ptr<SpanContext>> Extract(
+ const TextMapReader& reader) const override;
+
+ expected<std::unique_ptr<SpanContext>> Extract(
+ const HTTPHeadersReader& reader) const override;
+
+ private:
+ std::unique_ptr<Recorder> recorder_;
+ PropagationOptions propagation_options_;
+ std::mutex mutex_;
+ std::vector<SpanData> spans_;
+};
+
+} // namespace mocktracer
+END_OPENTRACING_ABI_NAMESPACE
+} // namespace opentracing
+
+#endif // OPENTRACING_MOCKTRACER_TRACER_H
diff --git a/mocktracer/include/opentracing/mocktracer/tracer_factory.h b/mocktracer/include/opentracing/mocktracer/tracer_factory.h
new file mode 100644
index 0000000..19106ff
--- /dev/null
+++ b/mocktracer/include/opentracing/mocktracer/tracer_factory.h
@@ -0,0 +1,22 @@
+#ifndef OPENTRACING_MOCKTRACER_TRACER_FACTORY_H
+#define OPENTRACING_MOCKTRACER_TRACER_FACTORY_H
+
+#include <opentracing/mocktracer/symbols.h>
+#include <opentracing/tracer_factory.h>
+
+namespace opentracing {
+BEGIN_OPENTRACING_ABI_NAMESPACE
+namespace mocktracer {
+
+class OPENTRACING_MOCK_TRACER_API MockTracerFactory : public TracerFactory {
+ public:
+ expected<std::shared_ptr<Tracer>> MakeTracer(const char* configuration,
+ std::string& error_message) const
+ noexcept override;
+};
+
+} // namespace mocktracer
+END_OPENTRACING_ABI_NAMESPACE
+} // namespace opentracing
+
+#endif // OPENTRACING_MOCKTRACER_TRACER_FACTORY_H