|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <memory> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include "opentelemetry/exporters/ostream/console_log_record_builder.h" |
| 10 | +#include "opentelemetry/exporters/ostream/console_push_metric_builder.h" |
| 11 | +#include "opentelemetry/exporters/ostream/console_span_builder.h" |
| 12 | +#include "opentelemetry/sdk/common/global_log_handler.h" |
| 13 | +#include "opentelemetry/sdk/configuration/configuration.h" |
| 14 | +#include "opentelemetry/sdk/configuration/configured_sdk.h" |
| 15 | +#include "opentelemetry/sdk/configuration/registry.h" |
| 16 | +#include "opentelemetry/sdk/configuration/yaml_configuration_parser.h" |
| 17 | + |
| 18 | +#ifdef LATER |
| 19 | +# include "custom_log_record_exporter_builder.h" |
| 20 | +# include "custom_log_record_processor_builder.h" |
| 21 | +# include "custom_pull_metric_exporter_builder.h" |
| 22 | +# include "custom_push_metric_exporter_builder.h" |
| 23 | +# include "custom_sampler_builder.h" |
| 24 | +# include "custom_span_exporter_builder.h" |
| 25 | +# include "custom_span_processor_builder.h" |
| 26 | +#endif |
| 27 | + |
| 28 | +#ifdef BAZEL_BUILD |
| 29 | +# include "examples/common/logs_foo_library/foo_library.h" |
| 30 | +# include "examples/common/metrics_foo_library/foo_library.h" |
| 31 | +#else |
| 32 | +# include "logs_foo_library/foo_library.h" |
| 33 | +# include "metrics_foo_library/foo_library.h" |
| 34 | +#endif |
| 35 | + |
| 36 | +#ifdef OTEL_HAVE_OTLP_HTTP |
| 37 | +# include "opentelemetry/exporters/otlp/otlp_http_log_record_builder.h" |
| 38 | +# include "opentelemetry/exporters/otlp/otlp_http_push_metric_builder.h" |
| 39 | +# include "opentelemetry/exporters/otlp/otlp_http_span_builder.h" |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifdef OTEL_HAVE_OTLP_GRPC |
| 43 | +# include "opentelemetry/exporters/otlp/otlp_grpc_log_record_builder.h" |
| 44 | +# include "opentelemetry/exporters/otlp/otlp_grpc_push_metric_builder.h" |
| 45 | +# include "opentelemetry/exporters/otlp/otlp_grpc_span_builder.h" |
| 46 | +#endif |
| 47 | + |
| 48 | +#ifdef OTEL_HAVE_OTLP_FILE |
| 49 | +# include "opentelemetry/exporters/otlp/otlp_file_log_record_builder.h" |
| 50 | +# include "opentelemetry/exporters/otlp/otlp_file_push_metric_builder.h" |
| 51 | +# include "opentelemetry/exporters/otlp/otlp_file_span_builder.h" |
| 52 | +#endif |
| 53 | + |
| 54 | +#ifdef OTEL_HAVE_ZIPKIN |
| 55 | +# include "opentelemetry/exporters/zipkin/zipkin_builder.h" |
| 56 | +#endif |
| 57 | + |
| 58 | +#ifdef OTEL_HAVE_PROMETHEUS |
| 59 | +# include "opentelemetry/exporters/prometheus/prometheus_pull_builder.h" |
| 60 | +#endif |
| 61 | + |
| 62 | +static bool opt_help = false; |
| 63 | +static std::string yaml_file_path = ""; |
| 64 | + |
| 65 | +static std::unique_ptr<opentelemetry::sdk::configuration::ConfiguredSdk> sdk; |
| 66 | + |
| 67 | +namespace |
| 68 | +{ |
| 69 | + |
| 70 | +class CheckerLogHandler : public opentelemetry::sdk::common::internal_log::LogHandler |
| 71 | +{ |
| 72 | +public: |
| 73 | + void Handle(opentelemetry::sdk::common::internal_log::LogLevel level, |
| 74 | + const char * /* file */, |
| 75 | + int /* line */, |
| 76 | + const char *msg, |
| 77 | + const opentelemetry::sdk::common::AttributeMap & /* attributes */) noexcept override |
| 78 | + { |
| 79 | + if (msg == nullptr) |
| 80 | + { |
| 81 | + msg = "<no msg>"; |
| 82 | + } |
| 83 | + switch (level) |
| 84 | + { |
| 85 | + case opentelemetry::sdk::common::internal_log::LogLevel::None: |
| 86 | + break; |
| 87 | + case opentelemetry::sdk::common::internal_log::LogLevel::Error: |
| 88 | + fprintf(stdout, "[ERROR] %s\n", msg); |
| 89 | + break; |
| 90 | + case opentelemetry::sdk::common::internal_log::LogLevel::Warning: |
| 91 | + fprintf(stdout, "[WARNING] %s\n", msg); |
| 92 | + break; |
| 93 | + case opentelemetry::sdk::common::internal_log::LogLevel::Info: |
| 94 | + fprintf(stdout, "[INFO] %s\n", msg); |
| 95 | + break; |
| 96 | + case opentelemetry::sdk::common::internal_log::LogLevel::Debug: |
| 97 | + fprintf(stdout, "[DEBUG] %s\n", msg); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +void InitOtel(const std::string &config_file) |
| 104 | +{ |
| 105 | + auto level = opentelemetry::sdk::common::internal_log::LogLevel::Info; |
| 106 | + |
| 107 | + opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(level); |
| 108 | + |
| 109 | + opentelemetry::nostd::shared_ptr<opentelemetry::sdk::common::internal_log::LogHandler> |
| 110 | + checker_log_handler(new CheckerLogHandler()); |
| 111 | + |
| 112 | + opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(checker_log_handler); |
| 113 | + |
| 114 | + /* 1 - Create a registry */ |
| 115 | + |
| 116 | + std::shared_ptr<opentelemetry::sdk::configuration::Registry> registry( |
| 117 | + new opentelemetry::sdk::configuration::Registry); |
| 118 | + |
| 119 | + /* 2 - Populate the registry with the core components supported */ |
| 120 | + |
| 121 | + opentelemetry::exporter::trace::ConsoleSpanBuilder::Register(registry.get()); |
| 122 | + opentelemetry::exporter::metrics::ConsolePushMetricBuilder::Register(registry.get()); |
| 123 | + opentelemetry::exporter::logs::ConsoleLogRecordBuilder::Register(registry.get()); |
| 124 | + |
| 125 | +#ifdef OTEL_HAVE_OTLP_HTTP |
| 126 | + opentelemetry::exporter::otlp::OtlpHttpSpanBuilder::Register(registry.get()); |
| 127 | + opentelemetry::exporter::otlp::OtlpHttpPushMetricBuilder::Register(registry.get()); |
| 128 | + opentelemetry::exporter::otlp::OtlpHttpLogRecordBuilder::Register(registry.get()); |
| 129 | +#endif |
| 130 | + |
| 131 | +#ifdef OTEL_HAVE_OTLP_GRPC |
| 132 | + opentelemetry::exporter::otlp::OtlpGrpcSpanBuilder::Register(registry.get()); |
| 133 | + opentelemetry::exporter::otlp::OtlpGrpcPushMetricBuilder::Register(registry.get()); |
| 134 | + opentelemetry::exporter::otlp::OtlpGrpcLogRecordBuilder::Register(registry.get()); |
| 135 | +#endif |
| 136 | + |
| 137 | +#ifdef OTEL_HAVE_OTLP_FILE |
| 138 | + opentelemetry::exporter::otlp::OtlpFileSpanBuilder::Register(registry.get()); |
| 139 | + opentelemetry::exporter::otlp::OtlpFilePushMetricBuilder::Register(registry.get()); |
| 140 | + opentelemetry::exporter::otlp::OtlpFileLogRecordBuilder::Register(registry.get()); |
| 141 | +#endif |
| 142 | + |
| 143 | +#ifdef OTEL_HAVE_ZIPKIN |
| 144 | + opentelemetry::exporter::zipkin::ZipkinBuilder::Register(registry.get()); |
| 145 | +#endif |
| 146 | + |
| 147 | +#ifdef OTEL_HAVE_PROMETHEUS |
| 148 | + opentelemetry::exporter::metrics::PrometheusPullBuilder::Register(registry.get()); |
| 149 | +#endif |
| 150 | + |
| 151 | + /* 3 - Populate the registry with external extensions plugins */ |
| 152 | + |
| 153 | +#ifdef LATER |
| 154 | + CustomSamplerBuilder::Register(registry.get()); |
| 155 | + CustomSpanExporterBuilder::Register(registry.get()); |
| 156 | + CustomSpanProcessorBuilder::Register(registry.get()); |
| 157 | + CustomPushMetricExporterBuilder::Register(registry.get()); |
| 158 | + CustomPullMetricExporterBuilder::Register(registry.get()); |
| 159 | + CustomLogRecordExporterBuilder::Register(registry.get()); |
| 160 | + CustomLogRecordProcessorBuilder::Register(registry.get()); |
| 161 | +#endif |
| 162 | + |
| 163 | + /* 4 - Parse a config.yaml */ |
| 164 | + |
| 165 | + // See |
| 166 | + // https://github.com/open-telemetry/opentelemetry-configuration/blob/main/examples/kitchen-sink.yaml |
| 167 | + auto model = opentelemetry::sdk::configuration::YamlConfigurationParser::ParseFile(config_file); |
| 168 | + |
| 169 | + if (model != nullptr) |
| 170 | + { |
| 171 | + fprintf(stdout, "MODEL PARSED\n"); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + fprintf(stdout, "FAILED TO PARSE MODEL\n"); |
| 176 | + exit(1); |
| 177 | + } |
| 178 | + |
| 179 | + /* 5 - Build the SDK from the parsed config.yaml */ |
| 180 | + |
| 181 | + sdk = opentelemetry::sdk::configuration::ConfiguredSdk::Create(registry, model); |
| 182 | + |
| 183 | + if (model != nullptr) |
| 184 | + { |
| 185 | + fprintf(stdout, "SDK CREATED\n"); |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + fprintf(stdout, "FAILED TO CREATE SDK\n"); |
| 190 | + exit(2); |
| 191 | + } |
| 192 | + |
| 193 | + /* 6 - Deploy the SDK */ |
| 194 | + |
| 195 | + if (sdk != nullptr) |
| 196 | + { |
| 197 | + sdk->Install(); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +void CleanupOtel() |
| 202 | +{ |
| 203 | + if (sdk != nullptr) |
| 204 | + { |
| 205 | + sdk->UnInstall(); |
| 206 | + } |
| 207 | + sdk.reset(nullptr); |
| 208 | +} |
| 209 | +} // namespace |
| 210 | + |
| 211 | +static void usage(FILE *out) |
| 212 | +{ |
| 213 | + static const char *msg = |
| 214 | + "Usage: example_yaml [options]\n" |
| 215 | + "Valid options are:\n" |
| 216 | + " --help Print this help\n" |
| 217 | + " --yaml Path to a yaml configuration file\n" |
| 218 | + "\n" |
| 219 | + "The configuration file used will be:\n" |
| 220 | + " 1) the file provided in the command line\n" |
| 221 | + " 2) the file provided in environment variable ${OTEL_EXPERIMENTAL_CONFIG_FILE}\n" |
| 222 | + " 3) file config.yaml\n"; |
| 223 | + |
| 224 | + fprintf(out, "%s", msg); |
| 225 | +} |
| 226 | + |
| 227 | +static int parse_args(int argc, char *argv[]) |
| 228 | +{ |
| 229 | + int remaining_argc = argc; |
| 230 | + char **remaining_argv = argv; |
| 231 | + |
| 232 | + while (remaining_argc > 0) |
| 233 | + { |
| 234 | + if (strcmp(*remaining_argv, "--help") == 0) |
| 235 | + { |
| 236 | + opt_help = true; |
| 237 | + return 0; |
| 238 | + } |
| 239 | + |
| 240 | + if (remaining_argc >= 2) |
| 241 | + { |
| 242 | + if (strcmp(*remaining_argv, "--yaml") == 0) |
| 243 | + { |
| 244 | + remaining_argc--; |
| 245 | + remaining_argv++; |
| 246 | + yaml_file_path = *remaining_argv; |
| 247 | + remaining_argc--; |
| 248 | + remaining_argv++; |
| 249 | + continue; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + if (remaining_argc) |
| 254 | + { |
| 255 | + // Unknown option |
| 256 | + return 1; |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + return 0; |
| 261 | +} |
| 262 | + |
| 263 | +int main(int argc, char *argv[]) |
| 264 | +{ |
| 265 | + // Program name |
| 266 | + argc--; |
| 267 | + argv++; |
| 268 | + |
| 269 | + int rc = parse_args(argc, argv); |
| 270 | + |
| 271 | + if (rc != 0) |
| 272 | + { |
| 273 | + usage(stderr); |
| 274 | + return 1; |
| 275 | + } |
| 276 | + |
| 277 | + if (opt_help) |
| 278 | + { |
| 279 | + usage(stdout); |
| 280 | + return 0; |
| 281 | + } |
| 282 | + |
| 283 | + InitOtel(yaml_file_path); |
| 284 | + |
| 285 | + foo_library(); |
| 286 | + foo_library::counter_example("yaml"); |
| 287 | + foo_library::observable_counter_example("yaml"); |
| 288 | + foo_library::histogram_example("yaml"); |
| 289 | + |
| 290 | + CleanupOtel(); |
| 291 | + return 0; |
| 292 | +} |
0 commit comments