From b196c6498d22e47bb9d0da0153068ec54eac7956 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 11:44:33 +0200 Subject: Adding upstream version 1.6.0. Signed-off-by: Daniel Baumann --- .../opentracing/mocktracer/in_memory_recorder.h | 34 +++++++++ mocktracer/include/opentracing/mocktracer/json.h | 18 +++++ .../include/opentracing/mocktracer/json_recorder.h | 35 +++++++++ .../include/opentracing/mocktracer/recorder.h | 87 ++++++++++++++++++++++ .../include/opentracing/mocktracer/symbols.h | 21 ++++++ mocktracer/include/opentracing/mocktracer/tracer.h | 86 +++++++++++++++++++++ .../opentracing/mocktracer/tracer_factory.h | 22 ++++++ 7 files changed, 303 insertions(+) create mode 100644 mocktracer/include/opentracing/mocktracer/in_memory_recorder.h create mode 100644 mocktracer/include/opentracing/mocktracer/json.h create mode 100644 mocktracer/include/opentracing/mocktracer/json_recorder.h create mode 100644 mocktracer/include/opentracing/mocktracer/recorder.h create mode 100644 mocktracer/include/opentracing/mocktracer/symbols.h create mode 100644 mocktracer/include/opentracing/mocktracer/tracer.h create mode 100644 mocktracer/include/opentracing/mocktracer/tracer_factory.h (limited to 'mocktracer/include') 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 +#include +#include +#include + +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 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 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 +#include +#include + +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& 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 +#include +#include +#include +#include +#include + +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&& out); + + void RecordSpan(SpanData&& span_data) noexcept override; + + void Close() noexcept override; + + private: + std::mutex mutex_; + std::unique_ptr out_; + std::vector 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 +#include + +#include +#include +#include + +namespace opentracing { +BEGIN_OPENTRACING_ABI_NAMESPACE +namespace mocktracer { +struct SpanContextData { + uint64_t trace_id; + uint64_t span_id; + std::map 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 references; + std::string operation_name; + SystemTime start_timestamp; + SteadyClock::duration duration; + std::map tags; + std::vector 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 + +#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 +#include +#include +#include +#include +#include + +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; + + // 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 { + public: + explicit MockTracer(MockTracerOptions&& options); + + std::unique_ptr StartSpanWithOptions( + string_view operation_name, const StartSpanOptions& options) const + noexcept override; + + void Close() noexcept override; + + const std::vector& spans() const noexcept { return spans_; } + + using Tracer::Extract; + using Tracer::Inject; + + expected Inject(const SpanContext& sc, + std::ostream& writer) const override; + + expected Inject(const SpanContext& sc, + const TextMapWriter& writer) const override; + + expected Inject(const SpanContext& sc, + const HTTPHeadersWriter& writer) const override; + + expected> Extract( + std::istream& reader) const override; + + expected> Extract( + const TextMapReader& reader) const override; + + expected> Extract( + const HTTPHeadersReader& reader) const override; + + private: + std::unique_ptr recorder_; + PropagationOptions propagation_options_; + std::mutex mutex_; + std::vector 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 +#include + +namespace opentracing { +BEGIN_OPENTRACING_ABI_NAMESPACE +namespace mocktracer { + +class OPENTRACING_MOCK_TRACER_API MockTracerFactory : public TracerFactory { + public: + expected> 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 -- cgit v1.2.3