diff --git a/src/OpenTelemetry/Metrics/AggregatorStore.cs b/src/OpenTelemetry/Metrics/AggregatorStore.cs index d66833852..114095d9d 100644 --- a/src/OpenTelemetry/Metrics/AggregatorStore.cs +++ b/src/OpenTelemetry/Metrics/AggregatorStore.cs @@ -201,22 +201,25 @@ namespace OpenTelemetry.Metrics return -1; } - // Note: We are using storage from ThreadStatic for both the input order of tags and the sorted order of tags, + // Note: We are using storage from ThreadStatic (for upto MaxTagCacheSize tags) for both the input order of tags and the sorted order of tags, // so we need to make a deep copy for Dictionary storage. - var givenKeys = new string[length]; - tagKeys.CopyTo(givenKeys, 0); + if (length <= ThreadStaticStorage.MaxTagCacheSize) + { + var givenKeys = new string[length]; + tagKeys.CopyTo(givenKeys, 0); - var givenValues = new object[length]; - tagValues.CopyTo(givenValues, 0); + var givenValues = new object[length]; + tagValues.CopyTo(givenValues, 0); - var sortedTagKeys = new string[length]; - tempSortedTagKeys.CopyTo(sortedTagKeys, 0); + var sortedTagKeys = new string[length]; + tempSortedTagKeys.CopyTo(sortedTagKeys, 0); - var sortedTagValues = new object[length]; - tempSortedTagValues.CopyTo(sortedTagValues, 0); + var sortedTagValues = new object[length]; + tempSortedTagValues.CopyTo(sortedTagValues, 0); - givenTags = new Tags(givenKeys, givenValues); - sortedTags = new Tags(sortedTagKeys, sortedTagValues); + givenTags = new Tags(givenKeys, givenValues); + sortedTags = new Tags(sortedTagKeys, sortedTagValues); + } lock (this.tagsToMetricPointIndexDictionary) { diff --git a/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs b/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs index cee643afa..6e7c4005b 100644 --- a/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs +++ b/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs @@ -23,7 +23,7 @@ namespace OpenTelemetry.Metrics { internal sealed class ThreadStaticStorage { - private const int MaxTagCacheSize = 8; + internal const int MaxTagCacheSize = 8; [ThreadStatic] private static ThreadStaticStorage storage;