Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetrytracer: Add User-Agent header to OTLP trace exporters #32659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ new_features:
- area: tracing
change: |
Added support to configure a Dynatrace sampler for the OpenTelemetry tracer.
- area: tracing
change: |
Added User-Agent header to OTLP trace exporters according to the OpenTelemetry specification.

deprecated:
- area: listener
Expand Down
3 changes: 3 additions & 0 deletions source/extensions/tracers/opentelemetry/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ envoy_cc_library(
srcs = [
"grpc_trace_exporter.cc",
"http_trace_exporter.cc",
"otlp_utils.cc",
],
hdrs = [
"grpc_trace_exporter.h",
"http_trace_exporter.h",
"otlp_utils.h",
"trace_exporter.h",
],
deps = [
Expand All @@ -75,6 +77,7 @@ envoy_cc_library(
"//source/common/http:utility_lib",
"//source/common/protobuf",
"//source/common/tracing:trace_context_lib",
"//source/common/version:version_lib",
"@envoy_api//envoy/config/core/v3:pkg_cc_proto",
"@opentelemetry_proto//:trace_cc_proto",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "source/common/common/logger.h"
#include "source/common/grpc/typed_async_client.h"
#include "source/extensions/tracers/opentelemetry/otlp_utils.h"
#include "source/extensions/tracers/opentelemetry/trace_exporter.h"

#include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h"
Expand All @@ -30,7 +31,9 @@ class OpenTelemetryGrpcTraceExporterClient : Logger::Loggable<Logger::Id::tracin
LocalStream(OpenTelemetryGrpcTraceExporterClient& parent) : parent_(parent) {}

// Grpc::AsyncStreamCallbacks
void onCreateInitialMetadata(Http::RequestHeaderMap&) override {}
void onCreateInitialMetadata(Http::RequestHeaderMap& metadata) override {
metadata.setReferenceUserAgent(OtlpUtils::getOtlpUserAgentHeader());
}
void onReceiveInitialMetadata(Http::ResponseHeaderMapPtr&&) override {}
void onReceiveMessage(
std::unique_ptr<opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse>&&)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "source/common/common/enum_to_int.h"
#include "source/common/common/logger.h"
#include "source/common/protobuf/protobuf.h"
#include "source/extensions/tracers/opentelemetry/otlp_utils.h"

namespace Envoy {
namespace Extensions {
Expand Down Expand Up @@ -50,6 +51,10 @@ bool OpenTelemetryHttpTraceExporter::log(const ExportTraceServiceRequest& reques
message->headers().setReferenceMethod(Http::Headers::get().MethodValues.Post);
message->headers().setReferenceContentType(Http::Headers::get().ContentTypeValues.Protobuf);

// User-Agent header follows the OTLP specification:
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.30.0/specification/protocol/exporter.md#user-agent
message->headers().setReferenceUserAgent(OtlpUtils::getOtlpUserAgentHeader());

// Add all custom headers to the request.
for (const auto& header_pair : parsed_headers_to_add_) {
message->headers().setReference(header_pair.first, header_pair.second);
Expand Down
22 changes: 22 additions & 0 deletions source/extensions/tracers/opentelemetry/otlp_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "source/extensions/tracers/opentelemetry/otlp_utils.h"

#include <string>

#include "source/common/common/fmt.h"
#include "source/common/common/macros.h"
#include "source/common/version/version.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
namespace OpenTelemetry {

const std::string& OtlpUtils::getOtlpUserAgentHeader() {
CONSTRUCT_ON_FIRST_USE(std::string,
fmt::format("OTel-OTLP-Exporter-Envoy/{}", Envoy::VersionInfo::version()));
}

} // namespace OpenTelemetry
} // namespace Tracers
} // namespace Extensions
} // namespace Envoy
29 changes: 29 additions & 0 deletions source/extensions/tracers/opentelemetry/otlp_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <string>

namespace Envoy {
namespace Extensions {
namespace Tracers {
namespace OpenTelemetry {

/**
* Contains utility functions for Otel
*/
class OtlpUtils {

public:
/**
* @brief Get the User-Agent header value to be used on the OTLP exporter request.
*
* The header value is compliant with the OpenTelemetry specification. See:
* https://github.com/open-telemetry/opentelemetry-specification/blob/v1.30.0/specification/protocol/exporter.md#user-agent
* @return std::string The User-Agent for the OTLP exporters in Envoy.
*/
static const std::string& getOtlpUserAgentHeader();
};

} // namespace OpenTelemetry
} // namespace Tracers
} // namespace Extensions
} // namespace Envoy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/types.h>

#include "source/common/buffer/zero_copy_input_stream_impl.h"
#include "source/common/version/version.h"
#include "source/extensions/tracers/opentelemetry/grpc_trace_exporter.h"

#include "test/mocks/common.h"
Expand Down Expand Up @@ -70,6 +71,11 @@ TEST_F(OpenTelemetryGrpcTraceExporterTest, CreateExporterAndExportSpan) {
span.set_name("test");
*request.add_resource_spans()->add_scope_spans()->add_spans() = span;
EXPECT_TRUE(exporter.log(request));

Http::TestRequestHeaderMapImpl metadata;
callbacks_->onCreateInitialMetadata(metadata);
EXPECT_EQ(metadata.getUserAgentValue(),
"OTel-OTLP-Exporter-Envoy/" + Envoy::VersionInfo::version());
}

TEST_F(OpenTelemetryGrpcTraceExporterTest, NoExportWithHighWatermark) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/types.h>

#include "source/common/buffer/zero_copy_input_stream_impl.h"
#include "source/common/version/version.h"
#include "source/extensions/tracers/opentelemetry/http_trace_exporter.h"

#include "test/mocks/common.h"
Expand Down Expand Up @@ -78,6 +79,8 @@ TEST_F(OpenTelemetryHttpTraceExporterTest, CreateExporterAndExportSpan) {
callback = &callbacks;

EXPECT_EQ(Http::Headers::get().MethodValues.Post, message->headers().getMethodValue());
EXPECT_EQ(message->headers().getUserAgentValue(),
"OTel-OTLP-Exporter-Envoy/" + Envoy::VersionInfo::version());
EXPECT_EQ(Http::Headers::get().ContentTypeValues.Protobuf,
message->headers().getContentTypeValue());

Expand Down
Loading