|
| 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