Skip to content

Commit 87a085c

Browse files
authored
Merge branch 'main' into new_cmake_options
2 parents 9cca6ee + 5ef557b commit 87a085c

20 files changed

Lines changed: 1067 additions & 48 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [BUILD] Run the ext_http component install test on Windows
19+
[#4326](https://github.com/open-telemetry/opentelemetry-cpp/pull/4326)
20+
1821
* [BENCHMARK] Add multi-threaded base2 exponential histogram benchmarks
1922
[#4319](https://github.com/open-telemetry/opentelemetry-cpp/pull/4319)
2023

@@ -29,6 +32,9 @@ Increment the:
2932
* [CODE HEALTH] Move remaining API test helpers into anonymous namespaces
3033
[#4301](https://github.com/open-telemetry/opentelemetry-cpp/pull/4301)
3134

35+
* [BUG] Wait on a completion state in the Elasticsearch exporter
36+
[#4298](https://github.com/open-telemetry/opentelemetry-cpp/pull/4298)
37+
3238
* [BUG] Make SocketAddr string parsing safe and reject malformed addresses
3339
[#4292](https://github.com/open-telemetry/opentelemetry-cpp/pull/4292)
3440

@@ -79,6 +85,13 @@ Increment the:
7985
namespaces
8086
[#4302](https://github.com/open-telemetry/opentelemetry-cpp/pull/4302)
8187

88+
* [METRICS SDK] Fix histogram views being rejected when only
89+
`aggregation_cardinality_limit` is set
90+
[#4314](https://github.com/open-telemetry/opentelemetry-cpp/pull/4314)
91+
92+
* [SDK] Implement the ProbabilitySampler
93+
[#4135](https://github.com/open-telemetry/opentelemetry-cpp/pull/4135)
94+
8295
Breaking changes:
8396

8497
* [CMAKE] Rename cmake options with prefix `OTELCPP_`

‎ci/do_ci.ps1‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ switch ($action) {
465465
"api",
466466
"sdk",
467467
"ext_common",
468+
"ext_http",
468469
"ext_http_curl",
469470
"exporters_in_memory",
470471
"exporters_ostream",

‎docs/public/sdk/GettingStarted.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ Sampler
108108
^^^^^^^
109109

110110
Sampling is mechanism to control/reducing the number of samples of traces collected and sent to the backend.
111-
OpenTelemetry C++ SDK offers four samplers out of the box:
111+
OpenTelemetry C++ SDK offers five samplers out of the box:
112112

113113
- AlwaysOnSampler which samples every trace regardless of upstream sampling decisions.
114114
- AlwaysOffSampler which doesn’t sample any trace, regardless of upstream sampling decisions.
115115
- ParentBased which uses the parent span to make sampling decisions, if present.
116116
- TraceIdRatioBased which samples a configurable percentage of traces.
117+
- ProbabilitySampler which samples a configurable ratio of traces following the consistent probability sampling specification.
117118

118119
.. code:: cpp
119120
@@ -134,6 +135,10 @@ OpenTelemetry C++ SDK offers four samplers out of the box:
134135
auto always_off_sampler = std::unique_ptr<sdktrace::TraceIdRatioBasedSampler>
135136
(new sdktrace::TraceIdRatioBasedSampler(ratio));
136137
138+
//ProbabilitySampler - Sample 50% generated spans using consistent probability sampling
139+
auto probability_sampler = std::unique_ptr<sdktrace::ProbabilitySampler>
140+
(new sdktrace::ProbabilitySampler(ratio));
141+
137142
TracerContext
138143
^^^^^^^^^^^^^
139144

‎exporters/elasticsearch/src/es_log_record_exporter.cc‎

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#include <stdint.h>
54
#include <atomic>
65
#include <chrono>
76
#include <condition_variable>
7+
#include <cstdint>
88
#include <map>
99
#include <memory> // IWYU pragma: keep
1010
#include <mutex>
@@ -100,21 +100,24 @@ class ResponseHandler : public http_client::EventHandler
100100
<< log_message);
101101
}
102102

103-
// Set the response_received_ flag to true and notify any threads waiting on this result
104-
response_received_ = true;
103+
// Record the outcome and notify any threads waiting on this result
104+
recordCompletionLocked(CompletionState::Success);
105105
}
106106
cv_.notify_all();
107107
}
108108

109109
/**
110-
* A method the user calls to block their thread until the response is received. The longest
111-
* duration is the timeout of the request, set by SetTimeoutMs()
110+
* A method the user calls to block their thread until the request has either produced a
111+
* response or failed. The longest duration is the timeout of the request, set by
112+
* SetTimeoutMs(), which arrives here as a TimedOut session event.
112113
*/
113114
bool waitForResponse()
114115
{
115116
std::unique_lock<std::mutex> lk(mutex_);
116-
cv_.wait(lk);
117-
return response_received_;
117+
// Waiting on a predicate rather than bare: the completion may already have been recorded
118+
// before this thread got here, in which case there is no notification left to receive.
119+
cv_.wait(lk, [this] { return completion_ != CompletionState::Pending; });
120+
return completion_ == CompletionState::Success;
118121
}
119122

120123
/**
@@ -135,20 +138,23 @@ class ResponseHandler : public http_client::EventHandler
135138
{
136139
case http_client::SessionState::CreateFailed:
137140
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Failed to create session");
138-
cv_.notify_all();
141+
recordCompletion(CompletionState::Failure);
139142
break;
140143
case http_client::SessionState::Created:
141144
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Session created");
142145
break;
143146
case http_client::SessionState::Destroyed:
144147
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Session destroyed");
148+
// Nothing else will arrive after this. If no outcome was recorded, the session ended
149+
// without a response, so release the waiter rather than leaving it blocked forever.
150+
recordCompletion(CompletionState::Failure);
145151
break;
146152
case http_client::SessionState::Connecting:
147153
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Connecting to peer");
148154
break;
149155
case http_client::SessionState::ConnectFailed:
150156
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Failed to connect to peer");
151-
cv_.notify_all();
157+
recordCompletion(CompletionState::Failure);
152158
break;
153159
case http_client::SessionState::Connected:
154160
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Connected to peer");
@@ -158,22 +164,22 @@ class ResponseHandler : public http_client::EventHandler
158164
break;
159165
case http_client::SessionState::SendFailed:
160166
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Failed to send request");
161-
cv_.notify_all();
167+
recordCompletion(CompletionState::Failure);
162168
break;
163169
case http_client::SessionState::Response:
164170
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Received response");
165171
break;
166172
case http_client::SessionState::SSLHandshakeFailed:
167173
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Failed SSL Handshake");
168-
cv_.notify_all();
174+
recordCompletion(CompletionState::Failure);
169175
break;
170176
case http_client::SessionState::TimedOut:
171177
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Request timed out");
172-
cv_.notify_all();
178+
recordCompletion(CompletionState::Failure);
173179
break;
174180
case http_client::SessionState::NetworkError:
175181
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Network error");
176-
cv_.notify_all();
182+
recordCompletion(CompletionState::Failure);
177183
break;
178184
case http_client::SessionState::ReadError:
179185
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Read error");
@@ -183,18 +189,47 @@ class ResponseHandler : public http_client::EventHandler
183189
break;
184190
case http_client::SessionState::Cancelled:
185191
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] (manually) cancelled");
186-
cv_.notify_all();
192+
recordCompletion(CompletionState::Failure);
187193
break;
188194
}
189195
}
190196

191197
private:
198+
enum class CompletionState : std::uint8_t
199+
{
200+
Pending,
201+
Success,
202+
Failure
203+
};
204+
205+
/**
206+
* Record the outcome of the request, first writer wins, then release any waiter. Keeping the
207+
* first outcome means a session destroyed after a successful response does not overwrite it.
208+
*/
209+
void recordCompletion(CompletionState state)
210+
{
211+
{
212+
std::unique_lock<std::mutex> lk(mutex_);
213+
recordCompletionLocked(state);
214+
}
215+
cv_.notify_all();
216+
}
217+
218+
/// As recordCompletion(), for callers that already hold mutex_ and notify themselves.
219+
void recordCompletionLocked(CompletionState state)
220+
{
221+
if (completion_ == CompletionState::Pending)
222+
{
223+
completion_ = state;
224+
}
225+
}
226+
192227
// Define a condition variable and mutex
193228
std::condition_variable cv_;
194229
std::mutex mutex_;
195230

196-
// Whether the response from Elasticsearch has been received
197-
bool response_received_ = false;
231+
// Whether the request has completed, and how
232+
CompletionState completion_ = CompletionState::Pending;
198233

199234
// A string to store the response body
200235
std::string body_ = "";

‎exporters/otlp/src/otlp_populate_attribute_utils.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
// clang-format off
3030
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
31-
#include <google/protobuf/repeated_ptr_field.h>
31+
// IWYU pragma: no_include <google/protobuf/repeated_ptr_field.h>
3232
// IWYU pragma: no_include "net/proto2/public/repeated_field.h"
3333
#include "opentelemetry/proto/common/v1/common.pb.h"
3434
#include "opentelemetry/proto/resource/v1/resource.pb.h"

‎exporters/otlp/test/otlp_metrics_serialization_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
// clang-format off
2929
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
30-
#include <google/protobuf/repeated_ptr_field.h>
30+
// IWYU pragma: no_include <google/protobuf/repeated_ptr_field.h>
3131
#include "opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h"
3232
#include "opentelemetry/proto/common/v1/common.pb.h"
3333
#include "opentelemetry/proto/metrics/v1/metrics.pb.h"

‎sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation_config.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ class HistogramAggregationConfig : public AggregationConfig
5252

5353
AggregationType GetType() const noexcept override { return AggregationType::kHistogram; }
5454

55+
// The SDK-specified default bucket boundaries, used when no boundaries are configured.
56+
static const std::vector<double> &DefaultBoundaries()
57+
{
58+
static const std::vector<double> boundaries = {0.0, 5.0, 10.0, 25.0, 50.0,
59+
75.0, 100.0, 250.0, 500.0, 750.0,
60+
1000.0, 2500.0, 5000.0, 7500.0, 10000.0};
61+
return boundaries;
62+
}
63+
5564
std::vector<double> boundaries_;
5665
bool record_min_max_ = true;
5766
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <stdint.h>
7+
#include <string>
8+
9+
#include "opentelemetry/nostd/string_view.h"
10+
#include "opentelemetry/sdk/trace/sampler.h"
11+
#include "opentelemetry/trace/span_metadata.h"
12+
#include "opentelemetry/trace/trace_id.h"
13+
#include "opentelemetry/version.h"
14+
15+
OPENTELEMETRY_BEGIN_NAMESPACE
16+
namespace sdk
17+
{
18+
namespace trace
19+
{
20+
/**
21+
* The ProbabilitySampler computes and returns a decision based on the
22+
* span's 56-bit randomness value and the configured ratio, following the
23+
* consistent probability sampling specification.
24+
*/
25+
class ProbabilitySampler : public Sampler
26+
{
27+
public:
28+
/**
29+
* @param ratio the sampling probability: either 0 (never sample) or a
30+
* value in [2^-56, 1.0]. Other values (including NaN) log a warning and
31+
* fall back to the default of 1.0. If the randomness value of the span is
32+
* greater than or equal to the rejection threshold derived from the ratio,
33+
* ShouldSample will return RECORD_AND_SAMPLE.
34+
*/
35+
explicit ProbabilitySampler(double ratio);
36+
37+
/**
38+
* @return Returns either RECORD_AND_SAMPLE or DROP based on the comparison
39+
* of the span's randomness value against the configured rejection
40+
* threshold. The parent SampledFlag is ignored.
41+
*/
42+
SamplingResult ShouldSample(
43+
const opentelemetry::trace::SpanContext &parent_context,
44+
opentelemetry::trace::TraceId trace_id,
45+
nostd::string_view /*name*/,
46+
opentelemetry::trace::SpanKind /*span_kind*/,
47+
const opentelemetry::common::KeyValueIterable & /*attributes*/,
48+
const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/) noexcept override;
49+
50+
/**
51+
* @return Description MUST be ProbabilitySampler{0.000100}
52+
*/
53+
nostd::string_view GetDescription() const noexcept override;
54+
55+
private:
56+
std::string description_;
57+
uint64_t threshold_;
58+
};
59+
} // namespace trace
60+
} // namespace sdk
61+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
8+
#include "opentelemetry/sdk/trace/sampler.h"
9+
#include "opentelemetry/version.h"
10+
11+
OPENTELEMETRY_BEGIN_NAMESPACE
12+
namespace sdk
13+
{
14+
namespace trace
15+
{
16+
17+
/**
18+
* Factory class for ProbabilitySampler.
19+
*/
20+
class ProbabilitySamplerFactory
21+
{
22+
public:
23+
/**
24+
* Create a ProbabilitySampler.
25+
* @param ratio the sampling probability: either 0 (never sample) or a
26+
* value in [2^-56, 1.0]. Other values (including NaN) log a warning and
27+
* fall back to the default of 1.0.
28+
*/
29+
static std::unique_ptr<Sampler> Create(double ratio);
30+
};
31+
32+
} // namespace trace
33+
} // namespace sdk
34+
OPENTELEMETRY_END_NAMESPACE

‎sdk/src/configuration/sdk_builder.cc‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
#include "opentelemetry/sdk/logs/processor.h"
147147
#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
148148
#include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
149+
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
149150
#include "opentelemetry/sdk/metrics/cardinality_limits.h"
150151
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
151152
#include "opentelemetry/sdk/metrics/instruments.h"
@@ -1748,8 +1749,43 @@ void SdkBuilder::AddView(
17481749
}
17491750
else
17501751
{
1751-
sdk_aggregation_config = std::make_shared<opentelemetry::sdk::metrics::AggregationConfig>(
1752-
stream->aggregation_cardinality_limit);
1752+
// No explicit `aggregation` block was configured, so the view falls back to the
1753+
// instrument's default aggregation. ViewRegistry::AddView() rejects a view whose
1754+
// AggregationConfig type does not match the (possibly instrument-derived) aggregation
1755+
// type, so the config created here must match that same default rather than always
1756+
// being a plain AggregationConfig (which only satisfies kSum/kLastValue/kDrop).
1757+
auto effective_aggregation_type = sdk_aggregation_type;
1758+
if (effective_aggregation_type == opentelemetry::sdk::metrics::AggregationType::kDefault)
1759+
{
1760+
bool is_monotonic{false};
1761+
effective_aggregation_type =
1762+
opentelemetry::sdk::metrics::DefaultAggregation::GetDefaultAggregationType(
1763+
sdk_instrument_type, is_monotonic);
1764+
}
1765+
1766+
switch (effective_aggregation_type)
1767+
{
1768+
case opentelemetry::sdk::metrics::AggregationType::kHistogram: {
1769+
auto histogram_config =
1770+
std::make_shared<opentelemetry::sdk::metrics::HistogramAggregationConfig>(
1771+
stream->aggregation_cardinality_limit);
1772+
// A default-constructed HistogramAggregationConfig has empty boundaries_, which
1773+
// LongHistogramAggregation/DoubleHistogramAggregation interpret as "use these zero
1774+
// boundaries" rather than "no boundaries configured" (that distinction only exists
1775+
// when the config pointer itself is null). Since this config is synthesized here
1776+
// rather than coming from an explicit `aggregation` block, it must carry the SDK's
1777+
// default boundaries to preserve the instrument's default histogram shape.
1778+
histogram_config->boundaries_ =
1779+
opentelemetry::sdk::metrics::HistogramAggregationConfig::DefaultBoundaries();
1780+
sdk_aggregation_config = histogram_config;
1781+
break;
1782+
}
1783+
1784+
default:
1785+
sdk_aggregation_config = std::make_shared<opentelemetry::sdk::metrics::AggregationConfig>(
1786+
stream->aggregation_cardinality_limit);
1787+
break;
1788+
}
17531789
}
17541790
}
17551791

0 commit comments

Comments
 (0)