2222import java .util .List ;
2323import java .util .Map ;
2424import java .util .concurrent .ConcurrentHashMap ;
25+ import org .apache .skywalking .apm .agent .core .boot .ServiceManager ;
2526import org .apache .skywalking .apm .agent .core .meter .Counter ;
2627import org .apache .skywalking .apm .agent .core .meter .Histogram ;
2728import org .apache .skywalking .apm .agent .core .meter .MeterFactory ;
@@ -58,6 +59,12 @@ public class AgentSo11y {
5859 private static Histogram INTERCEPTOR_TIME_COST ;
5960
6061 public static void measureTracingContextCreation (boolean forceSampling , boolean ignoredTracingContext ) {
62+ if (!ServiceManager .INSTANCE .isBooted ()) {
63+ // Agent kernel services could be not-booted-yet as ServiceManager#INSTANCE#boot executed after agent
64+ // transfer initialization.
65+ // Skip when the services are not ready to avoid MeterService status is not initialized.
66+ return ;
67+ }
6168 if (forceSampling ) {
6269 if (ignoredTracingContext ) {
6370 if (PROPAGATED_IGNORE_CONTEXT_COUNTER == null ) {
@@ -98,6 +105,12 @@ public static void measureTracingContextCreation(boolean forceSampling, boolean
98105 }
99106
100107 public static void measureTracingContextCompletion (boolean ignoredTracingContext ) {
108+ if (!ServiceManager .INSTANCE .isBooted ()) {
109+ // Agent kernel services could be not-booted-yet as ServiceManager#INSTANCE#boot executed after agent
110+ // transfer initialization.
111+ // Skip when the services are not ready to avoid MeterService status is not initialized.
112+ return ;
113+ }
101114 if (ignoredTracingContext ) {
102115 if (FINISH_IGNORE_CONTEXT_COUNTER == null ) {
103116 FINISH_IGNORE_CONTEXT_COUNTER = MeterFactory .counter ("finished_ignored_context_counter" ).build ();
@@ -112,6 +125,12 @@ public static void measureTracingContextCompletion(boolean ignoredTracingContext
112125 }
113126
114127 public static void measureLeakedTracingContext (boolean ignoredTracingContext ) {
128+ if (!ServiceManager .INSTANCE .isBooted ()) {
129+ // Agent kernel services could be not-booted-yet as ServiceManager#INSTANCE#boot executed after agent
130+ // transfer initialization.
131+ // Skip when the services are not ready to avoid MeterService status is not initialized.
132+ return ;
133+ }
115134 if (ignoredTracingContext ) {
116135 if (LEAKED_IGNORE_CONTEXT_COUNTER == null ) {
117136 LEAKED_IGNORE_CONTEXT_COUNTER = MeterFactory
@@ -132,6 +151,12 @@ public static void measureLeakedTracingContext(boolean ignoredTracingContext) {
132151 }
133152
134153 public static void durationOfInterceptor (double timeCostInNanos ) {
154+ if (!ServiceManager .INSTANCE .isBooted ()) {
155+ // Agent kernel services could be not-booted-yet as ServiceManager#INSTANCE#boot executed after agent
156+ // transfer initialization.
157+ // Skip when the services are not ready to avoid MeterService status is not initialized.
158+ return ;
159+ }
135160 if (INTERCEPTOR_TIME_COST == null ) {
136161 INTERCEPTOR_TIME_COST = MeterFactory
137162 .histogram ("tracing_context_performance" )
@@ -142,6 +167,12 @@ public static void durationOfInterceptor(double timeCostInNanos) {
142167 }
143168
144169 public static void errorOfPlugin (String pluginName , String interType ) {
170+ if (!ServiceManager .INSTANCE .isBooted ()) {
171+ // Agent kernel services could be not-booted-yet as ServiceManager#INSTANCE#boot executed after agent
172+ // transfer initialization.
173+ // Skip when the services are not ready to avoid MeterService status is not initialized.
174+ return ;
175+ }
145176 Counter counter = ERROR_COUNTER_CACHE .computeIfAbsent (pluginName + interType , key -> MeterFactory
146177 .counter ("interceptor_error_counter" )
147178 .tag ("plugin_name" , pluginName )
0 commit comments