Skip to content

Commit

Permalink
Fix #1867 Orderly shutdown in examples (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored Jan 4, 2023
1 parent d5f0c48 commit a343da0
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 24 deletions.
14 changes: 11 additions & 3 deletions examples/batch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace nostd = opentelemetry::nostd;
namespace
{

void initTracer()
void InitTracer()
{
auto exporter = trace_exporter::OStreamSpanExporterFactory::Create();

Expand All @@ -46,6 +46,12 @@ void initTracer()
trace_api::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace_api::Provider::SetTracerProvider(none);
}

nostd::shared_ptr<trace_api::Tracer> get_tracer()
{
auto provider = trace_api::Provider::GetTracerProvider();
Expand All @@ -65,7 +71,7 @@ void StartAndEndSpans()
int main()
{
// Removing this line will leave the default noop TracerProvider in place.
initTracer();
InitTracer();

std::cout << "Creating first batch of " << kNumSpans << " spans and waiting 3 seconds ...\n";
StartAndEndSpans();
Expand All @@ -83,7 +89,9 @@ int main()
StartAndEndSpans();
printf("Shutting down and draining queue.... \n");
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
// We immediately let the program terminate which invokes the processor destructor

// We invoke the processor destructor
// which in turn invokes the processor Shutdown(), which finally drains the queue of ALL
// its spans.
CleanupTracer();
}
3 changes: 2 additions & 1 deletion examples/grpc/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void RunClient(uint16_t port)

int main(int argc, char **argv)
{
initTracer();
InitTracer();
// set global propagator
context::propagation::GlobalTextMapPropagator::SetGlobalPropagator(
opentelemetry::nostd::shared_ptr<context::propagation::TextMapPropagator>(
Expand All @@ -114,5 +114,6 @@ int main(int argc, char **argv)
port = default_port;
}
RunClient(port);
CleanupTracer();
return 0;
}
3 changes: 2 additions & 1 deletion examples/grpc/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void RunServer(uint16_t port)

int main(int argc, char **argv)
{
initTracer();
InitTracer();
constexpr uint16_t default_port = 8800;
uint16_t port;
if (argc > 1)
Expand All @@ -120,5 +120,6 @@ int main(int argc, char **argv)
}

RunServer(port);
CleanupTracer();
return 0;
}
8 changes: 7 additions & 1 deletion examples/grpc/tracer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class GrpcServerCarrier : public opentelemetry::context::propagation::TextMapCar
ServerContext *context_;
};

void initTracer()
void InitTracer()
{
auto exporter = opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create();
auto processor =
Expand All @@ -90,6 +90,12 @@ void initTracer()
new opentelemetry::trace::propagation::HttpTraceContext()));
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
opentelemetry::trace::Provider::SetTracerProvider(none);
}

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> get_tracer(std::string tracer_name)
{
auto provider = opentelemetry::trace::Provider::GetTracerProvider();
Expand Down
3 changes: 2 additions & 1 deletion examples/http/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void sendRequest(const std::string &url)

int main(int argc, char *argv[])
{
initTracer();
InitTracer();
constexpr char default_host[] = "localhost";
constexpr char default_path[] = "/helloworld";
constexpr uint16_t default_port = 8800;
Expand All @@ -92,4 +92,5 @@ int main(int argc, char *argv[])
std::string url = "http://" + std::string(default_host) + ":" + std::to_string(port) +
std::string(default_path);
sendRequest(url);
CleanupTracer();
}
3 changes: 2 additions & 1 deletion examples/http/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class RequestHandler : public HTTP_SERVER_NS::HttpRequestCallback

int main(int argc, char *argv[])
{
initTracer();
InitTracer();

// The port the validation service listens to can be specified via the command line.
if (argc > 1)
Expand All @@ -91,5 +91,6 @@ int main(int argc, char *argv[])
}
http_server.Stop();
root_span->End();
CleanupTracer();
return 0;
}
8 changes: 7 additions & 1 deletion examples/http/tracer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class HttpTextMapCarrier : public opentelemetry::context::propagation::TextMapCa
T headers_;
};

void initTracer()
void InitTracer()
{
auto exporter = opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create();
auto processor =
Expand All @@ -80,6 +80,12 @@ void initTracer()
new opentelemetry::trace::propagation::HttpTraceContext()));
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
opentelemetry::trace::Provider::SetTracerProvider(none);
}

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> get_tracer(std::string tracer_name)
{
auto provider = opentelemetry::trace::Provider::GetTracerProvider();
Expand Down
8 changes: 8 additions & 0 deletions examples/jaeger/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ void InitTracer()
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
} // namespace

int main(int argc, char *argv[])
Expand All @@ -42,4 +48,6 @@ int main(int argc, char *argv[])
InitTracer();

foo_library();

