/*
 * Copyright 2019 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.quarkus.jaeger.runtime;

import java.util.concurrent.atomic.AtomicReference;

import io.jaegertracing.Configuration;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;

public class QuarkusJaegerTracer implements Tracer {

    static AtomicReference<Tracer> REF = new AtomicReference<>();

    public QuarkusJaegerTracer() {
    }

    @Override
    public String toString() {
        return tracer().toString();
    }

    Tracer tracer() {
        return REF.updateAndGet((orig) -> {
            if (orig != null) {
                return orig;
            }
            return Configuration.fromEnv().withMetricsFactory(new QuarkusJaegerMetricsFactory()).getTracer();
        });
    }

    @Override
    public SpanBuilder buildSpan(String operationName) {
        return tracer().buildSpan(operationName);
    }

    @Override
    public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
        tracer().inject(spanContext, format, carrier);
    }

    @Override
    public <C> SpanContext extract(Format<C> format, C carrier) {
        return tracer().extract(format, carrier);
    }

    @Override
    public ScopeManager scopeManager() {
        return tracer().scopeManager();
    }

    @Override
    public Span activeSpan() {
        return tracer().activeSpan();
    }
}

@@ -631,61 +631,8 @@
 e;%0A%0A
-import java.util.concurrent.atomic.AtomicReference;%0A%0A
 impo
@@ -900,111 +900,46 @@
     
-static AtomicReference%3CTracer%3E REF = new AtomicReference%3C%3E();%0A%0A    public QuarkusJaegerTracer() %7B%0A    %7D
+private static volatile Tracer tracer;
 %0A%0A  
@@ -1028,16 +1028,31 @@
  %7D%0A%0A    
+private static 
 Tracer t
@@ -1073,104 +1073,122 @@
     
-return REF.updateAndGet((orig) -%3E %7B%0A            if (orig != null) %7B%0A                return orig;
+if (tracer == null) %7B%0A            synchronized (QuarkusJaegerTracer.class) %7B%0A                if (tracer == null) %7B
 %0A   
@@ -1192,22 +1192,16 @@
         
-    %7D%0A
         
@@ -1204,22 +1204,24 @@
         
-return
+tracer =
  Configu
@@ -1236,16 +1236,45 @@
 romEnv()
+%0A                            
 .withMet
@@ -1345,10 +1345,63 @@
     
-%7D)
+        %7D%0A            %7D%0A        %7D%0A        return tracer
 ;%0A  

Make sure that tracer is instantiated only once