Skip to content

Commit

Permalink
Merge branch 'main' into ostream_log_test-fix-Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Feb 13, 2022
2 parents 936cbcc + e89e5b3 commit fadffb9
Show file tree
Hide file tree
Showing 26 changed files with 1,058 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ along with build files for CMake and Bazel.

See [CONTRIBUTING.md](CONTRIBUTING.md)

We meet weekly, and the time of the meeting alternates between Monday at 15:00
We meet weekly, and the time of the meeting alternates between Monday at 13:00
PT and Wednesday at 10:00 PT. The meeting is subject to change depending on
contributors' availability. Check the [OpenTelemetry community
calendar](https://calendar.google.com/calendar/embed?src=google.com_b79e3e90j7bbsa2n2p5an5lf60%40group.calendar.google.com)
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/metrics/async_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include "observer_result.h"
# include "opentelemetry/metrics/observer_result.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
Expand Down
1 change: 1 addition & 0 deletions api/include/opentelemetry/metrics/sync_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/nostd/type_traits.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
Expand Down
6 changes: 6 additions & 0 deletions docs/performance/benchmarks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Performance Tests - Benchmarks
==============================

Click `here <https://open-telemetry.github.io/opentelemetry-cpp/benchmarks/index.html>`_ to view the latest performance benchmarks for packages in this repo.

Please note that the flutation in the results are mainly because [machines with different CPUs](https://github.com/benchmark-action/github-action-benchmark/issues/79) are used for tests.
8 changes: 8 additions & 0 deletions docs/public/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ OpenTelemetry C++
otel_docs/namespace_opentelemetry__sdk__instrumentationlibrary
otel_docs/namespace_opentelemetry__sdk__resource

.. toctree::
:maxdepth: 1
:caption: Performance
:name: performance-tests
:glob:

performance/**

.. toctree::
:maxdepth: 1
:caption: Further Reading
Expand Down
24 changes: 14 additions & 10 deletions docs/public/sdk/GettingStarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ The OpenTelemetry C++ SDK allow for creation of Resources and for associating th
auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes
{
{"service.name": "shoppingcart"},
{"service.instance.id": "instance-12"}
{"service.name", "shoppingcart"},
{"service.instance.id", "instance-12"}
};
auto resource = opentelemetry::sdk::resource::Resource::Create(resource_attributes);
auto received_attributes = resource.GetAttributes();
Expand Down Expand Up @@ -136,18 +136,21 @@ OpenTelemetry C++ SDK offers four samplers out of the box:
.. code:: cpp
//AlwaysOnSampler
opentelemetry::sdk::trace::AlwaysOnSampler always_on_sampler;
auto always_on_sampler = std::unique_ptr<sdktrace::AlwaysOnSampler>
(new sdktrace::AlwaysOnSampler);
//AlwaysOffSampler
opentelemetry::sdk::trace::AlwaysOffSampler always_off_sampler;
auto always_off_sampler = std::unique_ptr<sdktrace::AlwaysOffSampler>
(new sdktrace::AlwaysOffSampler);
//ParentBasedSampler
opentelemetry::sdk::trace::ParentBasedSampler sampler_off(std::make_shared<AlwaysOffSampler>());
auto parent_based_sampler = std::unique_ptr<sdktrace::ParentBasedSampler>
(new sdktrace::ParentBasedSampler);
//TraceIdRatioBasedSampler - Sample 50% generated spans
double ratio = 0.5;
opentelemetry::sdk::trace::TraceIdRatioBasedSampler s(ratio);
auto always_off_sampler = std::unique_ptr<sdktrace::TraceIdRatioBasedSampler>
(new sdktrace::TraceIdRatioBasedSampler(ratio));
TracerContext
^^^^^^^^^^^^^
Expand All @@ -172,11 +175,12 @@ There are two different mechanisms to create TraceProvider instance
.. code:: cpp
// Created using `TracerContext` instance
auto tracer_provider = sdktrace::TracerProvider(tracer_context);
auto tracer_provider = nostd::shared_ptr<sdktrace::TracerProvider>
(new sdktrace::TracerProvider(tracer_context));
// Create using SDK configurations as parameter
auto tracer_provider =
sdktrace::TracerProvider(std::move(simple_processor), resource, std::move(always_on_sampler));
auto tracer_provider = nostd::shared_ptr<sdktrace::TracerProvider>
(std::move(simple_processor), resource, std::move(always_on_sampler));
// set the global tracer TraceProvider
opentelemetry::trace::Provider::SetTracerProvider(tracer_provider);
Expand Down
179 changes: 179 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/async_instruments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/metrics/async_instruments.h"
# include "opentelemetry/metrics/observer_result.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/metrics/measurement_processor.h"

# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/sdk/metrics/instruments.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

template <class T>
class Asynchronous
{
public:
Asynchronous(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<T> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: name_(name),
instrumentation_library_{instrumentation_library},
measurement_processor_{measurement_processor},
callback_(callback),
description_(description),
unit_(unit)
{}

protected:
std::string name_;
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_;
const MeasurementProcessor *measurement_processor_;
void (*callback_)(opentelemetry::metrics::ObserverResult<T> &);
std::string description_;
std::string unit_;
};

class LongObservableCounter : public opentelemetry::metrics::ObservableCounter<long>,
public Asynchronous<long>
{
public:
LongObservableCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name,
instrumentation_library,
measurement_processor,
callback,
description,
unit)

{}
};

class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter<double>,
public Asynchronous<double>
{
public:
DoubleObservableCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name,
instrumentation_library,
measurement_processor,
callback,
description,
unit)

{}
};

class LongObservableGauge : public opentelemetry::metrics::ObservableGauge<long>,
public Asynchronous<long>
{
public:
LongObservableGauge(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name,
instrumentation_library,
measurement_processor,
callback,
description,
unit)

{}
};

class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge<double>,
public Asynchronous<double>
{
public:
DoubleObservableGauge(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name,
instrumentation_library,
measurement_processor,
callback,
description,
unit)

{}
};

class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter<long>,
public Asynchronous<long>
{
public:
LongObservableUpDownCounter(
nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name,
instrumentation_library,
measurement_processor,
callback,
description,
unit)

{}
};

class DoubleObservableUpDownCounter
: public opentelemetry::metrics::ObservableUpDownCounter<double>,
public Asynchronous<double>
{
public:
DoubleObservableUpDownCounter(
nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name,
instrumentation_library,
measurement_processor,
callback,
description,
unit)
{}
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
108 changes: 108 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/measurement_processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/metric_reader.h"
# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h"

# include <map>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

static std::size_t MakeKey(const MetricReader &metric_reader)
{
return reinterpret_cast<std::size_t>(&metric_reader);
}
class MeasurementProcessor
{
public:
virtual void RecordLong(long value) noexcept = 0;

virtual void RecordLong(long value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0;

virtual void RecordDouble(double value) noexcept = 0;

virtual void RecordDouble(double value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0;

virtual bool Collect(MetricReader &reader,
AggregationTemporarily aggregation_temporarily,
nostd::function_ref<bool(MetricData)> callback) noexcept = 0;
};

class DefaultMeasurementProcessor : public MeasurementProcessor
{

public:
bool AddMetricStorage(const MetricReader &reader)
{
// TBD = check if already present.
metric_storages_[MakeKey(reader)] = std::unique_ptr<SyncMetricStorage>(new SyncMetricStorage());
return true;
}

virtual void RecordLong(long value) noexcept override
{
for (const auto &kv : metric_storages_)
{
kv.second->RecordLong(value);
}
}

virtual void RecordLong(
long value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
{
for (const auto &kv : metric_storages_)
{
kv.second->RecordLong(value, attributes);
}
}

virtual void RecordDouble(double value) noexcept override
{
for (const auto &kv : metric_storages_)
{
kv.second->RecordDouble(value);
}
}

virtual void RecordDouble(
double value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
{
for (const auto &kv : metric_storages_)
{
kv.second->RecordDouble(value, attributes);
}
}

bool Collect(MetricReader &reader,
AggregationTemporarily aggregation_temporarily,
nostd::function_ref<bool(MetricData)> callback) noexcept override
{
auto i = metric_storages_.find(MakeKey(reader));
if (i != metric_storages_.end())
{
return i->second->Collect(aggregation_temporarily, callback);
}
return false;
}

private:
std::map<std::size_t, std::unique_ptr<SyncMetricStorage>> metric_storages_;
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Loading

0 comments on commit fadffb9

Please sign in to comment.