CleanupTracer();
}
12 changes: 10 additions & 2 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace metrics_api = opentelemetry::metrics;
namespace
{

void initMetrics(const std::string &name)
void InitMetrics(const std::string &name)
{
std::unique_ptr<metric_sdk::PushMetricExporter> exporter{
new exportermetrics::OStreamMetricExporter};
Expand Down Expand Up @@ -83,6 +83,12 @@ void initMetrics(const std::string &name)
std::move(histogram_view));
metrics_api::Provider::SetMeterProvider(provider);
}

void CleanupMetrics()
{
std::shared_ptr<metrics_api::MeterProvider> none;
metrics_api::Provider::SetMeterProvider(none);
}
} // namespace

int main(int argc, char **argv)
Expand All @@ -94,7 +100,7 @@ int main(int argc, char **argv)
}

std::string name{"ostream_metric_example"};
initMetrics(name);
InitMetrics(name);

if (example_type == "counter")
{
Expand All @@ -118,4 +124,6 @@ int main(int argc, char **argv)
observable_counter_example.join();
histogram_example.join();
}

CleanupMetrics();
}
12 changes: 10 additions & 2 deletions examples/multi_processor/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace nostd = opentelemetry::nostd;

namespace
{
std::shared_ptr<InMemorySpanData> initTracer()
std::shared_ptr<InMemorySpanData> InitTracer()
{
std::shared_ptr<InMemorySpanData> data;

Expand All @@ -44,6 +44,12 @@ std::shared_ptr<InMemorySpanData> initTracer()
return data;
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace_api::Provider::SetTracerProvider(none);
}

void dumpSpans(std::vector<std::unique_ptr<trace_sdk::SpanData>> &spans)
{
char span_buf[trace_api::SpanId::kSize * 2];
Expand Down Expand Up @@ -79,9 +85,11 @@ void dumpSpans(std::vector<std::unique_ptr<trace_sdk::SpanData>> &spans)
int main()
{
// Removing this line will leave the default noop TracerProvider in place.
std::shared_ptr<InMemorySpanData> data = initTracer();
std::shared_ptr<InMemorySpanData> data = InitTracer();

foo_library();
auto memory_spans = data->GetSpans();
dumpSpans(memory_spans);

CleanupTracer();
}
20 changes: 15 additions & 5 deletions examples/multithreaded/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace nostd = opentelemetry::nostd;

namespace
{
void initTracer()
void InitTracer()
{
auto exporter = opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create();
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
Expand All @@ -28,6 +28,12 @@ void initTracer()
trace_api::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace_api::Provider::SetTracerProvider(none);
}

nostd::shared_ptr<trace_api::Tracer> get_tracer()
{
auto provider = trace_api::Provider::GetTracerProvider();
Expand Down Expand Up @@ -56,10 +62,14 @@ void run_threads()

int main()
{
initTracer();
InitTracer();

auto root_span = get_tracer()->StartSpan(__func__);
trace_api::Scope scope(root_span);
{
auto root_span = get_tracer()->StartSpan(__func__);
trace_api::Scope scope(root_span);

run_threads();
}

run_threads();
CleanupTracer();
}
14 changes: 14 additions & 0 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ void InitTracer()
trace::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}

void InitLogger()
{
// Create OTLP exporter instance
Expand All @@ -50,6 +56,12 @@ void InitLogger()

opentelemetry::logs::Provider::SetLoggerProvider(provider);
}

void CleanupLogger()
{
nostd::shared_ptr<logs::LoggerProvider> none;
opentelemetry::logs::Provider::SetLoggerProvider(none);
}
} // namespace

int main(int argc, char *argv[])
Expand All @@ -66,6 +78,8 @@ int main(int argc, char *argv[])
InitLogger();
InitTracer();
foo_library();
CleanupTracer();
CleanupLogger();
}
#else
int main()
Expand Down
8 changes: 8 additions & 0 deletions examples/otlp/grpc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ void InitTracer()
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
} // namespace

int main(int argc, char *argv[])
Expand All @@ -47,4 +53,6 @@ int main(int argc, char *argv[])
InitTracer();

foo_library();

CleanupTracer();
}
12 changes: 10 additions & 2 deletions examples/otlp/grpc_metric_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace

otlp_exporter::OtlpGrpcMetricExporterOptions options;

void initMetrics()
void InitMetrics()
{
auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(options);

Expand All @@ -47,6 +47,12 @@ void initMetrics()

metrics_api::Provider::SetMeterProvider(provider);
}

void CleanupMetrics()
{
std::shared_ptr<metrics_api::MeterProvider> none;
metrics_api::Provider::SetMeterProvider(none);
}
} // namespace

int main(int argc, char *argv[])
Expand All @@ -66,7 +72,7 @@ int main(int argc, char *argv[])
}
}
// Removing this line will leave the default noop MetricProvider in place.
initMetrics();
InitMetrics();
std::string name{"otlp_grpc_metric_example"};

if (example_type == "counter")
Expand All @@ -91,4 +97,6 @@ int main(int argc, char *argv[])
observable_counter_example.join();
histogram_example.join();
}

CleanupMetrics();
}
Loading

0 comments on commit a343da0

Please sign in to comment.