@@ -58,61 +58,6 @@ internal MetricAggregator(SentryOptions options, IMetricHub metricHub,
58
58
}
59
59
}
60
60
61
- internal static string GetMetricBucketKey ( MetricType type , string metricKey , MeasurementUnit unit ,
62
- IDictionary < string , string > ? tags )
63
- {
64
- var typePrefix = type . ToStatsdType ( ) ;
65
- var serializedTags = GetTagsKey ( tags ) ;
66
-
67
- return $ "{ typePrefix } _{ metricKey } _{ unit } _{ serializedTags } ";
68
- }
69
-
70
- internal static string GetTagsKey ( IDictionary < string , string > ? tags )
71
- {
72
- if ( tags == null || tags . Count == 0 )
73
- {
74
- return string . Empty ;
75
- }
76
-
77
- const char pairDelimiter = ',' ; // Delimiter between key-value pairs
78
- const char keyValueDelimiter = '=' ; // Delimiter between key and value
79
- const char escapeChar = '\\ ' ;
80
-
81
- var builder = new StringBuilder ( ) ;
82
-
83
- foreach ( var tag in tags )
84
- {
85
- // Escape delimiters in key and value
86
- var key = EscapeString ( tag . Key , pairDelimiter , keyValueDelimiter , escapeChar ) ;
87
- var value = EscapeString ( tag . Value , pairDelimiter , keyValueDelimiter , escapeChar ) ;
88
-
89
- if ( builder . Length > 0 )
90
- {
91
- builder . Append ( pairDelimiter ) ;
92
- }
93
-
94
- builder . Append ( key ) . Append ( keyValueDelimiter ) . Append ( value ) ;
95
- }
96
-
97
- return builder . ToString ( ) ;
98
-
99
- static string EscapeString ( string input , params char [ ] charsToEscape )
100
- {
101
- var escapedString = new StringBuilder ( input . Length ) ;
102
-
103
- foreach ( var ch in input )
104
- {
105
- if ( charsToEscape . Contains ( ch ) )
106
- {
107
- escapedString . Append ( escapeChar ) ; // Prefix with escape character
108
- }
109
- escapedString . Append ( ch ) ;
110
- }
111
-
112
- return escapedString . ToString ( ) ;
113
- }
114
- }
115
-
116
61
/// <inheritdoc cref="IMetricAggregator.Increment"/>
117
62
public void Increment ( string key ,
118
63
double value = 1.0 ,
@@ -186,19 +131,28 @@ private void Emit(
186
131
timestamp ??= DateTimeOffset . UtcNow ;
187
132
unit ??= MeasurementUnit . None ;
188
133
134
+ var updatedTags = tags != null ? new Dictionary < string , string > ( tags ) : new Dictionary < string , string > ( ) ;
135
+ updatedTags . AddIfNotNullOrEmpty ( "release" , _options . Release ) ;
136
+ updatedTags . AddIfNotNullOrEmpty ( "environment" , _options . Environment ) ;
137
+ var span = _metricHub . GetSpan ( ) ;
138
+ if ( span ? . GetTransaction ( ) is { } transaction )
139
+ {
140
+ updatedTags . AddIfNotNullOrEmpty ( "transaction" , transaction . TransactionName ) ;
141
+ }
142
+
189
143
Func < string , Metric > addValuesFactory = type switch
190
144
{
191
- MetricType . Counter => _ => new CounterMetric ( key , value , unit . Value , tags , timestamp ) ,
192
- MetricType . Gauge => _ => new GaugeMetric ( key , value , unit . Value , tags , timestamp ) ,
193
- MetricType . Distribution => _ => new DistributionMetric ( key , value , unit . Value , tags , timestamp ) ,
194
- MetricType . Set => _ => new SetMetric ( key , ( int ) value , unit . Value , tags , timestamp ) ,
145
+ MetricType . Counter => _ => new CounterMetric ( key , value , unit . Value , updatedTags , timestamp ) ,
146
+ MetricType . Gauge => _ => new GaugeMetric ( key , value , unit . Value , updatedTags , timestamp ) ,
147
+ MetricType . Distribution => _ => new DistributionMetric ( key , value , unit . Value , updatedTags , timestamp ) ,
148
+ MetricType . Set => _ => new SetMetric ( key , ( int ) value , unit . Value , updatedTags , timestamp ) ,
195
149
_ => throw new ArgumentOutOfRangeException ( nameof ( type ) , type , "Unknown MetricType" )
196
150
} ;
197
151
198
152
var timeBucket = GetOrAddTimeBucket ( timestamp . Value . GetTimeBucketKey ( ) ) ;
199
153
200
154
timeBucket . AddOrUpdate (
201
- GetMetricBucketKey ( type , key , unit . Value , tags ) ,
155
+ MetricHelper . GetMetricBucketKey ( type , key , unit . Value , updatedTags ) ,
202
156
addValuesFactory ,
203
157
( _ , metric ) =>
204
158
{
@@ -223,6 +177,16 @@ private void Emit(
223
177
{
224
178
RecordCodeLocation ( type , key , unit . Value , stackLevel + 1 , timestamp . Value ) ;
225
179
}
180
+
181
+ switch ( span )
182
+ {
183
+ case TransactionTracer transactionTracer :
184
+ transactionTracer . MetricsSummary . Add ( type , key , value , unit , tags ) ;
185
+ break ;
186
+ case SpanTracer spanTracer :
187
+ spanTracer . MetricsSummary . Add ( type , key , value , unit , tags ) ;
188
+ break ;
189
+ }
226
190
}
227
191
228
192
private ConcurrentDictionary < string , Metric > GetOrAddTimeBucket ( long bucketKey )
0 commit comments