Skip to content

Commit

Permalink
Merge branch 'main' into fix_api_singleton_1520
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Sep 13, 2022
2 parents 9edfa95 + cfaf8a1 commit 02e78bb
Show file tree
Hide file tree
Showing 27 changed files with 869 additions and 422 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
mv api-benchmark_result.json benchmarks
mv sdk-benchmark_result.json benchmarks
mv exporters-benchmark_result.json benchmarks
- uses: actions/upload-artifact@master
- uses: actions/upload-artifact@v3
with:
name: benchmark_results
path: benchmarks
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ jobs:
env BENCHMARK_DIR=/benchmark
./ci/do_ci.sh benchmark
- name: Upload benchmark results
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: benchmark_reports
path: /home/runner/benchmark
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Increment the:
## [Unreleased]

* [BUILD] Upgrade opentelemetry-proto to v0.19.0 [#1579](https://github.com/open-telemetry/opentelemetry-cpp/pull/1579)
* [METRICS EXPORTER] Add `OtlpGrpcMetricExporterFactory` and `OtlpHttpMetricExporterFactory`.
[#1606](https://github.com/open-telemetry/opentelemetry-cpp/pull/1606)
* [METRICS EXPORTER] Add `OtlpGrpcClient` [#1606](https://github.com/open-telemetry/opentelemetry-cpp/pull/1606)
* [BUILD] Fix header only api singletons [#1604](https://github.com/open-telemetry/opentelemetry-cpp/pull/1604)

## [1.6.0] 2022-08-15
Expand Down
2 changes: 2 additions & 0 deletions cmake/opentelemetry-cpp-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# opentelemetry-cpp::metrics - Imported target of opentelemetry-cpp::metrics
# opentelemetry-cpp::logs - Imported target of opentelemetry-cpp::logs
# opentelemetry-cpp::in_memory_span_exporter - Imported target of opentelemetry-cpp::in_memory_span_exporter
# opentelemetry-cpp::otlp_grpc_client - Imported target of opentelemetry-cpp::otlp_grpc_client
# opentelemetry-cpp::otlp_recordable - Imported target of opentelemetry-cpp::otlp_recordable
# opentelemetry-cpp::otlp_grpc_exporter - Imported target of opentelemetry-cpp::otlp_grpc_exporter
# opentelemetry-cpp::otlp_grpc_log_exporter - Imported target of opentelemetry-cpp::otlp_grpc_log_exporter
Expand Down Expand Up @@ -85,6 +86,7 @@ set(_OPENTELEMETRY_CPP_LIBRARIES_TEST_TARGETS
logs
in_memory_span_exporter
otlp_recordable
otlp_grpc_client
otlp_grpc_exporter
otlp_grpc_log_exporter
otlp_grpc_metrics_exporter
Expand Down
13 changes: 10 additions & 3 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if(WITH_OTLP_GRPC)
add_executable(example_otlp_grpc grpc_main.cc)
target_link_libraries(
example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT} common_foo_library
opentelemetry_trace opentelemetry_exporter_otlp_grpc gRPC::grpc++)
opentelemetry_trace opentelemetry_exporter_otlp_grpc)
if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_grpc_log grpc_log_main.cc)
target_link_libraries(
Expand All @@ -16,8 +16,7 @@ if(WITH_OTLP_GRPC)
opentelemetry_trace
opentelemetry_logs
opentelemetry_exporter_otlp_grpc
opentelemetry_exporter_otlp_grpc_log
gRPC::grpc++)
opentelemetry_exporter_otlp_grpc_log)
endif()
endif()

Expand All @@ -38,3 +37,11 @@ if(WITH_OTLP_HTTP)
opentelemetry_exporter_otlp_http_log)
endif()
endif()

if(WITH_OTLP_GRPC AND NOT WITH_METRICS_PREVIEW)
add_executable(example_otlp_metric_grpc grpc_metric_main.cc)
target_link_libraries(
example_otlp_metric_grpc ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_grpc_metrics)
endif()
24 changes: 20 additions & 4 deletions examples/otlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,37 @@ This is an example of how to use the [OpenTelemetry
Protocol](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/README.md)
(OTLP) exporter.

