@@ -69,63 +69,56 @@ public ComponentsMetrics(IMeterFactory meterFactory)
69
69
70
70
public void Navigation ( string componentType , string route )
71
71
{
72
- var tags = new TagList
73
- {
74
- { "aspnetcore.components.type" , componentType ?? "unknown" } ,
75
- { "aspnetcore.components.route" , route ?? "unknown" } ,
76
- } ;
72
+ var tags = new TagList ( ) ;
73
+ AddComponentTypeTag ( ref tags , componentType ) ;
74
+ AddRouteTag ( ref tags , route ) ;
77
75
78
76
_navigationCount . Add ( 1 , tags ) ;
79
77
}
80
78
81
79
public async Task CaptureEventDuration ( Task task , long startTimestamp , string ? componentType , string ? methodName , string ? attributeName )
82
80
{
83
- var tags = new TagList
84
- {
85
- { "aspnetcore.components.type" , componentType ?? "unknown" } ,
86
- { "code.function.name" , methodName ?? "unknown" } ,
87
- { "aspnetcore.components.attribute.name" , attributeName ?? "unknown" }
88
- } ;
81
+ var tags = new TagList ( ) ;
82
+ AddComponentTypeTag ( ref tags , componentType ) ;
83
+ AddMethodNameTag ( ref tags , methodName ) ;
84
+ AddAttributeNameTag ( ref tags , attributeName ) ;
89
85
90
86
try
91
87
{
92
88
await task ;
93
89
}
94
90
catch ( Exception ex )
95
91
{
96
- tags . Add ( "error.type" , ex . GetType ( ) . FullName ?? "unknown" ) ;
92
+ AddErrorTag ( ref tags , ex ) ;
97
93
}
98
94
var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
99
95
_eventDuration . Record ( duration . TotalSeconds , tags ) ;
100
96
}
101
97
102
98
public void FailEventSync ( Exception ex , long startTimestamp , string ? componentType , string ? methodName , string ? attributeName )
103
99
{
104
- var tags = new TagList
105
- {
106
- { "aspnetcore.components.type" , componentType ?? "unknown" } ,
107
- { "code.function.name" , methodName ?? "unknown" } ,
108
- { "aspnetcore.components.attribute.name" , attributeName ?? "unknown" } ,
109
- { "error.type" , ex . GetType ( ) . FullName ?? "unknown" }
110
- } ;
100
+ var tags = new TagList ( ) ;
101
+ AddComponentTypeTag ( ref tags , componentType ) ;
102
+ AddMethodNameTag ( ref tags , methodName ) ;
103
+ AddAttributeNameTag ( ref tags , attributeName ) ;
104
+ AddErrorTag ( ref tags , ex ) ;
105
+
111
106
var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
112
107
_eventDuration . Record ( duration . TotalSeconds , tags ) ;
113
108
}
114
109
115
110
public async Task CaptureParametersDuration ( Task task , long startTimestamp , string ? componentType )
116
111
{
117
- var tags = new TagList
118
- {
119
- { "aspnetcore.components.type" , componentType ?? "unknown" } ,
120
- } ;
112
+ var tags = new TagList ( ) ;
113
+ AddComponentTypeTag ( ref tags , componentType ) ;
121
114
122
115
try
123
116
{
124
117
await task ;
125
118
}
126
119
catch ( Exception ex )
127
120
{
128
- tags . Add ( "error.type" , ex . GetType ( ) . FullName ?? "unknown" ) ;
121
+ AddErrorTag ( ref tags , ex ) ;
129
122
}
130
123
var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
131
124
_parametersDuration . Record ( duration . TotalSeconds , tags ) ;
@@ -134,11 +127,10 @@ public async Task CaptureParametersDuration(Task task, long startTimestamp, stri
134
127
public void FailParametersSync ( Exception ex , long startTimestamp , string ? componentType )
135
128
{
136
129
var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
137
- var tags = new TagList
138
- {
139
- { "aspnetcore.components.type" , componentType ?? "unknown" } ,
140
- { "error.type" , ex . GetType ( ) . FullName ?? "unknown" }
141
- } ;
130
+ var tags = new TagList ( ) ;
131
+ AddComponentTypeTag ( ref tags , componentType ) ;
132
+ AddErrorTag ( ref tags , ex ) ;
133
+
142
134
_parametersDuration . Record ( duration . TotalSeconds , tags ) ;
143
135
}
144
136
@@ -152,7 +144,7 @@ public async Task CaptureBatchDuration(Task task, long startTimestamp, int diffL
152
144
}
153
145
catch ( Exception ex )
154
146
{
155
- tags . Add ( "error.type" , ex . GetType ( ) . FullName ?? "unknown" ) ;
147
+ AddErrorTag ( ref tags , ex ) ;
156
148
}
157
149
var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
158
150
_batchDuration . Record ( duration . TotalSeconds , tags ) ;
@@ -162,10 +154,9 @@ public async Task CaptureBatchDuration(Task task, long startTimestamp, int diffL
162
154
public void FailBatchSync ( Exception ex , long startTimestamp )
163
155
{
164
156
var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
165
- var tags = new TagList
166
- {
167
- { "error.type" , ex . GetType ( ) . FullName ?? "unknown" }
168
- } ;
157
+ var tags = new TagList ( ) ;
158
+ AddErrorTag ( ref tags , ex ) ;
159
+
169
160
_batchDuration . Record ( duration . TotalSeconds , tags ) ;
170
161
}
171
162
@@ -174,4 +165,45 @@ public void Dispose()
174
165
_meter . Dispose ( ) ;
175
166
_lifeCycleMeter . Dispose ( ) ;
176
167
}
168
+
169
+ private static void AddComponentTypeTag ( ref TagList tags , string ? componentType )
170
+ {
171
+ if ( componentType != null )
172
+ {
173
+ tags . Add ( "aspnetcore.components.type" , componentType ) ;
174
+ }
175
+ }
176
+
177
+ private static void AddRouteTag ( ref TagList tags , string ? route )
178
+ {
179
+ if ( route != null )
180
+ {
181
+ tags . Add ( "aspnetcore.components.route" , route ) ;
182
+ }
183
+ }
184
+
185
+ private static void AddMethodNameTag ( ref TagList tags , string ? methodName )
186
+ {
187
+ if ( methodName != null )
188
+ {
189
+ tags . Add ( "code.function.name" , methodName ) ;
190
+ }
191
+ }
192
+
193
+ private static void AddAttributeNameTag ( ref TagList tags , string ? attributeName )
194
+ {
195
+ if ( attributeName != null )
196
+ {
197
+ tags . Add ( "aspnetcore.components.attribute.name" , attributeName ) ;
198
+ }
199
+ }
200
+
201
+ private static void AddErrorTag ( ref TagList tags , Exception ? exception )
202
+ {
203
+ var errorType = exception ? . GetType ( ) . FullName ;
204
+ if ( errorType is not null )
205
+ {
206
+ tags . Add ( "error.type" , errorType ) ;
207
+ }
208
+ }
177
209
}
0 commit comments