Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed May 9, 2024
1 parent 2999936 commit c2a2ef4
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 167 deletions.
46 changes: 23 additions & 23 deletions examples/otlp/file_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h"
#include "opentelemetry/logs/provider.h"
#include "opentelemetry/sdk/logs/exporter.h"
#include "opentelemetry/sdk/logs/logger_provider.h"
#include "opentelemetry/sdk/logs/logger_provider_factory.h"
#include "opentelemetry/sdk/logs/processor.h"
#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"

// sdk::TracerProvider and sdk::LoggerProvider is just used to call ForceFlush and prevent to cancel
// running exportings when destroy and shutdown exporters.It's optional to users.
#include "opentelemetry/sdk/logs/logger_provider.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"

#include <iostream>
#include <string>
#include <utility>
Expand All @@ -43,52 +40,55 @@ namespace
{
opentelemetry::exporter::otlp::OtlpFileExporterOptions opts;
opentelemetry::exporter::otlp::OtlpFileLogRecordExporterOptions log_opts;

std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> tracer_provider;
std::shared_ptr<opentelemetry::sdk::logs::LoggerProvider> logger_provider;

void InitTracer()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpFileExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
auto exporter = otlp::OtlpFileExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));

// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
std::shared_ptr<opentelemetry::trace::TracerProvider> api_provider = tracer_provider;
trace::Provider::SetTracerProvider(api_provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
if (tracer_provider)
{
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
tracer_provider->ForceFlush();
}

tracer_provider.reset();
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}

void InitLogger()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpFileLogRecordExporterFactory::Create(log_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
nostd::shared_ptr<logs::LoggerProvider> provider(
logs_sdk::LoggerProviderFactory::Create(std::move(processor)));
auto exporter = otlp::OtlpFileLogRecordExporterFactory::Create(log_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor));

opentelemetry::logs::Provider::SetLoggerProvider(provider);
std::shared_ptr<opentelemetry::logs::LoggerProvider> api_provider = logger_provider;
opentelemetry::logs::Provider::SetLoggerProvider(api_provider);
}

void CleanupLogger()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<logs::LoggerProvider> provider =
logs::Provider::GetLoggerProvider();
if (provider)
if (logger_provider)
{
static_cast<logs_sdk::LoggerProvider *>(provider.get())->ForceFlush();
logger_provider->ForceFlush();
}

logger_provider.reset();
nostd::shared_ptr<logs::LoggerProvider> none;
opentelemetry::logs::Provider::SetLoggerProvider(none);
}
Expand Down
13 changes: 8 additions & 5 deletions examples/otlp/file_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,29 @@ namespace
{
opentelemetry::exporter::otlp::OtlpFileExporterOptions opts;

std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> sdk_provider;
std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> provider;

void InitTracer()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpFileExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
sdk_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));

// Set the global trace provider
trace::Provider::SetTracerProvider(sdk_provider);
std::shared_ptr<opentelemetry::trace::TracerProvider> api_provider = provider;
trace::Provider::SetTracerProvider(api_provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
if (sdk_provider)
if (provider)
{
sdk_provider->ForceFlush(std::chrono::milliseconds(10000));
provider->ForceFlush(std::chrono::milliseconds(10000));
}

provider.reset();
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
Expand Down
40 changes: 22 additions & 18 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,52 +40,56 @@ namespace
{
opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts;
opentelemetry::exporter::otlp::OtlpGrpcLogRecordExporterOptions log_opts;

std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> tracer_provider;
std::shared_ptr<opentelemetry::sdk::logs::LoggerProvider> logger_provider;

void InitTracer()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));

// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
std::shared_ptr<opentelemetry::trace::TracerProvider> api_provider = tracer_provider;
trace::Provider::SetTracerProvider(api_provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
if (tracer_provider)
{
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
tracer_provider->ForceFlush();
}

tracer_provider.reset();
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}

void InitLogger()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
nostd::shared_ptr<logs::LoggerProvider> provider(
logs_sdk::LoggerProviderFactory::Create(std::move(processor)));
auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor));

opentelemetry::logs::Provider::SetLoggerProvider(provider);
// Set the global logger provider
std::shared_ptr<opentelemetry::logs::LoggerProvider> api_provider = logger_provider;
opentelemetry::logs::Provider::SetLoggerProvider(api_provider);
}

