forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opentelemetry tracer: add Dynatrace resource detector (envoyproxy#30824)
* Add Dynatrace resource detector Co-authored-by: Thomas Ebner <[email protected]> Signed-off-by: Joao Grassi <[email protected]> * PR suggestions Signed-off-by: Joao Grassi <[email protected]> * Add tests for Dynatrace file reader Signed-off-by: Joao Grassi <[email protected]> * PR suggestions Signed-off-by: Joao Grassi <[email protected]> * schemaUrl_ -> schema_url_ Signed-off-by: Joao Grassi <[email protected]> * PR suggestions and changelog Signed-off-by: Joao Grassi <[email protected]> --------- Signed-off-by: Joao Grassi <[email protected]> Co-authored-by: Thomas Ebner <[email protected]>
- Loading branch information
1 parent
61b5fcb
commit be410d6
Showing
23 changed files
with
742 additions
and
19 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
.../extensions/tracers/opentelemetry/resource_detectors/v3/dynatrace_resource_detector.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.extensions.tracers.opentelemetry.resource_detectors.v3; | ||
|
||
import "udpa/annotations/status.proto"; | ||
|
||
option java_package = "io.envoyproxy.envoy.extensions.tracers.opentelemetry.resource_detectors.v3"; | ||
option java_outer_classname = "DynatraceResourceDetectorProto"; | ||
option java_multiple_files = true; | ||
option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/tracers/opentelemetry/resource_detectors/v3;resource_detectorsv3"; | ||
option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
||
// [#protodoc-title: Dynatrace Resource Detector config] | ||
|
||
// Configuration for the Dynatrace Resource Detector extension. | ||
// The resource detector reads from the Dynatrace enrichment files | ||
// and adds host/process related attributes to the OpenTelemetry resource. | ||
// | ||
// See: | ||
// | ||
// `Enrich ingested data with Dynatrace-specific dimensions <https://docs.dynatrace.com/docs/shortlink/enrichment-files>`_ | ||
// | ||
// [#extension: envoy.tracers.opentelemetry.resource_detectors.dynatrace] | ||
message DynatraceResourceDetectorConfig { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_extension", | ||
"envoy_cc_library", | ||
"envoy_extension_package", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_extension_package() | ||
|
||
envoy_cc_extension( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
deps = [ | ||
":dynatrace_resource_detector_lib", | ||
"//envoy/registry", | ||
"//source/common/config:utility_lib", | ||
"@envoy_api//envoy/extensions/tracers/opentelemetry/resource_detectors/v3:pkg_cc_proto", | ||
], | ||
) | ||
|
||
envoy_cc_library( | ||
name = "dynatrace_resource_detector_lib", | ||
srcs = [ | ||
"dynatrace_metadata_file_reader.cc", | ||
"dynatrace_resource_detector.cc", | ||
], | ||
hdrs = [ | ||
"dynatrace_metadata_file_reader.h", | ||
"dynatrace_resource_detector.h", | ||
], | ||
deps = [ | ||
"//source/common/config:datasource_lib", | ||
"//source/extensions/tracers/opentelemetry/resource_detectors:resource_detector_lib", | ||
"@envoy_api//envoy/extensions/tracers/opentelemetry/resource_detectors/v3:pkg_cc_proto", | ||
], | ||
) |
37 changes: 37 additions & 0 deletions
37
source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/config.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/config.h" | ||
|
||
#include "envoy/extensions/tracers/opentelemetry/resource_detectors/v3/dynatrace_resource_detector.pb.h" | ||
#include "envoy/extensions/tracers/opentelemetry/resource_detectors/v3/dynatrace_resource_detector.pb.validate.h" | ||
|
||
#include "source/common/config/utility.h" | ||
#include "source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_metadata_file_reader.h" | ||
#include "source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_resource_detector.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Tracers { | ||
namespace OpenTelemetry { | ||
|
||
ResourceDetectorPtr DynatraceResourceDetectorFactory::createResourceDetector( | ||
const Protobuf::Message& message, Server::Configuration::TracerFactoryContext& context) { | ||
|
||
auto mptr = Envoy::Config::Utility::translateAnyToFactoryConfig( | ||
dynamic_cast<const ProtobufWkt::Any&>(message), context.messageValidationVisitor(), *this); | ||
|
||
const auto& proto_config = MessageUtil::downcastAndValidate< | ||
const envoy::extensions::tracers::opentelemetry::resource_detectors::v3:: | ||
DynatraceResourceDetectorConfig&>(*mptr, context.messageValidationVisitor()); | ||
|
||
DynatraceMetadataFileReaderPtr reader = std::make_unique<DynatraceMetadataFileReaderImpl>(); | ||
return std::make_unique<DynatraceResourceDetector>(proto_config, std::move(reader)); | ||
} | ||
|
||
/** | ||
* Static registration for the Dynatrace resource detector factory. @see RegisterFactory. | ||
*/ | ||
REGISTER_FACTORY(DynatraceResourceDetectorFactory, ResourceDetectorFactory); | ||
|
||
} // namespace OpenTelemetry | ||
} // namespace Tracers | ||
} // namespace Extensions | ||
} // namespace Envoy |
48 changes: 48 additions & 0 deletions
48
source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/config.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "envoy/extensions/tracers/opentelemetry/resource_detectors/v3/dynatrace_resource_detector.pb.h" | ||
|
||
#include "source/extensions/tracers/opentelemetry/resource_detectors/resource_detector.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Tracers { | ||
namespace OpenTelemetry { | ||
|
||
/** | ||
* Config registration for the Dynatrace resource detector. @see ResourceDetectorFactory. | ||
*/ | ||
class DynatraceResourceDetectorFactory : public ResourceDetectorFactory { | ||
public: | ||
/** | ||
* @brief Creates a Resource Detector that reads from the Dynatrace enrichment files. | ||
* | ||
* @see | ||
* https://docs.dynatrace.com/docs/shortlink/enrichment-files#oneagent-virtual-files | ||
* | ||
* @param message The resource detector configuration. | ||
* @param context The tracer factory context. | ||
* @return ResourceDetectorPtr | ||
*/ | ||
ResourceDetectorPtr | ||
createResourceDetector(const Protobuf::Message& message, | ||
Server::Configuration::TracerFactoryContext& context) override; | ||
|
||
ProtobufTypes::MessagePtr createEmptyConfigProto() override { | ||
return std::make_unique<envoy::extensions::tracers::opentelemetry::resource_detectors::v3:: | ||
DynatraceResourceDetectorConfig>(); | ||
} | ||
|
||
std::string name() const override { | ||
return "envoy.tracers.opentelemetry.resource_detectors.dynatrace"; | ||
} | ||
}; | ||
|
||
DECLARE_FACTORY(DynatraceResourceDetectorFactory); | ||
|
||
} // namespace OpenTelemetry | ||
} // namespace Tracers | ||
} // namespace Extensions | ||
} // namespace Envoy |
54 changes: 54 additions & 0 deletions
54
...ions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_metadata_file_reader.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_metadata_file_reader.h" | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include "envoy/common/exception.h" | ||
|
||
#include "source/common/common/logger.h" | ||
|
||
#include "absl/strings/str_cat.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Tracers { | ||
namespace OpenTelemetry { | ||
|
||
namespace { | ||
|
||
bool isIndirectionFile(const std::string& file_name) { | ||
return absl::StartsWith(file_name, "dt_metadata_"); | ||
} | ||
|
||
std::string readFile(const std::string& file_name) { | ||
if (file_name.empty()) { | ||
return ""; | ||
} | ||
|
||
std::ifstream file(file_name); | ||
if (file.fail()) { | ||
throw EnvoyException(absl::StrCat("Unable to read Dynatrace enrichment file: ", file_name)); | ||
} | ||
|
||
std::stringstream file_string; | ||
file_string << file.rdbuf(); | ||
|
||
return file_string.str(); | ||
} | ||
|
||
} // namespace | ||
|
||
std::string DynatraceMetadataFileReaderImpl::readEnrichmentFile(const std::string& file_name) { | ||
if (const bool indirection_file = isIndirectionFile(file_name); indirection_file) { | ||
return readFile(readFile(file_name)); | ||
} else { | ||
return readFile(file_name); | ||
} | ||
} | ||
|
||
} // namespace OpenTelemetry | ||
} // namespace Tracers | ||
} // namespace Extensions | ||
} // namespace Envoy |
49 changes: 49 additions & 0 deletions
49
...sions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_metadata_file_reader.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include "envoy/common/pure.h" | ||
|
||
#include "absl/strings/match.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Tracers { | ||
namespace OpenTelemetry { | ||
|
||
/** | ||
* @brief A file reader that reads the content of the Dynatrace metadata enrichment files. | ||
* When OneAgent is monitoring your application, it provides access to the enrichment files. | ||
* These files do not physically exists in your file system, but are provided by the OneAgent on | ||
* demand. This allows obtaining not only information about the host, but also about process. | ||
* | ||
* @see | ||
* https://docs.dynatrace.com/docs/shortlink/enrichment-files#oneagent-virtual-files | ||
* | ||
*/ | ||
class DynatraceMetadataFileReader { | ||
public: | ||
virtual ~DynatraceMetadataFileReader() = default; | ||
|
||
/** | ||
* @brief Reads the enrichment file and returns the enrichment metadata. | ||
* | ||
* @param file_name The file name. | ||
* @return const std::string String (java-like properties) containing the enrichment metadata. | ||
*/ | ||
virtual std::string readEnrichmentFile(const std::string& file_name) PURE; | ||
}; | ||
|
||
using DynatraceMetadataFileReaderPtr = std::unique_ptr<DynatraceMetadataFileReader>; | ||
|
||
class DynatraceMetadataFileReaderImpl : public DynatraceMetadataFileReader { | ||
public: | ||
std::string readEnrichmentFile(const std::string& file_name) override; | ||
}; | ||
|
||
} // namespace OpenTelemetry | ||
} // namespace Tracers | ||
} // namespace Extensions | ||
} // namespace Envoy |
54 changes: 54 additions & 0 deletions
54
...ensions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_resource_detector.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "source/extensions/tracers/opentelemetry/resource_detectors/dynatrace/dynatrace_resource_detector.h" | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Tracers { | ||
namespace OpenTelemetry { | ||
namespace { | ||
|
||
void addAttributes(const std::string& content, Resource& resource) { | ||
for (const auto& line : StringUtil::splitToken(content, "\n", false, true)) { | ||
const auto key_value = StringUtil::splitToken(line, "="); | ||
if (key_value.size() != 2) { | ||
continue; | ||
} | ||
resource.attributes_[std::string(key_value[0])] = std::string(key_value[1]); | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
Resource DynatraceResourceDetector::detect() { | ||
Resource resource; | ||
resource.schema_url_ = ""; | ||
int failure_count = 0; | ||
|
||
for (const auto& file_name : DynatraceResourceDetector::dynatraceMetadataFiles()) { | ||
TRY_NEEDS_AUDIT { | ||
std::string content = dynatrace_file_reader_->readEnrichmentFile(file_name); | ||
if (content.empty()) { | ||
failure_count++; | ||
} else { | ||
addAttributes(content, resource); | ||
} | ||
} | ||
END_TRY catch (const EnvoyException&) { failure_count++; } | ||
} | ||
|
||
if (failure_count > 0) { | ||
ENVOY_LOG( | ||
warn, | ||
"Dynatrace OpenTelemetry resource detector is configured but could not detect attributes. " | ||
"Check the Dynatrace deployment status to ensure it is correctly deployed."); | ||
} | ||
|
||
return resource; | ||
} | ||
|
||
} // namespace OpenTelemetry | ||
} // namespace Tracers | ||
} // namespace Extensions | ||
} // namespace Envoy |
Oops, something went wrong.