{"id":182,"date":"2023-08-24T11:50:04","date_gmt":"2023-08-24T10:50:04","guid":{"rendered":"https:\/\/mrzebra.co.uk\/code\/?p=182"},"modified":"2023-08-24T11:50:04","modified_gmt":"2023-08-24T10:50:04","slug":"opentelemetry-null-span-exporter","status":"publish","type":"post","link":"https:\/\/zebra-north.com\/code\/2023\/08\/24\/opentelemetry-null-span-exporter\/","title":{"rendered":"OpenTelemetry Null Span Exporter"},"content":{"rendered":"\n<p>The OpenTelemetry C++ implementation ships with several exporters for outputting your recorded traces:  ostream, HTTP, gRPC, and more. It does not, however, provide a null implementation.<\/p>\n\n\n\n<p>A null exporter is useful in test environments, where you just want to discard instrumentation data.  The simple class below provides an implementation of a null span exporter.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-cpp\">#ifndef INCLUDED_NULLSPANEXPORTER_510E043774634B3883865AE2BD56BDBD\n#define INCLUDED_NULLSPANEXPORTER_510E043774634B3883865AE2BD56BDBD\n\n#include &quot;opentelemetry\/sdk\/trace\/exporter.h&quot;\n#include &quot;opentelemetry\/sdk\/trace\/span_data.h&quot;\n#include &lt;chrono&gt;\n#include &lt;memory&gt;\n\n\/**\n * @brief A null exporter to discard telemetry traces during unit tests.\n *\/\nclass NullSpanExporter : public opentelemetry::sdk::trace::SpanExporter\n{\n    \/**\n     * Create a span recordable.\n     *\n     * @return Returns a newly initialized Recordable object.\n     *\/\n    virtual std::unique_ptr&lt;opentelemetry::sdk::trace::Recordable&gt; MakeRecordable() noexcept override\n    {\n        return std::make_unique&lt;opentelemetry::sdk::trace::SpanData&gt;();\n    }\n\n    \/**\n     * Exports a batch of span recordables.\n     *\n     * @param spans Returns a span of unique pointers to span recordables.\n     *\/\n    virtual opentelemetry::sdk::common::ExportResult Export(const opentelemetry::nostd::span&lt;std::unique_ptr&lt;opentelemetry::sdk::trace::Recordable&gt;&gt;&amp; spans) noexcept override\n    {\n        (void)spans;\n        return opentelemetry::sdk::common::ExportResult::kSuccess;\n    }\n\n    \/**\n     * Shut down the exporter.\n     *\n     * @param timeout An optional timeout.\n     *\n     * @return Returns the status of the operation.\n     *\/\n    virtual bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override\n    {\n        (void)timeout;\n        return true;\n    }\n};\n\n#endif\n<\/code><\/pre>\n\n\n\n<p>To use this exporter, simply create a <code>NullSpanExporter<\/code> instead of whatever exporter you are currently using:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-cpp\">#include &quot;NullSpanExporter.h&quot;\n\n#include &quot;opentelemetry\/context\/runtime_context.h&quot;\n#include &quot;opentelemetry\/sdk\/trace\/exporter.h&quot;\n#include &quot;opentelemetry\/sdk\/trace\/processor.h&quot;\n#include &quot;opentelemetry\/sdk\/trace\/simple_processor_factory.h&quot;\n#include &quot;opentelemetry\/sdk\/trace\/tracer_provider_factory.h&quot;\n#include &quot;opentelemetry\/trace\/provider.h&quot;\n\nusing opentelemetry::context::RuntimeContext;\nusing opentelemetry::context::Token;\nusing opentelemetry::sdk::trace::SimpleSpanProcessorFactory;\nusing opentelemetry::sdk::trace::TracerProviderFactory;\nusing opentelemetry::trace::Provider;\nusing opentelemetry::trace::TracerProvider;\n\n#include &lt;memory&gt;\n\nvoid initializeTracing()\n{\n    \/\/ Create a null exporter that discards traces.\n    auto exporter = std::make_unique&lt;NullSpanExporter&gt;();\n\n    \/\/ Processing done on the span prior to exporting.\n    auto processor = SimpleSpanProcessorFactory::Create(std::move(exporter));\n\n    \/\/ Create the TracerProvider, which is is a factory to create the Tracer.\n    std::shared_ptr&lt;TracerProvider&gt; provider = TracerProviderFactory::Create(std::move(processor));\n\n    \/\/ Set the global trace provider.\n    Provider::SetTracerProvider(provider);\n\n    \/\/ Create the tracer.\n    tracer = provider-&gt;GetTracer(&quot;price-worker&quot;);\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The OpenTelemetry C++ implementation ships with several exporters for outputting your recorded traces: ostream, HTTP, gRPC, and more. It does not, however, provide a null implementation. A null exporter is useful in test environments, where you just want to discard instrumentation data. The simple class below provides an implementation of a null span exporter.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[30],"class_list":["post-182","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-opentelemetry"],"_links":{"self":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":1,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/182\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}