Skip to content

Commit 71d5b1d

Browse files
authored
Zipkin exporter (#471)
1 parent 7706c6d commit 71d5b1d

File tree

16 files changed

+1049
-10
lines changed

16 files changed

+1049
-10
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [EXPORTER] Added Zipkin Exporter. ([#471](https://github.com/open-telemetry/opentelemetry-cpp/pull/471))
19+
1820
## [0.2.0] 2021-03-02
1921

2022
* [SDK] Added `ForceFlush` to `TracerProvider`. ([#588](https://github.com/open-telemetry/opentelemetry-cpp/pull/588)).

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ if(WITH_STL)
7474
endif()
7575

7676
option(WITH_OTLP "Whether to include the OpenTelemetry Protocol in the SDK" OFF)
77+
option(WITH_ZIPKIN "Whether to include the Zipkin exporter in the SDK" OFF)
7778

7879
option(WITH_PROMETHEUS "Whether to include the Prometheus Client in the SDK"
7980
OFF)

exporters/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2021, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
if(WITH_OTLP)
216
add_subdirectory(otlp)
317
endif()
@@ -9,6 +23,10 @@ if(WITH_PROMETHEUS)
923
add_subdirectory(prometheus)
1024
endif()
1125

26+
if(WITH_ZIPKIN)
27+
add_subdirectory(zipkin)
28+
endif()
29+
1230
if(WITH_ELASTICSEARCH)
1331
add_subdirectory(elasticsearch)
1432
endif()

exporters/zipkin/CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
include_directories(include)
16+
find_package(CURL)
17+
if(CURL_FOUND)
18+
add_definitions(-DWITH_CURL)
19+
endif()
20+
add_library(zipkin_trace_exporter src/zipkin_exporter.cc src/recordable.cc)
21+
if(BUILD_TESTING)
22+
add_executable(zipkin_recordable_test test/zipkin_recordable_test.cc)
23+
24+
target_link_libraries(zipkin_recordable_test ${GTEST_BOTH_LIBRARIES}
25+
${CMAKE_THREAD_LIBS_INIT} zipkin_trace_exporter)
26+
27+
gtest_add_tests(
28+
TARGET zipkin_recordable_test
29+
TEST_PREFIX exporter.
30+
TEST_LIST zipkin_recordable_test)
31+
endif() # BUILD_TESTING
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "nlohmann/json.hpp"
20+
#include "opentelemetry/sdk/trace/recordable.h"
21+
#include "opentelemetry/version.h"
22+
23+
OPENTELEMETRY_BEGIN_NAMESPACE
24+
namespace exporter
25+
{
26+
namespace zipkin
27+
{
28+
using ZipkinSpan = nlohmann::json;
29+
30+
enum class TransportFormat
31+
{
32+
kJson,
33+
kProtobuf
34+
};
35+
36+
class Recordable final : public sdk::trace::Recordable
37+
{
38+
public:
39+
const ZipkinSpan &span() const noexcept { return span_; }
40+
41+
void SetIds(trace::TraceId trace_id,
42+
trace::SpanId span_id,
43+
trace::SpanId parent_span_id) noexcept override;
44+
45+
void SetAttribute(nostd::string_view key,
46+
const opentelemetry::common::AttributeValue &value) noexcept override;
47+
48+
void AddEvent(nostd::string_view name,
49+
core::SystemTimestamp timestamp,
50+
const common::KeyValueIterable &attributes) noexcept override;
51+
52+
void AddLink(const opentelemetry::trace::SpanContext &span_context,
53+
const common::KeyValueIterable &attributes) noexcept override;
54+
55+
void SetStatus(trace::StatusCode code, nostd::string_view description) noexcept override;
56+
57+
void SetName(nostd::string_view name) noexcept override;
58+
59+
void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override;
60+
61+
virtual void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override;
62+
63+
void SetDuration(std::chrono::nanoseconds duration) noexcept override;
64+
65+
private:
66+
ZipkinSpan span_;
67+
};
68+
} // namespace zipkin
69+
} // namespace exporter
70+
OPENTELEMETRY_END_NAMESPACE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "opentelemetry/exporters/zipkin/recordable.h"
20+
#include "opentelemetry/ext/http/client/http_client_factory.h"
21+
#include "opentelemetry/ext/http/common/url_parser.h"
22+
#include "opentelemetry/sdk/trace/exporter.h"
23+
#include "opentelemetry/sdk/trace/span_data.h"
24+
25+
#include "nlohmann/json.hpp"
26+
27+
OPENTELEMETRY_BEGIN_NAMESPACE
28+
namespace exporter
29+
{
30+
namespace zipkin
31+
{
32+
33+
const std::string kZipkinEndpointDefault = "http://localhost:9411/api/v2/spans";
34+
35+
/**
36+
* Struct to hold Zipkin exporter options.
37+
*/
38+
struct ZipkinExporterOptions
39+
{
40+
// The endpoint to export to. By default the OpenTelemetry Collector's default endpoint.
41+
std::string endpoint = kZipkinEndpointDefault;
42+
TransportFormat format = TransportFormat::kJson;
43+
std::string service_name = "default-service";
44+
std::string ipv4;
45+
std::string ipv6;
46+
};
47+
48+
namespace trace_sdk = opentelemetry::sdk::trace;
49+
namespace http_client = opentelemetry::ext::http::client;
50+
51+
/**
52+
* The Zipkin exporter exports span data in JSON format as expected by Zipkin
53+
*/
54+
class ZipkinExporter final : public trace_sdk::SpanExporter
55+
{
56+
public:
57+
/**
58+
* Create a ZipkinExporter using all default options.
59+
*/
60+
ZipkinExporter();
61+
62+
/**
63+
* Create a ZipkinExporter using the given options.
64+
*/
65+
explicit ZipkinExporter(const ZipkinExporterOptions &options);
66+
67+
/**
68+
* Create a span recordable.
69+
* @return a newly initialized Recordable object
70+
*/
71+
std::unique_ptr<trace_sdk::Recordable> MakeRecordable() noexcept override;
72+
73+
/**
74+
* Export a batch of span recordables in JSON format.
75+
* @param spans a span of unique pointers to span recordables
76+
*/
77+
trace_sdk::ExportResult Export(
78+
const nostd::span<std::unique_ptr<trace_sdk::Recordable>> &spans) noexcept override;
79+
80+
/**
81+
* Shut down the exporter.
82+
* @param timeout an optional timeout, default to max.
83+
*/
84+
bool Shutdown(
85+
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override
86+
{
87+
return true;
88+
}
89+
90+
private:
91+
void InitializeLocalEndpoint();
92+
93+
private:
94+
// The configuration options associated with this exporter.
95+
bool isShutdown_ = false;
96+
ZipkinExporterOptions options_;
97+
std::shared_ptr<http_client::HttpClientSync> http_client_;
98+
opentelemetry::ext::http::common::UrlParser url_parser_;
99+
nlohmann::json local_end_point_;
100+
};
101+
} // namespace zipkin
102+
} // namespace exporter
103+
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)