-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlimits.dart
109 lines (94 loc) · 3.99 KB
/
limits.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information
import 'package:meta/meta.dart';
import '../../../api.dart' as api;
import '../../../sdk.dart' as sdk;
import '../../experimental_sdk.dart' as sdk;
/// Applies given [sdk.SpanLimits] to a list of [api.SpanLink]s.
@protected
List<api.SpanLink> applyLinkLimits(List<api.SpanLink> links, sdk.SpanLimits limits) {
final spanLink = <api.SpanLink>[];
for (final link in links) {
if (spanLink.length >= limits.maxNumLink) {
break;
}
if (!link.context.isValid) continue;
final linkAttributes = <api.Attribute>[];
// make sure override duplicated attributes in the list
final attributeMap = <String, int>{};
var droppedAttributes = 0;
for (final attr in link.attributes) {
// if attributes num is already greater than maxNumAttributesPerLink
// and this key doesn't exist in the list, drop it.
if (attributeMap.length >= limits.maxNumAttributesPerLink && !attributeMap.containsKey(attr.key)) {
droppedAttributes++;
continue;
}
// apply maxNumAttributeLength limit.
final trimmedAttr = applyAttributeLimits(attr, limits);
// if this key has been added before, find its index,
// and replace it with new value.
final idx = attributeMap[attr.key];
if (idx != null) {
linkAttributes[idx] = trimmedAttr;
} else {
// record this new key's index with linkAttributes length,
// and add this new attr in linkAttributes.
attributeMap[attr.key] = linkAttributes.length;
linkAttributes.add(trimmedAttr);
}
}
spanLink.add(api.SpanLink(link.context, attributes: linkAttributes, droppedAttributes: droppedAttributes));
}
return spanLink;
}
/// Applies given [sdk.SpanLimits] to an [api.Attribute].
@protected
api.Attribute applyAttributeLimits(api.Attribute attr, sdk.SpanLimits limits) {
// if maxNumAttributeLength is less than zero, then it has unlimited length.
if (limits.maxNumAttributeLength < 0) return attr;
if (attr.value is String) {
attr = api.Attribute.fromString(
attr.key, applyAttributeLengthLimit(attr.value as String, limits.maxNumAttributeLength));
} else if (attr.value is List<String>) {
final listString = attr.value as List<String>;
for (var j = 0; j < listString.length; j++) {
listString[j] = applyAttributeLengthLimit(listString[j], limits.maxNumAttributeLength);
}
attr = api.Attribute.fromStringList(attr.key, listString);
}
return attr;
}
@protected
api.Attribute applyAttributeLimitsForLog(
api.Attribute attr,
sdk.LogRecordLimits limits,
) {
// if maxNumAttributeLength is less than zero, then it has unlimited length.
if (limits.attributeValueLengthLimit < 0) return attr;
if (attr.value is String) {
return (attr.value as String).length > limits.attributeValueLengthLimit
? api.Attribute.fromString(attr.key, (attr.value as String).substring(0, limits.attributeValueLengthLimit))
: attr;
} else if (attr.value is List<String>) {
final list = (attr.value as List<String>);
List<String>? truncated;
for (int i = 0; i < list.length; i++) {
final s = list[i];
if (s.length > limits.attributeValueLengthLimit) {
truncated ??= List<String>.from(list, growable: false);
truncated[i] = s.substring(0, limits.attributeValueLengthLimit);
}
}
if (truncated != null) {
return api.Attribute.fromStringList(attr.key, truncated);
}
}
return attr;
}
/// Truncate just strings which length is longer than configuration.
/// Reference: https://github.com/open-telemetry/opentelemetry-java/blob/14ffacd1cdd22f5aa556eeda4a569c7f144eadf2/sdk/common/src/main/java/io/opentelemetry/sdk/internal/AttributeUtil.java#L80
@protected
String applyAttributeLengthLimit(String value, int lengthLimit) {
return value.length > lengthLimit ? value.substring(0, lengthLimit) : value;
}