@@ -69,63 +69,56 @@ public ComponentsMetrics(IMeterFactory meterFactory)
6969
7070 public void Navigation ( string componentType , string route )
7171 {
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 ) ;
7775
7876 _navigationCount . Add ( 1 , tags ) ;
7977 }
8078
8179 public async Task CaptureEventDuration ( Task task , long startTimestamp , string ? componentType , string ? methodName , string ? attributeName )
8280 {
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 ) ;
8985
9086 try
9187 {
9288 await task ;
9389 }
9490 catch ( Exception ex )
9591 {
96- tags . Add ( "error.type" , ex . GetType ( ) . FullName ?? "unknown" ) ;
92+ AddErrorTag ( ref tags , ex ) ;
9793 }
9894 var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
9995 _eventDuration . Record ( duration . TotalSeconds , tags ) ;
10096 }
10197
10298 public void FailEventSync ( Exception ex , long startTimestamp , string ? componentType , string ? methodName , string ? attributeName )
10399 {
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+
111106 var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
112107 _eventDuration . Record ( duration . TotalSeconds , tags ) ;
113108 }
114109
115110 public async Task CaptureParametersDuration ( Task task , long startTimestamp , string ? componentType )
116111 {
117- var tags = new TagList
118- {
119- { "aspnetcore.components.type" , componentType ?? "unknown" } ,
120- } ;
112+ var tags = new TagList ( ) ;
113+ AddComponentTypeTag ( ref tags , componentType ) ;
121114
122115 try
123116 {
124117 await task ;
125118 }
126119 catch ( Exception ex )
127120 {
128- tags . Add ( "error.type" , ex . GetType ( ) . FullName ?? "unknown" ) ;
121+ AddErrorTag ( ref tags , ex ) ;
129122 }
130123 var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
131124 _parametersDuration . Record ( duration . TotalSeconds , tags ) ;
@@ -134,11 +127,10 @@ public async Task CaptureParametersDuration(Task task, long startTimestamp, stri
134127 public void FailParametersSync ( Exception ex , long startTimestamp , string ? componentType )
135128 {
136129 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+
142134 _parametersDuration . Record ( duration . TotalSeconds , tags ) ;
143135 }
144136
@@ -152,7 +144,7 @@ public async Task CaptureBatchDuration(Task task, long startTimestamp, int diffL
152144 }
153145 catch ( Exception ex )
154146 {
155- tags . Add ( "error.type" , ex . GetType ( ) . FullName ?? "unknown" ) ;
147+ AddErrorTag ( ref tags , ex ) ;
156148 }
157149 var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
158150 _batchDuration . Record ( duration . TotalSeconds , tags ) ;
@@ -162,10 +154,9 @@ public async Task CaptureBatchDuration(Task task, long startTimestamp, int diffL
162154 public void FailBatchSync ( Exception ex , long startTimestamp )
163155 {
164156 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+
169160 _batchDuration . Record ( duration . TotalSeconds , tags ) ;
170161 }
171162
@@ -174,4 +165,45 @@ public void Dispose()
174165 _meter . Dispose ( ) ;
175166 _lifeCycleMeter . Dispose ( ) ;
176167 }
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+ }
177209}
0 commit comments