-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
489 additions
and
25 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/sdk/common/attribute_utils.h" | ||
#include "opentelemetry/sdk/resource/resource_detector.h" | ||
#include "opentelemetry/sdk/version/version.h" | ||
#include "opentelemetry/version.h" | ||
|
||
#include <memory> | ||
#include <sstream> | ||
#include <unordered_map> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace resource | ||
{ | ||
|
||
using ResourceAttributes = | ||
std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue>; | ||
|
||
class Resource | ||
{ | ||
public: | ||
Resource(const Resource &) = default; | ||
|
||
const ResourceAttributes &GetAttributes() const noexcept; | ||
|
||
/** | ||
* Returns a new, merged {@link Resource} by merging the current Resource | ||
* with the other Resource. In case of a collision, current Resource takes | ||
* precedence. | ||
* | ||
* @param other the Resource that will be merged with this. | ||
* @returns the newly merged Resource. | ||
*/ | ||
|
||
Resource Merge(const Resource &other) noexcept; | ||
|
||
/** | ||
* Returns a newly created Resource with the specified attributes. | ||
* It adds (merge) SDK attributes and OTEL attributes before returning. | ||
* @param attributes for this resource | ||
* @returns the newly created Resource. | ||
*/ | ||
|
||
static Resource Create(const ResourceAttributes &attributes); | ||
|
||
/** | ||
* Returns an Empty resource. | ||
*/ | ||
|
||
static Resource &GetEmpty(); | ||
|
||
/** | ||
* Returns a Resource that indentifies the SDK in use. | ||
*/ | ||
|
||
static Resource &GetDefault(); | ||
|
||
protected: | ||
/** | ||
* The constructor is protected and only for use internally by the class and | ||
* inside ResourceDetector class. | ||
* Users should use the Create factory method to obtain a Resource | ||
* instance. | ||
*/ | ||
Resource(const ResourceAttributes &attributes = ResourceAttributes()) noexcept; | ||
|
||
private: | ||
ResourceAttributes attributes_; | ||
|
||
friend class OTELResourceDetector; | ||
}; | ||
|
||
} // namespace resource | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
35 changes: 35 additions & 0 deletions
35
sdk/include/opentelemetry/sdk/resource/resource_detector.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,35 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/nostd/unique_ptr.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace resource | ||
{ | ||
|
||
class Resource; | ||
|
||
/** | ||
* Interface for a Resource Detector | ||
*/ | ||
class ResourceDetector | ||
{ | ||
public: | ||
virtual Resource Detect() = 0; | ||
}; | ||
|
||
/** | ||
* OTelResourceDetector to detect the presence of and create a Resource | ||
* from the OTEL_RESOURCE_ATTRIBUTES environment variable. | ||
*/ | ||
class OTELResourceDetector : public ResourceDetector | ||
{ | ||
public: | ||
Resource Detect() noexcept override; | ||
}; | ||
|
||
} // namespace resource | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
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
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,26 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_library( | ||
name = "resource", | ||
srcs = glob(["**/*.cc"]), | ||
hdrs = glob(["**/*.h"]), | ||
include_prefix = "src/resource", | ||
deps = [ | ||
"//api", | ||
"//sdk:headers", | ||
], | ||
) |
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,16 @@ | ||
add_library(opentelemetry_resources resource.cc resource_detector.cc) | ||
|
||
set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) | ||
|
||
target_link_libraries(opentelemetry_resources opentelemetry_common) | ||
|
||
target_include_directories( | ||
opentelemetry_resources | ||
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/sdk/include>") | ||
|
||
install( | ||
TARGETS opentelemetry_resources | ||
EXPORT "${PROJECT_NAME}-target" | ||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
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,60 @@ | ||
#include "opentelemetry/sdk/resource/resource.h" | ||
#include "opentelemetry/nostd/span.h" | ||
#include "opentelemetry/sdk/resource/resource_detector.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace resource | ||
{ | ||
|
||
const std::string kTelemetrySdkLanguage = "telemetry.sdk.language"; | ||
const std::string kTelemetrySdkName = "telemetry.sdk.name"; | ||
const std::string kTelemetrySdkVersion = "telemetry.sdk.version"; | ||
|
||
Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_(attributes) {} | ||
|
||
Resource Resource::Merge(const Resource &other) noexcept | ||
{ | ||
ResourceAttributes merged_resource_attributes(other.attributes_); | ||
merged_resource_attributes.insert(attributes_.begin(), attributes_.end()); | ||
return Resource(merged_resource_attributes); | ||
} | ||
|
||
Resource Resource::Create(const ResourceAttributes &attributes) | ||
{ | ||
static auto otel_resource = OTELResourceDetector().Detect(); | ||
auto default_resource = Resource::GetDefault(); | ||
|
||
if (attributes.size() > 0) | ||
{ | ||
Resource tmp_resource(attributes); | ||
auto merged_resource = tmp_resource.Merge(default_resource); | ||
return merged_resource.Merge(otel_resource); | ||
} | ||
return default_resource.Merge(otel_resource); | ||
} | ||
|
||
Resource &Resource::GetEmpty() | ||
{ | ||
static Resource empty_resource; | ||
return empty_resource; | ||
} | ||
|
||
Resource &Resource::GetDefault() | ||
{ | ||
static Resource default_resource({{kTelemetrySdkLanguage, "cpp"}, | ||
{kTelemetrySdkName, "opentelemetry"}, | ||
{kTelemetrySdkVersion, OPENTELEMETRY_SDK_VERSION}}); | ||
return default_resource; | ||
} | ||
|
||
const ResourceAttributes &Resource::GetAttributes() const noexcept | ||
{ | ||
return attributes_; | ||
} | ||
|
||
} // namespace resource | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
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,34 @@ | ||
#include "opentelemetry/sdk/resource/resource_detector.h" | ||
#include <cstdlib> | ||
#include "opentelemetry/sdk/resource/resource.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace resource | ||
{ | ||
|
||
const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; | ||
|
||
Resource OTELResourceDetector::Detect() noexcept | ||
{ | ||
char *attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); | ||
if (attributes_str == nullptr) | ||
return Resource(); | ||
// return Resource::GetEmpty(); | ||
ResourceAttributes attributes; | ||
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); | ||
attributes[key] = value; | ||
} | ||
return Resource(attributes); | ||
} | ||
|
||
} // namespace resource | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
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 |
---|---|---|
|
@@ -23,5 +23,6 @@ cc_library( | |
"//api", | ||
"//sdk:headers", | ||
"//sdk/src/common:random", | ||
"//sdk/src/resource", | ||
], | ||
) |
Oops, something went wrong.