forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Extend OpenTelemetry Tracer with resource detectors #2
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
Merged
joaopgrassi
merged 2 commits into
feat/otel-resource-detectors-env
from
feat/otel-resource-detectors-internal
Sep 11, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
27 changes: 27 additions & 0 deletions
27
source/extensions/tracers/opentelemetry/resource_detectors/BUILD
This file contains hidden or 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,27 @@ | ||
| load( | ||
| "//bazel:envoy_build_system.bzl", | ||
| "envoy_cc_library", | ||
| "envoy_extension_package", | ||
| ) | ||
|
|
||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| envoy_extension_package() | ||
|
|
||
| envoy_cc_library( | ||
| name = "resource_detector_lib", | ||
| srcs = [ | ||
| "resource_provider.cc", | ||
| ], | ||
| hdrs = [ | ||
| "resource_detector.h", | ||
| "resource_provider.h", | ||
| ], | ||
| deps = [ | ||
| "//envoy/config:typed_config_interface", | ||
| "//envoy/server:tracer_config_interface", | ||
| "//source/common/common:logger_lib", | ||
| "//source/common/config:utility_lib", | ||
| "@envoy_api//envoy/config/trace/v3:pkg_cc_proto", | ||
| ], | ||
| ) |
31 changes: 31 additions & 0 deletions
31
source/extensions/tracers/opentelemetry/resource_detectors/environment/BUILD
This file contains hidden or 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,31 @@ | ||
| 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 = [ | ||
| ":resource_detector_environment_lib", | ||
| "//envoy/registry", | ||
| "//source/common/config:utility_lib", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_library( | ||
| name = "resource_detector_environment_lib", | ||
| srcs = ["environment_resource_detector.cc"], | ||
| hdrs = ["environment_resource_detector.h"], | ||
| deps = [ | ||
| "//source/common/config:datasource_lib", | ||
| "//source/extensions/tracers/opentelemetry/resource_detectors:resource_detector_lib", | ||
| ], | ||
| ) |
25 changes: 25 additions & 0 deletions
25
source/extensions/tracers/opentelemetry/resource_detectors/environment/config.cc
This file contains hidden or 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 @@ | ||
| #include "config.h" | ||
|
|
||
| #include "source/common/config/utility.h" | ||
|
|
||
| #include "environment_resource_detector.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| ResourceDetectorPtr EnvironmentResourceDetectorFactory::createResourceDetector( | ||
| Server::Configuration::TracerFactoryContext& context) { | ||
| return std::make_shared<EnvironmentResourceDetector>(context); | ||
| } | ||
|
|
||
| /** | ||
| * Static registration for the Env resource detector factory. @see RegisterFactory. | ||
| */ | ||
| REGISTER_FACTORY(EnvironmentResourceDetectorFactory, ResourceDetectorFactory); | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
37 changes: 37 additions & 0 deletions
37
source/extensions/tracers/opentelemetry/resource_detectors/environment/config.h
This file contains hidden or 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 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "source/extensions/tracers/opentelemetry/resource_detectors/resource_detector.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| /** | ||
| * Config registration for the Environment resource detector. @see ResourceDetectorFactory. | ||
| */ | ||
| class EnvironmentResourceDetectorFactory : public ResourceDetectorFactory { | ||
| public: | ||
| /** | ||
| * @brief Create a Resource Detector that reads from the OTEL_RESOURCE_ATTRIBUTES | ||
| * environment variable. | ||
| * | ||
| * @param context | ||
| * @return ResourceDetectorPtr | ||
| */ | ||
| ResourceDetectorPtr | ||
| createResourceDetector(Server::Configuration::TracerFactoryContext& context) override; | ||
|
|
||
| std::string name() const override { | ||
| return "envoy.tracers.opentelemetry.resource_detectors.environment"; | ||
| } | ||
| }; | ||
|
|
||
| DECLARE_FACTORY(EnvironmentResourceDetectorFactory); | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
56 changes: 56 additions & 0 deletions
56
...ons/tracers/opentelemetry/resource_detectors/environment/environment_resource_detector.cc
This file contains hidden or 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,56 @@ | ||
| #include "environment_resource_detector.h" | ||
|
|
||
| #include <sstream> | ||
| #include <string> | ||
|
|
||
| #include "source/common/config/datasource.h" | ||
| #include "source/extensions/tracers/opentelemetry/resource_detectors/resource_detector.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| constexpr absl::string_view kOtelResourceAttributesEnv = "OTEL_RESOURCE_ATTRIBUTES"; | ||
|
|
||
| /** | ||
| * @brief Detects a resource from the OTEL_RESOURCE_ATTRIBUTES environment variable | ||
| * Based on the OTel C++ SDK: | ||
| * https://github.com/open-telemetry/opentelemetry-cpp/blob/v1.11.0/sdk/src/resource/resource_detector.cc | ||
| * | ||
| * @return Resource A resource with the attributes from the OTEL_RESOURCE_ATTRIBUTES environment | ||
| * variable. | ||
| */ | ||
| Resource EnvironmentResourceDetector::detect() { | ||
|
joaopgrassi marked this conversation as resolved.
Outdated
|
||
| envoy::config::core::v3::DataSource ds; | ||
| ds.set_environment_variable(kOtelResourceAttributesEnv); | ||
|
|
||
| Resource resource; | ||
| resource.schemaUrl = ""; | ||
|
|
||
| TRY_NEEDS_AUDIT { | ||
| auto attributes_str = Config::DataSource::read(ds, true, context_.serverFactoryContext().api()); | ||
| if (attributes_str.empty()) { | ||
| return resource; | ||
| } | ||
|
|
||
| std::istringstream iss(attributes_str); | ||
| std::string token; | ||
| while (std::getline(iss, token, ',')) { | ||
| size_t pos = token.find('='); | ||
| std::string key = token.substr(0, pos); | ||
| std::string value = token.substr(pos + 1); | ||
| resource.attributes[key] = value; | ||
| } | ||
| } | ||
| END_TRY catch (const EnvoyException& e) { | ||
| ENVOY_LOG(error, "Failed to read resource attributes: {}.", e.what()); | ||
| } | ||
|
|
||
| return resource; | ||
| } | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
34 changes: 34 additions & 0 deletions
34
...ions/tracers/opentelemetry/resource_detectors/environment/environment_resource_detector.h
This file contains hidden or 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,34 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/server/factory_context.h" | ||
|
|
||
| #include "source/common/common/logger.h" | ||
| #include "source/extensions/tracers/opentelemetry/resource_detectors/resource_detector.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| /** | ||
| * @brief A resource detector that extracts attributes from the OTEL_RESOURCE_ATTRIBUTES environment | ||
| * variable. | ||
| * @see | ||
| * https://github.com/open-telemetry/opentelemetry-specification/blob/v1.24.0/specification/resource/sdk.md#detecting-resource-information-from-the-environment | ||
| * | ||
| */ | ||
| class EnvironmentResourceDetector : public ResourceDetector, Logger::Loggable<Logger::Id::tracing> { | ||
| public: | ||
| EnvironmentResourceDetector(Server::Configuration::TracerFactoryContext& context) | ||
| : context_(context) {} | ||
| Resource detect() override; | ||
|
|
||
| private: | ||
| Server::Configuration::TracerFactoryContext& | ||
| context_; // TODO, is keeping a reference ok (ownership)? | ||
| }; | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.