### Traces
The application in `grpc_main.cc` initializes an `OtlpGrpcExporter` instance,
the application in `http_main.cc` initializes an `OtlpHttpExporter` instance.
The application in `http_log_main.cc` initializes an `OtlpHttpLogExporter` instance,
the application in `grpc_log_main.cc` initializes an `OtlpGrpcLogExporter` instance.
And they register a tracer provider from the [OpenTelemetry
SDK](https://github.com/open-telemetry/opentelemetry-cpp). The application then
calls a `foo_library` which has been instrumented using the [OpenTelemetry
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api).

### Logs
The application in `http_log_main.cc` initializes an `OtlpHttpLogExporter` instance,
the application in `grpc_log_main.cc` initializes an `OtlpGrpcLogExporter` instance.
And they register a logger provider from the [OpenTelemetry
SDK](https://github.com/open-telemetry/opentelemetry-cpp). The application then
calls a `logs_foo_library` which has been instrumented using the [OpenTelemetry
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api).

### Metrics
The application in `grpc_metrics_main.cc` initializes an `OtlpGrpcMetricExporter` instance.
And it registers a meter provider from the [OpenTelemetry
SDK](https://github.com/open-telemetry/opentelemetry-cpp). The application then
calls a `metrics_foo_library` which has been instrumented using the [OpenTelemetry
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api).

### Enable SSL/TLS
To enable TLS authentication for OTLP grpc exporter, SslCredentials can be used
by specifying the path to client certificate pem file, or the string containing
this certificate via OtlpGrpcExporterOptions. The path to such a .pem file can be
provided as a command-line argument alongwith the collector endpoint to the main
binary invocation above.

### Running OpenTelemetry Collector as docker container
Resulting spans are exported to the **OpenTelemetry Collector** using the OTLP
exporter. The OpenTelemetry Collector can be configured to export to other
backends (see list of [supported
Expand All @@ -34,13 +50,13 @@ OpenTelemetry Collector with an OTLP receiver by running:
- On Unix based systems use:

```console
docker run --rm -it -p 4317:4317 -p 4318:4318 -v $(pwd)/examples/otlp:/cfg otel/opentelemetry-collector:0.38.0 --config=/cfg/opentelemetry-collector-config/config.dev.yaml
docker run --rm -it -p 4317:4317 -p 4318:4318 -v $(pwd)/examples/otlp:/cfg otel/opentelemetry-collector:0.59.0 --config=/cfg/opentelemetry-collector-config/config.dev.yaml
```

- On Windows use:

```console
docker run --rm -it -p 4317:4317 -p 4318:4318 -v "%cd%/examples/otlp":/cfg otel/opentelemetry-collector:0.38.0 --config=/cfg/opentelemetry-collector-config/config.dev.yaml
docker run --rm -it -p 4317:4317 -p 4318:4318 -v "%cd%/examples/otlp":/cfg otel/opentelemetry-collector:0.59.0 --config=/cfg/opentelemetry-collector-config/config.dev.yaml
```

Note that the OTLP gRPC and HTTP exporters connects to the Collector at `localhost:4317` and `localhost:4318/v1/traces` respectively. This can be changed with first argument from command-line, for example:
Expand Down
97 changes: 97 additions & 0 deletions examples/otlp/grpc_metric_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifndef ENABLE_METRICS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_factory.h"
# include "opentelemetry/metrics/provider.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
# include "opentelemetry/sdk/metrics/meter.h"
# include "opentelemetry/sdk/metrics/meter_provider.h"

# include <memory>
# include <thread>

# ifdef BAZEL_BUILD
# include "examples/common/metrics_foo_library/foo_library.h"
# else
# include "metrics_foo_library/foo_library.h"
# endif

namespace metric_sdk = opentelemetry::sdk::metrics;
namespace nostd = opentelemetry::nostd;
namespace common = opentelemetry::common;
namespace metrics_api = opentelemetry::metrics;
namespace otlp_exporter = opentelemetry::exporter::otlp;

namespace
{

otlp_exporter::OtlpGrpcMetricExporterOptions options;

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

std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

// Initialize and set the global MeterProvider
metric_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)};
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);
p->AddMetricReader(std::move(reader));

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

int main(int argc, char *argv[])
{
std::string example_type;
if (argc > 1)
{
options.endpoint = argv[1];
if (argc > 2)
{
example_type = argv[2];
if (argc > 3)
{
options.use_ssl_credentials = true;
options.ssl_credentials_cacert_path = argv[3];
}
}
}
// Removing this line will leave the default noop MetricProvider in place.
initMetrics();
std::string name{"otlp_grpc_metric_example"};

if (example_type == "counter")
{
foo_library::counter_example(name);
}
else if (example_type == "observable_counter")
{
foo_library::observable_counter_example(name);
}
else if (example_type == "histogram")
{
foo_library::histogram_example(name);
}
else
{
std::thread counter_example{&foo_library::counter_example, name};
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
std::thread histogram_example{&foo_library::histogram_example, name};

counter_example.join();
observable_counter_example.join();
histogram_example.join();
}
}
#endif
7 changes: 5 additions & 2 deletions examples/otlp/opentelemetry-collector-config/config.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ receivers:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
cors_allowed_origins:
- '*'
service:
pipelines:
traces:
Expand All @@ -22,3 +20,8 @@ service:
- otlp
exporters:
- logging
metrics:
receivers:
- otlp
exporters:
- logging
Loading

0 comments on commit 02e78bb

Please sign in to comment.