Skip to content

Commit d036d82

Browse files
authored
Reapply "Change OTLP HTTP content_type default to binary (open-telemetry#2558)" (open-telemetry#2564)
1 parent a799f4a commit d036d82

14 files changed

+154
-6
lines changed

CHANGELOG.md

+3
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.2] 2024-02-27
1922

2023
* [SDK] Fix observable attributes drop

exporters/otlp/BUILD

+1
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

+2-1
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

+4
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

+5
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

+2-2
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

+72
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

+26
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

+1-1
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

+1-1
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;

exporters/otlp/src/otlp_http_metric_exporter_options.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace otlp
1717
OtlpHttpMetricExporterOptions::OtlpHttpMetricExporterOptions()
1818
{
1919
url = GetOtlpDefaultMetricsEndpoint();
20-
content_type = HttpRequestContentType::kJson;
20+
content_type = GetOtlpHttpProtocolFromString(GetOtlpDefaultHttpMetricsProtocol());
2121
json_bytes_mapping = JsonBytesMappingKind::kHexId;
2222
use_json_name = false;
2323
console_debug = false;

exporters/otlp/test/otlp_http_exporter_test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigJsonBytesMappingTest)
522522
EXPECT_EQ(GetOptions(exporter).json_bytes_mapping, JsonBytesMappingKind::kHex);
523523
}
524524

525+
TEST(OtlpHttpExporterTest, ConfigDefaultProtocolTest)
526+
{
527+
OtlpHttpExporterOptions opts;
528+
EXPECT_EQ(opts.content_type, HttpRequestContentType::kBinary);
529+
}
530+
525531
# ifndef NO_GETENV
526532
// Test exporter configuration options with use_ssl_credentials
527533
TEST_F(OtlpHttpExporterTestPeer, ConfigFromEnv)
@@ -531,6 +537,7 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigFromEnv)
531537
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
532538
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
533539
setenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "k1=v3,k1=v4", 1);
540+
setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/json", 1);
534541

535542
std::unique_ptr<OtlpHttpExporter> exporter(new OtlpHttpExporter());
536543
EXPECT_EQ(GetOptions(exporter).url, url);
@@ -557,11 +564,13 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigFromEnv)
557564
++range.first;
558565
EXPECT_TRUE(range.first == range.second);
559566
}
567+
EXPECT_EQ(GetOptions(exporter).content_type, HttpRequestContentType::kJson);
560568

561569
unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
562570
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
563571
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
564572
unsetenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS");
573+
unsetenv("OTEL_EXPORTER_OTLP_PROTOCOL");
565574
}
566575

567576
TEST_F(OtlpHttpExporterTestPeer, ConfigFromTracesEnv)
@@ -571,6 +580,7 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigFromTracesEnv)
571580
setenv("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", "1eternity", 1);
572581
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
573582
setenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "k1=v3,k1=v4", 1);
583+
setenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "http/json", 1);
574584

575585
std::unique_ptr<OtlpHttpExporter> exporter(new OtlpHttpExporter());
576586
EXPECT_EQ(GetOptions(exporter).url, url);
@@ -597,11 +607,13 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigFromTracesEnv)
597607
++range.first;
598608
EXPECT_TRUE(range.first == range.second);
599609
}
610+
EXPECT_EQ(GetOptions(exporter).content_type, HttpRequestContentType::kJson);
600611

601612
unsetenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT");
602613
unsetenv("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT");
603614
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
604615
unsetenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS");
616+
unsetenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL");
605617
}
606618
# endif
607619

exporters/otlp/test/otlp_http_log_record_exporter_test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,12 @@ TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigJsonBytesMappingTest)
642642
EXPECT_EQ(GetOptions(exporter).json_bytes_mapping, JsonBytesMappingKind::kHex);
643643
}
644644

645+
TEST(OtlpHttpLogRecordExporterTest, ConfigDefaultProtocolTest)
646+
{
647+
OtlpHttpLogRecordExporterOptions opts;
648+
EXPECT_EQ(opts.content_type, HttpRequestContentType::kBinary);
649+
}
650+
645651
# ifndef NO_GETENV
646652
// Test exporter configuration options with use_ssl_credentials
647653
TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigFromEnv)
@@ -651,6 +657,7 @@ TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigFromEnv)
651657
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
652658
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
653659
setenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS", "k1=v3,k1=v4", 1);
660+
setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/json", 1);
654661

