Skip to content

Commit

Permalink
Added max value length per span attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mamunto committed Nov 7, 2024
1 parent d0006d3 commit ad03f89
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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 @@ -251,7 +254,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

0 comments on commit ad03f89

Please sign in to comment.