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

Added max value length per span attribute #634

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
11 changes: 10 additions & 1 deletion Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class RecordEventsReadableSpan: ReadableSpan {
public private(set) var totalRecordedLinks: Int
/// Max number of attributes per span.
public private(set) var maxNumberOfAttributes: Int
/// Max value length of attribute per span.
public private(set) var maxValueLengthPerSpanAttribute: Int
/// Max number of attributes per event.
public private(set) var maxNumberOfAttributesPerEvent: Int

Expand Down Expand Up @@ -140,6 +142,7 @@ public class RecordEventsReadableSpan: ReadableSpan {
events = ArrayWithCapacity<SpanData.Event>(capacity: spanLimits.eventCountLimit)
maxNumberOfAttributes = spanLimits.attributeCountLimit
maxNumberOfAttributesPerEvent = spanLimits.attributePerEventCountLimit
maxValueLengthPerSpanAttribute = spanLimits.attributeValueLengthLimit
}

/// Creates and starts a span with the given configuration.
Expand Down Expand Up @@ -253,7 +256,13 @@ public class RecordEventsReadableSpan: ReadableSpan {
if attributes[key] == nil, totalAttributeCount > maxNumberOfAttributes {
return
}
attributes[key] = value
/// Process only `string` type value
if case .string(let value) = value {
let formattedValue = value.count > maxValueLengthPerSpanAttribute ? String(value.prefix(maxValueLengthPerSpanAttribute)) : value
attributes[key] = AttributeValue(formattedValue)
} else {
attributes[key] = value
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions Sources/OpenTelemetrySdk/Trace/SpanLimits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public struct SpanLimits: Equatable {
public private(set) var attributePerEventCountLimit: Int = 128
/// the global default max number of attributes per Link.
public private(set) var attributePerLinkCountLimit: Int = 128
/// the global default attributes value max length
public private(set) var attributeValueLengthLimit: Int = Int.max

/// Returns the defaultSpanLimits.
public init() {}

Expand All @@ -30,6 +33,12 @@ public struct SpanLimits: Equatable {
return spanLimits
}

@discardableResult public func settingAttributeValueLengthLimit(_ number: UInt) -> Self {
var spanLimits = self
spanLimits.attributeValueLengthLimit = number > 0 ? Int(number) : 0
return spanLimits
}

@discardableResult public func settingEventCountLimit(_ number: UInt) -> Self {
var spanLimits = self
spanLimits.eventCountLimit = number > 0 ? Int(number) : 0
Expand All @@ -56,6 +65,7 @@ public struct SpanLimits: Equatable {

public static func == (lhs: SpanLimits, rhs: SpanLimits) -> Bool {
return lhs.attributeCountLimit == rhs.attributeCountLimit &&
lhs.attributeValueLengthLimit == rhs.attributeValueLengthLimit &&
lhs.eventCountLimit == rhs.eventCountLimit &&
lhs.linkCountLimit == rhs.linkCountLimit &&
lhs.attributePerEventCountLimit == rhs.attributePerEventCountLimit &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,21 @@ class RecordEventsReadableSpanTest: XCTestCase {
XCTAssertEqual(spanData.totalAttributeCount, 2 * maxNumberOfAttributes)
}

func testAttributesValueLength() {
let maxValueLength = 8
let spanLimits = SpanLimits().settingAttributeValueLengthLimit(UInt(maxValueLength))
let span = createTestSpan(config: spanLimits)
span.setAttribute(key: "max_value_length", value: .string("this is a big text that is longer than \(maxValueLength) characters"))
span.end()
let spanData = span.toSpanData()
if case .string(let value) = spanData.attributes["max_value_length"] {
XCTAssertEqual(span.maxValueLengthPerSpanAttribute, maxValueLength)
XCTAssertEqual(value, "this is ")
} else {
XCTFail()
}
}

func testDroppingAndAddingAttributes() {
let maxNumberOfAttributes = 8
let spanLimits = SpanLimits().settingAttributeCountLimit(UInt(maxNumberOfAttributes))
Expand Down
Loading