8000 Change OTLP HTTP content_type default to binary (#2558) · open-telemetry/opentelemetry-cpp@a883dc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit a883dc7

Browse files
authored
Change OTLP HTTP content_type default to binary (#2558)
1 parent 32196a5 commit a883dc7

14 files changed

+154
-6
lines changed

CHANGELOG.md

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

1616
## [Unreleased]
1717

18+
* [SDK] Change OTLP HTTP content_type default to binary
19+
[#2558](https://github.com/open-telemetry/opentelemetry-cpp/pull/2558)
20+
1821
## [1.14.1] 2024-02-23
1922

2023
* [SDK] Restore Recordable API compatibility with versions < 1.14.0

exporters/otlp/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ cc_library(
104104
cc_library(
105105
name = "otlp_http_client",
106106
srcs = [
107+
"src/otlp_http.cc",
107108
"src/otlp_http_client.cc",
108109
],
109110
hdrs = [

exporters/otlp/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ if(WITH_OTLP_GRPC)
108108
endif()
109109

110110
if(WITH_OTLP_HTTP)
111-
add_library(opentelemetry_exporter_otlp_http_client src/otlp_http_client.cc)
111+
add_library(opentelemetry_exporter_otlp_http_client src/otlp_http.cc
112+
src/otlp_http_client.cc)
112113
set_target_properties(opentelemetry_exporter_otlp_http_client
113114
PROPERTIES EXPORT_NAME otlp_http_client)
114115
set_target_version(opentelemetry_exporter_otlp_http_client)

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ std::string GetOtlpDefaultHttpTracesEndpoint();
4141
std::string GetOtlpDefaultHttpMetricsEndpoint();
4242
std::string GetOtlpDefaultHttpLogsEndpoint();
4343

44+
std::string GetOtlpDefaultHttpTracesProtocol();
45+
std::string GetOtlpDefaultHttpMetricsProtocol();
46+
std::string GetOtlpDefaultHttpLogsProtocol();
47+
4448
// Compatibility with OTELCPP 1.8.2
4549
inline std::string GetOtlpDefaultHttpEndpoint()
4650
{

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#pragma once
55

6+
#include "opentelemetry/common/macros.h"
7+
#include "opentelemetry/nostd/string_view.h"
68
#include "opentelemetry/sdk/version/version.h"
79

810
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -24,6 +26,9 @@ enum class HttpRequestContentType
2426
kBinary,
2527
};
2628

29+
OPENTELEMETRY_EXPORT HttpRequestContentType
30+
GetOtlpHttpProtocolFromString(nostd::string_view name) noexcept;
31+
2732
} // namespace otlp
2833
} // namespace exporter
2934
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ struct OtlpHttpClientOptions
5252
/** SSL options. */
5353
ext::http::client::HttpSslOptions ssl_options;
5454

55-
// By default, post json data
56-
HttpRequestContentType content_type = HttpRequestContentType::kJson;
55+
// By default, post binary data
56+
HttpRequestContentType content_type = HttpRequestContentType::kBinary;
5757

5858
// If convert bytes into hex. By default, we will convert all bytes but id into base64
5959
// This option is ignored if content_type is not kJson

exporters/otlp/src/otlp_environment.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,78 @@ std::string GetOtlpDefaultHttpLogsEndpoint()
207207
return kDefault;
208208
}
209209

210+
std::string GetOtlpDefaultHttpTracesProtocol()
211+
{
212+
constexpr char kSignalEnv[] = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL";
213+
constexpr char kGenericEnv[] = "OTEL_EXPORTER_OTLP_PROTOCOL";
214+
constexpr char kDefault[] = "http/protobuf";
215+
216+
std::string value;
217+
bool exists;
218+
219+
exists = sdk_common::GetStringEnvironmentVariable(kSignalEnv, value);
220+
if (exists)
221+
{
222+
return value;
223+
}
224+
225+
exists = sdk_common::GetStringEnvironmentVariable(kGenericEnv, value);
226+
if (exists)
227+
{
228+
return value;
229+
}
230+
231+
return kDefault;
232+
}
233+
234+
std::string GetOtlpDefaultHttpMetricsProtocol()
235+
{
236+
constexpr char kSignalEnv[] = "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL";
237+
constexpr char kGenericEnv[] = "OTEL_EXPORTER_OTLP_PROTOCOL";
238+
constexpr char kDefault[] = "http/protobuf";
239+
240+
std::string value;
241+
bool exists;
242+
243+
exists = sdk_common::GetStringEnvironmentVariable(kSignalEnv, value);
244+
if (exists)
245+
{
246+
return value;
247+
}
248+
249+
exists = sdk_common::GetStringEnvironmentVariable(kGenericEnv, value);
250+
if (exists)
251+
{
252+
return value;
253+
}
254+
255+
return kDefault;
256+
}
257+
258+
std::string GetOtlpDefaultHttpLogsProtocol()
259+
{
260+
constexpr char kSignalEnv[] = "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL";
261+
constexpr char kGenericEnv[] = "OTEL_EXPORTER_OTLP_PROTOCOL";
262+
constexpr char kDefault[] = "http/protobuf";
263+
264+
std::string value;
265+
bool exists;
266+
267+
exists = sdk_common::GetStringEnvironmentVariable(kSignalEnv, value);
268+
if (exists)
269+
{
270+
return value;
271+
}
272+
273+
exists = sdk_common::GetStringEnvironmentVariable(kGenericEnv, value);
274+
if (exists)
275+
{
276+
return value;
277+
}
278+
279+
return kDefault;
280+
}
281+
210282
bool GetOtlpDefaultGrpcTracesIsInsecure()
211283
{
212284
std::string endpoint = GetOtlpDefaultGrpcTracesEndpoint();

exporters/otlp/src/otlp_http.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "opentelemetry/exporters/otlp/otlp_http.h"
5+
6+
OPENTELEMETRY_BEGIN_NAMESPACE
7+
namespace exporter
8+
{
9+
namespace otlp
10+
{
11+
12+
HttpRequestContentType GetOtlpHttpProtocolFromString(nostd::string_view name) noexcept
13+
{
14+
if (name == "http/json")
15+
{
16+
return HttpRequestContentType::kJson;
17+
}
18+
else
19+
{
20+
return HttpRequestContentType::kBinary;
21+
}
22+
}
23+
24+
} // namespace otlp
25+
} // namespace exporter
26+
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/src/otlp_http_exporter_options.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace otlp
1717
OtlpHttpExporterOptions::OtlpHttpExporterOptions()
1818
{
1919
url = GetOtlpDefaultHttpTracesEndpoint();
20-
content_type = HttpRequestContentType::kJson;
20+
content_type = GetOtlpHttpProtocolFromString(GetOtlpDefaultHttpTracesProtocol());
2121
json_bytes_mapping = JsonBytesMappingKind::kHexId;
2222
use_json_name = false;
2323
console_debug = false;

exporters/otlp/src/otlp_http_log_record_exporter_options.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace otlp
1717
OtlpHttpLogRecordExporterOptions::OtlpHttpLogRecordExporterOptions()
1818
{
1919
url = GetOtlpDefaultHttpLogsEndpoint();
20-
content_type = HttpRequestContentType::kJson;
20+
content_type = GetOtlpHttpProtocolFromString(GetOtlpDefaultHttpLogsProtocol());
2121
json_bytes_mapping = JsonBytesMappingKind::kHexId;
2222
use_json_name = false;
2323
console_debug = false;

0 commit comments

Comments
 (0)
0