/*
 * 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 org.jboss.shamrock.jaeger.runtime;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;

import io.jaegertracing.internal.metrics.Counter;
import io.jaegertracing.internal.metrics.Gauge;
import io.jaegertracing.internal.metrics.Timer;
import io.jaegertracing.spi.MetricsFactory;
import io.smallrye.metrics.MetricRegistries;

public class ShamrockJaegerMetricsFactory implements MetricsFactory {

    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.BASE);
    
    @Override
    public Counter createCounter(final String name, final Map<String, String> tags) {
      org.eclipse.microprofile.metrics.Counter counter=
            registry.counter(meta(name, tags, MetricType.COUNTER));

      return new Counter() {
        @Override
        public void inc(long delta) {
          counter.inc(delta);
        }
      };
    }
  
    @Override
    public Timer createTimer(final String name, final Map<String, String> tags) {
      org.eclipse.microprofile.metrics.Timer timer=
            registry.timer(meta(name, tags, MetricType.TIMER));

      return new Timer() {
        @Override
        public void durationMicros(long time) {
          timer.update(time, TimeUnit.MICROSECONDS);
        }
      };
    }
  
    @Override
    public Gauge createGauge(final String name, final Map<String, String> tags) {
      JaegerGauge gauge=registry.register(meta(name, tags, MetricType.GAUGE), new JaegerGauge());

      return new Gauge() {
        @Override
        public void update(long amount) {
          gauge.update(amount);
        }
      };
    }

    static Metadata meta(final String name, final Map<String, String> tags, MetricType type) {
      Metadata meta = new Metadata(name, type);
      meta.setDisplayName(name);
      meta.setUnit("none");
      meta.setDescription(name);
      meta.setTags(new HashMap<String,String>(tags));
      meta.setReusable(true);
      return meta;
    }
  
    static class JaegerGauge implements org.eclipse.microprofile.metrics.Gauge<Long> {
      private AtomicLong value=new AtomicLong();
  
      public void update(long value) {
        this.value.set(value);
      }

      @Override
      public Long getValue() {
        return value.get();
      }
    }
}


@@ -1235,16 +1235,84 @@
 tory %7B%0A%0A
+    private static final String METRICS_PREFIX = %22jaeger_tracer_%22;%0A%0A
     Metr
@@ -2506,22 +2506,16 @@
 ta meta(
-final 
 String n
@@ -2566,24 +2566,146 @@
 ype type) %7B%0A
+      if (!name.startsWith(METRICS_PREFIX)) %7B%0A        // Add default prefix%0A        name = METRICS_PREFIX + name;%0A      %7D%0A
       Metada

Fix metrics prefix when doing native build