void CleanupLogger()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<logs::LoggerProvider> provider =
logs::Provider::GetLoggerProvider();
if (provider)
if (logger_provider)
{
static_cast<logs_sdk::LoggerProvider *>(provider.get())->ForceFlush();
logger_provider->ForceFlush();
}

logger_provider.reset();
nostd::shared_ptr<logs::LoggerProvider> none;
opentelemetry::logs::Provider::SetLoggerProvider(none);
}
Expand Down
23 changes: 9 additions & 14 deletions examples/otlp/grpc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/tracer_provider.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"
#include "opentelemetry/nostd/shared_ptr.h"

// sdk::TracerProvider is just used to call ForceFlush and prevent to cancel running exportings when
// destroy and shutdown exporters.It's optional to users.
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/tracer_provider.h"

#ifdef BAZEL_BUILD
# include "examples/common/foo_library/foo_library.h"
Expand All @@ -28,30 +24,29 @@ namespace
{
opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts;

std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> sdk_provider;
std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> provider;

void InitTracer()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
sdk_provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));

std::shared_ptr<opentelemetry::trace::TracerProvider> api_provider ( sdk_provider );
// Set the global trace provider
std::shared_ptr<opentelemetry::trace::TracerProvider> api_provider = provider;
trace::Provider::SetTracerProvider(api_provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
if (sdk_provider)
if (provider)
{
sdk_provider->ForceFlush();
sdk_provider = nullptr;
provider->ForceFlush();
}

provider.reset();
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
Expand Down
49 changes: 26 additions & 23 deletions examples/otlp/http_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h"
#include "opentelemetry/logs/provider.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/logs/logger_provider.h"
#include "opentelemetry/sdk/logs/logger_provider_factory.h"
#include "opentelemetry/sdk/logs/processor.h"
#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"

// sdk::TracerProvider and sdk::LoggerProvider is just used to call ForceFlush and prevent to cancel
// running exportings when destroy and shutdown exporters.It's optional to users.
#include "opentelemetry/sdk/logs/logger_provider.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"

#include <iostream>
#include <string>

Expand All @@ -40,6 +37,9 @@ namespace
{

opentelemetry::exporter::otlp::OtlpHttpExporterOptions trace_opts;

std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> tracer_provider;

void InitTracer()
{
if (trace_opts.url.size() > 9)
Expand All @@ -58,53 +58,56 @@ void InitTracer()
}
}
std::cout << "Using " << trace_opts.url << " to export trace spans." << std::endl;

// Create OTLP exporter instance
auto exporter = otlp::OtlpHttpExporterFactory::Create(trace_opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
auto exporter = otlp::OtlpHttpExporterFactory::Create(trace_opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));

// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
std::shared_ptr<opentelemetry::trace::TracerProvider> api_provider = tracer_provider;
trace::Provider::SetTracerProvider(api_provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
if (tracer_provider)
{
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
tracer_provider->ForceFlush();
}

tracer_provider.reset();
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}

opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterOptions logger_opts;

std::shared_ptr<opentelemetry::sdk::logs::LoggerProvider> logger_provider;

void InitLogger()
{
std::cout << "Using " << logger_opts.url << " to export log records." << std::endl;
logger_opts.console_debug = true;
// Create OTLP exporter instance
auto exporter = otlp::OtlpHttpLogRecordExporterFactory::Create(logger_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
std::shared_ptr<logs::LoggerProvider> provider =
logs_sdk::LoggerProviderFactory::Create(std::move(processor));
auto exporter = otlp::OtlpHttpLogRecordExporterFactory::Create(logger_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor));

opentelemetry::logs::Provider::SetLoggerProvider(provider);
std::shared_ptr<opentelemetry::logs::LoggerProvider> api_provider = logger_provider;
opentelemetry::logs::Provider::SetLoggerProvider(api_provider);
}

void CleanupLogger()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<logs::LoggerProvider> provider =
logs::Provider::GetLoggerProvider();
if (provider)
if (logger_provider)
{
static_cast<logs_sdk::LoggerProvider *>(provider.get())->ForceFlush();
logger_provider->ForceFlush();
}

logger_provider.reset();
std::shared_ptr<logs::LoggerProvider> none;
opentelemetry::logs::Provider::SetLoggerProvider(none);
}
Expand Down
Loading

0 comments on commit c2a2ef4

Please sign in to comment.