diff options
Diffstat (limited to 'src/jaegertracing/opentracing-cpp/mocktracer/include')
7 files changed, 303 insertions, 0 deletions
diff --git a/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/in_memory_recorder.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/in_memory_recorder.h new file mode 100644 index 000000000..31ff4dfdf --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/json.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/json.h new file mode 100644 index 000000000..0d35e90a6 --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/json_recorder.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/json_recorder.h new file mode 100644 index 000000000..fa5ef3c02 --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/recorder.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/recorder.h new file mode 100644 index 000000000..d6b8554ce --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/symbols.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/symbols.h new file mode 100644 index 000000000..9c6362825 --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/tracer.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/tracer.h new file mode 100644 index 000000000..a6d1c5fbd --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/tracer_factory.h b/src/jaegertracing/opentracing-cpp/mocktracer/include/opentracing/mocktracer/tracer_factory.h new file mode 100644 index 000000000..19106ffdc --- /dev/null +++ b/src/jaegertracing/opentracing-cpp/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 |