655662
std::unique_ptr<OtlpHttpLogRecordExporter> exporter(new OtlpHttpLogRecordExporter());
656663
EXPECT_EQ(GetOptions(exporter).url, url);
@@ -677,11 +684,13 @@ TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigFromEnv)
677684
++range.first;
678685
EXPECT_TRUE(range.first == range.second);
679686
}
687+
EXPECT_EQ(GetOptions(exporter).content_type, HttpRequestContentType::kJson);
680688

681689
unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
682690
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
683691
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
684692
unsetenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS");
693+
unsetenv("OTEL_EXPORTER_OTLP_PROTOCOL");
685694
}
686695

687696
TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigFromLogsEnv)
@@ -691,6 +700,7 @@ TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigFromLogsEnv)
691700
setenv("OTEL_EXPORTER_OTLP_LOGS_TIMEOUT", "20s", 1);
692701
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
693702
setenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS", "k1=v3,k1=v4", 1);
703+
setenv("OTEL_EXPORTER_OTLP_LOGS_PROTOCOL", "http/json", 1);
694704

695705
std::unique_ptr<OtlpHttpLogRecordExporter> exporter(new OtlpHttpLogRecordExporter());
696706
EXPECT_EQ(GetOptions(exporter).url, url);
@@ -717,11 +727,13 @@ TEST_F(OtlpHttpLogRecordExporterTestPeer, ConfigFromLogsEnv)
717727
++range.first;
718728
EXPECT_TRUE(range.first == range.second);
719729
}
730+
EXPECT_EQ(GetOptions(exporter).content_type, HttpRequestContentType::kJson);
720731

721732
unsetenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT");
722733
unsetenv("OTEL_EXPORTER_OTLP_LOGS_TIMEOUT");
723734
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
724735
unsetenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS");
736+
unsetenv("OTEL_EXPORTER_OTLP_LOGS_PROTOCOL");
725737
}
726738

727739
TEST_F(OtlpHttpLogRecordExporterTestPeer, DefaultEndpoint)

exporters/otlp/test/otlp_http_metric_exporter_test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,12 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ConfigJsonBytesMappingTest)
862862
google::protobuf::ShutdownProtobufLibrary();
863863
}
864864

865+
TEST(OtlpHttpMetricExporterTest, ConfigDefaultProtocolTest)
866+
{
867+
OtlpHttpMetricExporterOptions opts;
868+
EXPECT_EQ(opts.content_type, HttpRequestContentType::kBinary);
869+
}
870+
865871
#ifndef NO_GETENV
866872
// Test exporter configuration options with use_ssl_credentials
867873
TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromEnv)
@@ -871,6 +877,7 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromEnv)
871877
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
872878
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
873879
setenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS", "k1=v3,k1=v4", 1);
880+
setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/json", 1);
874881

875882
std::unique_ptr<OtlpHttpMetricExporter> exporter(new OtlpHttpMetricExporter());
876883
EXPECT_EQ(GetOptions(exporter).url, url);
@@ -897,11 +904,13 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromEnv)
897904
++range.first;
898905
EXPECT_TRUE(range.first == range.second);
899906
}
907+
EXPECT_EQ(GetOptions(exporter).content_type, HttpRequestContentType::kJson);
900908

901909
unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
902910
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
903911
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
904912
unsetenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS");
913+
unsetenv("OTEL_EXPORTER_OTLP_PROTOCOL");
905914
}
906915

907916
TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromMetricsEnv)
@@ -911,6 +920,7 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromMetricsEnv)
911920
setenv("OTEL_EXPORTER_OTLP_METRICS_TIMEOUT", "20s", 1);
912921
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
913922
setenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS", "k1=v3,k1=v4", 1);
923+
setenv("OTEL_EXPORTER_OTLP_METRICS_PROTOCOL", "http/json", 1);
914924

915925
std::unique_ptr<OtlpHttpMetricExporter> exporter(new OtlpHttpMetricExporter());
916926
EXPECT_EQ(GetOptions(exporter).url, url);
@@ -937,11 +947,13 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromMetricsEnv)
937947
++range.first;
938948
EXPECT_TRUE(range.first == range.second);
939949
}
950+
EXPECT_EQ(GetOptions(exporter).content_type, HttpRequestContentType::kJson);
940951

941952
unsetenv("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT");
942953
unsetenv("OTEL_EXPORTER_OTLP_METRICS_TIMEOUT");
943954
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
944955
unsetenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS");
956+
unsetenv("OTEL_EXPORTER_OTLP_METRICS_PROTOCOL");
945957
}
946958

947959
TEST_F(OtlpHttpMetricExporterTestPeer, DefaultEndpoint)

0 commit comments

Comments
 (0)