diff --git a/sdk/include/opentelemetry/sdk/common/attribute_utils.h b/sdk/include/opentelemetry/sdk/common/attribute_utils.h index e8935b8f62..5125e94d03 100644 --- a/sdk/include/opentelemetry/sdk/common/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/common/attribute_utils.h @@ -145,4 +145,4 @@ class AttributeMap : public std::unordered_map }; } // namespace common } // namespace sdk -OPENTELEMETRY_END_NAMESPACE +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/trace/span_data.h b/sdk/include/opentelemetry/sdk/trace/span_data.h index 6a6d823e51..4af22dc587 100644 --- a/sdk/include/opentelemetry/sdk/trace/span_data.h +++ b/sdk/include/opentelemetry/sdk/trace/span_data.h @@ -11,6 +11,7 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/trace/recordable.h" +#include "opentelemetry/sdk/trace/span_limits.h" #include "opentelemetry/trace/canonical_code.h" #include "opentelemetry/trace/span.h" #include "opentelemetry/trace/span_id.h" @@ -197,7 +198,11 @@ class SpanData final : public Recordable void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept override { - attribute_map_.SetAttribute(key, value); + if (attribute_map_.size() < SpanLimits::GetAttributeCountLimit() || + attribute_map_.find(std::string(key)) != attribute_map_.end()) + { + attribute_map_.SetAttribute(key, value); + } } void AddEvent(nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/trace/span_limits.h b/sdk/include/opentelemetry/sdk/trace/span_limits.h new file mode 100644 index 0000000000..bbcd4ae181 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/trace/span_limits.h @@ -0,0 +1,119 @@ +// Copyright 2021, 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. + +#pragma once +#include "opentelemetry/common/spin_lock_mutex.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace trace +{ +/** + * Collection of span limits configurations. + */ +class SpanLimits +{ +public: + /** + * Setter methods of configuration params + */ + static void SetAttributeCountLimit(int attributeCountLimit) + { + std::lock_guard guard(GetLock()); + GetSpanLimits()->AttributeCountLimit = attributeCountLimit; + } + + static void SetEventCountLimit(int eventCountLimit) + { + std::lock_guard guard(GetLock()); + GetSpanLimits()->EventCountLimit = eventCountLimit; + } + + static void SetLinkCountLimit(int linkCountLimit) + { + std::lock_guard guard(GetLock()); + GetSpanLimits()->LinkCountLimit = linkCountLimit; + } + + static void SetAttributePerEventCountLimit(int attributePerEventCountLimit) + { + std::lock_guard guard(GetLock()); + GetSpanLimits()->AttributePerEventCountLimit = attributePerEventCountLimit; + } + + static void SetAttributePerLinkCountLimit(int attributePerLinkCountLimit) + { + std::lock_guard guard(GetLock()); + GetSpanLimits()->AttributePerLinkCountLimit = attributePerLinkCountLimit; + } + + /** + * Getter methods of configuration params + */ + static int GetAttributeCountLimit() + { + std::lock_guard guard(GetLock()); + return GetSpanLimits()->AttributeCountLimit; + } + + static int GetEventCountLimit() + { + std::lock_guard guard(GetLock()); + return GetSpanLimits()->EventCountLimit; + } + + static int GetLinkCountLimit() + { + std::lock_guard guard(GetLock()); + return GetSpanLimits()->LinkCountLimit; + } + + static int GetAttributePerEventCountLimit() + { + std::lock_guard guard(GetLock()); + return GetSpanLimits()->AttributePerEventCountLimit; + } + + static int GetAttributePerLinkCountLimit() + { + std::lock_guard guard(GetLock()); + return GetSpanLimits()->AttributePerLinkCountLimit; + } + +private: + static nostd::shared_ptr &GetSpanLimits() noexcept + { + static nostd::shared_ptr spanLimits(new SpanLimits); + return spanLimits; + } + + static opentelemetry::common::SpinLockMutex &GetLock() noexcept + { + static opentelemetry::common::SpinLockMutex lock; + return lock; + } + + int AttributeCountLimit = 128; + int EventCountLimit = 128; + int LinkCountLimit = 128; + int AttributePerEventCountLimit = 128; + int AttributePerLinkCountLimit = 128; +}; +} // namespace trace +} // namespace sdk + +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 149684bc8e..42ebac5774 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -62,8 +62,8 @@ nostd::shared_ptr Tracer::StartSpan( auto span_context = std::unique_ptr(new trace_api::SpanContext( trace_id, span_id, trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled}, false, sampling_result.trace_state ? sampling_result.trace_state - : is_parent_span_valid ? parent_context.trace_state() - : trace_api::TraceState::GetDefault())); + : is_parent_span_valid ? parent_context.trace_state() + : trace_api::TraceState::GetDefault())); auto span = nostd::shared_ptr{ new (std::nothrow) Span{this->shared_from_this(), name, attributes, links, options,