com.alipay.lookout.api.Id Java Examples

The following examples show how to use com.alipay.lookout.api.Id. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PromWriterTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrintPromText() {
    PromWriter promWriter = new PromWriter();
    Id id = new DefaultRegistry().createId("aa.bb.cc");

    Date date = new Date();
    LookoutMeasurement measurement = new LookoutMeasurement(date, id);
    int i = 3;
    measurement.addTag("tagk" + i, "tagv" + i);
    measurement.put("xxx" + i, i);
    measurement.put("yyy" + i, i);
    measurement.put("zzz" + i, i);

    String str = promWriter.printFromLookoutMeasurement(measurement);
    System.out.println(str);
    Assert.assertTrue(str.contains("aa_bb_cc_xxx3{tagk3=\"tagv3\"} 3"));
}
 
Example #2
Source File: AbstractExporter.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
public AbstractExporter(String name, Registry registry, DataType type) {
    this.name = name;
    this.registry = registry;
    Map<String, String> tags = new HashMap<>();
    tags.put("exporter", name);
    // 转成小写, 跟之前保持一致
    tags.put("type", type.name().toLowerCase());
    Id id = registry.createId("exporter.stats", tags);
    MixinMetric mm = registry.mixinMetric(id);

    // 统计导出个数(导出的metric条数)
    count = mm.counter("count");

    // 统计导出次数(每次会导出多条)
    batch = mm.counter("batch");

    // 统计失败次数
    fail = mm.counter("fail");

    // 统计请求耗时
    time = mm.timer("time");
}
 
Example #3
Source File: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<Metric<?>> findAll() {
    final Iterator<com.alipay.lookout.api.Metric> lookoutIt = this.springBootActuatorRegistry
        .iterator();
    return new Iterable<Metric<?>>() {
        @Override
        public Iterator<Metric<?>> iterator() {
            List<Metric<?>> metricsRes = new LinkedList<Metric<?>>();
            while (lookoutIt.hasNext()) {
                com.alipay.lookout.api.Metric lookoutMetric = lookoutIt.next();
                Id id = lookoutMetric.id();
                Collection<Metric> metricList = findMetricsById(id);
                if (metricList != null && metricList.size() > 0) {
                    for (Metric metric : metricList) {
                        metricsRes.add(metric);
                    }
                }
            }
            return metricsRes.iterator();
        }
    };
}
 
Example #4
Source File: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
/***
 * Spring Boot Standard interface Implementation
 * @param metricName name
 * @return Converted Actuator Metric
 */
@Override
public Metric<?> findOne(String metricName) {
    if (StringUtils.isBlank(metricName)) {
        return null;
    }
    //Standard Actuator Implementation
    Id id = this.springBootActuatorRegistry.createId(metricName);
    List<Metric> metricList = findMetricsById(id);
    if (metricList != null && metricList.size() > 0) {
        //Converted to lookout Metrics,default first
        return metricList.get(0);
    } else {
        return null;
    }
}
 
Example #5
Source File: LookoutSpringBootMetricsImpl.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
public void submit(String metricName, double value) {
    if (StringUtils.isBlank(metricName)) {
        return;
    }
    metricName = wrapName(LOOKOUT_GAUGE_PREFIX, metricName);
    SimpleLookoutGauge gauge = this.gauges.get(metricName);
    if (gauge == null) {
        SimpleLookoutGauge newGauge = new SimpleLookoutGauge(value);
        gauge = this.gauges.putIfAbsent(metricName, newGauge);
        if (gauge == null) {
            Id id = this.registry.createId(metricName);
            this.registry.gauge(id, newGauge);
            return;
        }
    }
    gauge.setValue(value);
}
 
Example #6
Source File: JvmClassesMetricsImporter.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
public void register(Registry registry) {
    Id id = registry.createId("jvm.classes");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("unloaded", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getClassLoadingMXBean().getUnloadedClassCount();
        }
    });
    mixin.gauge("total", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getClassLoadingMXBean().getTotalLoadedClassCount();
        }
    });
    mixin.gauge("loaded", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return ManagementFactory.getClassLoadingMXBean().getLoadedClassCount();
        }
    });

}
 
Example #7
Source File: AbstractTopMetric.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
private void pushSafe(TreeSet<TopUtil.Entry<Id, Long>> set, TopUtil.Entry<Id, Long> e) {
    if (!set.isEmpty()) {
        boolean replaceable = false;
        TopUtil.Entry<Id, Long> boundaryTarget = null;
        if (order == TopUtil.Order.DESC) {
            boundaryTarget = set.first();
            replaceable = boundaryTarget.getValue() < e.getValue();
        } else {
            boundaryTarget = set.last();
            replaceable = boundaryTarget.getValue() > e.getValue();
        }
        if (replaceable & set.size() >= maxNumber) {
            remove(set, boundaryTarget);
        }
        if (!replaceable & set.size() >= maxNumber) {
            return;//不add
        }
    }
    add(set, e);
}
 
Example #8
Source File: NetTrafficMetricsImporter.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    for (final Map.Entry<String, Long[]> entry : statByFace.entrySet()) {
        final String face = entry.getKey();
        Id id = registry.createId("os.net.stats").withTag("intfc", face);
        MixinMetric mixin = registry.mixinMetric(id);

        for (int i = 0; i < entry.getValue().length; i++) {
            final int index = i;
            mixin.gauge(FIELDS[index], new Gauge<Long>() {
                @Override
                public Long value() {
                    loadIfNessesary();
                    return statByFace.get(face)[index];
                }
            });
        }
    }
}
 
Example #9
Source File: LookoutMetricTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookoutCounter() {
    ManualClock clock = new ManualClock();
    //StepRegistry r = new StepRegistry(clock, new LookoutConfig());
    Id id = new DefaultRegistry().createId("aa");
    LookoutCounter counter = new LookoutCounter(id, clock, 500);
    counter.inc();
    counter.inc(7);
    counter.dec();
    counter.dec(2);

    clock.setWallTime(500);
    String str = "";
    for (Object m : counter.measure().measurements()) {
        System.out.println(m);
        str += m;
    }
    Assert.assertTrue(str.contains("count:5"));
    Assert.assertTrue(str.contains("rate:10.0"));
}
 
Example #10
Source File: SimpleLookoutClientDemo.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    LookoutRegistry lookoutRegistry = new LookoutRegistry(new StdoutObserver());
    //构建一个全局的客户端实例
    final SimpleLookoutClient client = new SimpleLookoutClient("appName", lookoutRegistry);
    client.addCommonTags(new BasicTag("instant", "machine-a"));
    //本地观察metrics定时打印
    lookoutRegistry.addMetricObserver(new StdoutObserver());

    //获取配置类,如果需要修改配置
    MetricConfig lookoutConfig=client.getLookoutConfig();
    lookoutConfig.setProperty(LookoutConfig.LOOKOUT_AGENT_HOST_ADDRESS,"127.0.0.1");

    //具体使用示例
    Id id = client.getRegistry().createId("http_requests_total");
    Counter counter = client.getRegistry().counter(id);
    counter.inc();

    try {
        Thread.sleep(60000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example #11
Source File: AbstractWebfluxImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
    Map<String, String> tags = new HashMap<>();
    tags.put("importer", name);
    tags.put("type", "metric");
    Id id = registry.createId("importer.stats", tags);
    MixinMetric mm = registry.mixinMetric(id);
    counter = mm.counter("count");
    size = mm.distributionSummary("size");
}
 
Example #12
Source File: AbstractTopMetric.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(TopUtil.Entry<Id, Long> o1,
                   TopUtil.Entry<Id, Long> o2) {
    return o1.getValue() > o2
        .getValue() ? 1
        : (o1.getValue() < o2
            .getValue() ? -1
            : 0);
}
 
Example #13
Source File: JvmSystemPropertiesInfoMetricImporterTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * Method: register(Registry registry)
 */
@Test
public void testRegister() throws Exception {
    Registry registry = new DefaultRegistry();
    JvmSystemPropertiesInfoMetricImporter jvmSystemPropertiesInfoMetricImporter = new JvmSystemPropertiesInfoMetricImporter();
    jvmSystemPropertiesInfoMetricImporter.register(registry);
    Id idSys = registry.createId(LookoutIdNameConstants.JVM_SYSTEM_PROP_NAME);
    InfoWrapper infoWrapper = registry.get(idSys);
    assertEquals(System.getProperties(), infoWrapper.value());
    Id envId = registry.createId(LookoutIdNameConstants.JVM_SYSTEM_ENV_NAME);
    InfoWrapper envWrapper = registry.get(envId);
    assertEquals(System.getenv(), envWrapper.value());
}
 
Example #14
Source File: FileSystemSpaceMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {

    Id basicId = registry.createId("instance.file.system");

    File[] roots = File.listRoots();
    // For each filesystem root;
    //TODO dynamic  File.listRoots() and for each;
    for (final File root : roots) {
        Id id = basicId.withTag("root", root.getAbsolutePath()).withTag(
            LookoutConstants.LOW_PRIORITY_TAG);
        MixinMetric mixin = registry.mixinMetric(id);
        mixin.gauge("total.space", new Gauge<Long>() {
            @Override
            public Long value() {
                return root.getTotalSpace();
            }
        });

        mixin.gauge("free.space", new Gauge<Long>() {
            @Override
            public Long value() {
                return root.getFreeSpace();
            }
        });
        mixin.gauge("usabe.space", new Gauge<Long>() {
            @Override
            public Long value() {
                return root.getUsableSpace();
            }
        });
    }
}
 
Example #15
Source File: JvmGcMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {
    Id id = registry.createId("jvm.gc");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("young.count", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.youngGCCount;
        }
    });
    mixin.gauge("young.time", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.youngGCTime;
        }
    });
    mixin.gauge("old.count", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.oldGCCount;
        }
    });
    mixin.gauge("old.time", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.oldGCTime;
        }
    });
}
 
Example #16
Source File: JvmMemoryMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {
    Id id = registry.createId("jvm.memory");
    MixinMetric mixin = registry.mixinMetric(id);
    heapImport(mixin);
    nonheapImport(mixin);
    codeCacheImport(mixin);
}
 
Example #17
Source File: AbstractTopMetric.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * record rollTimeStamp and clear cache;
 * 不会并发poll所以不需要加锁(即使有并发重复roll也没关系);
 *
 * @param rollStamp
 */
public void roll(long rollStamp) {
    if (rollStamp > lastRolledStamp) {
        lastRolledStamp = rollStamp;
        //clear cache set
        set = new TreeSet<TopUtil.Entry<Id, Long>>(comparator);
    }
}
 
Example #18
Source File: AbstractTopMetric.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
protected void pushAsync(Long value, Id timerId) {
    final TopUtil.Entry<Id, Long> entry = new TopUtil.Entry<Id, Long>(timerId, value);
    TopUtil.executor.execute(new Runnable() {
        @Override
        public void run() {
            lock.lock();
            try {
                pushSafe(set, entry);
            } finally {
                lock.unlock();
            }
        }
    });
}
 
Example #19
Source File: RpcLookoutId.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * create consumerId
 *
 * @return consumerId
 */
public Id fetchConsumerStatId() {

    if (consumerId == null) {
        synchronized (consumerIdLock) {
            if (consumerId == null) {
                consumerId = Lookout.registry().createId("rpc.consumer.service.stats");
            }
        }
    }

    return consumerId;
}
 
Example #20
Source File: LookoutSpringBootMetricsImpl.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void decrement(String metricName) {
    if (StringUtils.isBlank(metricName)) {
        return;
    }
    metricName = wrapName(LOOKOUT_COUNTER_PREFIX, metricName);
    Id id = this.registry.createId(metricName);
    Counter counter = this.registry.counter(id);
    counter.dec();
}
 
Example #21
Source File: BasePersistQueueProcessor.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
protected void doStart() {
    try {
        queue = new MappedFilePersistentQueue(dir, queueName);
        Map<String, String> tags = new HashMap<>();
        tags.put("name", queueName);
        Id id = registry.createId(QUEUE_SIZE_METRIC_NAME, tags);
        registry.gauge(id, (Gauge<Number>) () -> queue.getBackFileSize() / BYTES_OF_MB);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    ThreadFactory tf = new ThreadFactoryBuilder()
            .setNameFormat("queue_processor_poller-%d")
            .setThreadFactory(DequeueThread::new)
            .build();
    executor = Executors.newFixedThreadPoolExecutor(
            threads,
            100,
            "queue_processor_poller",
            tf,
            new ThreadPoolExecutor.AbortPolicy(),
            registry);

    // 启动线程
    for (int i = 0; i < threads; i++) {
        this.executor.execute(this::poll);
    }
}
 
Example #22
Source File: RpcLookout.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Collect the RPC server information.
 *
 * @param rpcServerMetricsModel server information model
 */
public void collectServer(RpcServerLookoutModel rpcServerMetricsModel) {

    try {
        Id methodProviderId = createMethodProviderId(rpcServerMetricsModel);
        MixinMetric methodProviderMetric = Lookout.registry().mixinMetric(methodProviderId);

        recordCounterAndTimer(methodProviderMetric, rpcServerMetricsModel);

    } catch (Throwable t) {
        LOGGER.error(LogCodes.getLog(LogCodes.ERROR_METRIC_REPORT_ERROR), t);
    }
}
 
Example #23
Source File: RpcLookoutId.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public Id fetchProviderPubId() {
    if (providerConfigId == null) {
        synchronized (providerConfigIdLock) {
            if (providerConfigId == null) {
                providerConfigId = Lookout.registry().createId("rpc.provider.info.stats");
            }
        }
    }
    return providerConfigId;
}
 
Example #24
Source File: RpcLookout.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Create consumer id
 *
 * @param model RpcClientLookoutModel
 * @return Id
 */
private Id createMethodConsumerId(RpcClientLookoutModel model) {

    Map<String, String> tags = new HashMap<String, String>(6);

    tags.put("app", StringUtils.defaultString(model.getApp()));
    tags.put("service", StringUtils.defaultString(model.getService()));
    tags.put("method", StringUtils.defaultString(model.getMethod()));
    tags.put("protocol", StringUtils.defaultString(model.getProtocol()));
    tags.put("invoke_type", StringUtils.defaultString(model.getInvokeType()));
    tags.put("target_app", StringUtils.defaultString(model.getTargetApp()));

    return rpcLookoutId.fetchConsumerStatId().withTags(tags);
}
 
Example #25
Source File: MemoryStatsMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    Id id = registry.createId("os.memory.stats");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("total.bytes", new Gauge<Long>() {
        @Override
        public Long value() {
            loadIfNessesary();
            return memstats.memTotal;
        }
    });

    mixin.gauge("free.bytes", new Gauge<Long>() {
        @Override
        public Long value() {
            loadIfNessesary();
            return memstats.memFree;
        }
    });
    mixin.gauge("buffers.bytes", new Gauge<Long>() {
        @Override
        public Long value() {
            loadIfNessesary();
            return memstats.buffers;
        }
    });
    mixin.gauge("cached.bytes", new Gauge<Long>() {
        @Override
        public Long value() {
            loadIfNessesary();
            return memstats.cached;
        }
    });

}
 
Example #26
Source File: SystemLoadMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    Id id = registry.createId("os.systemload.average");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("1min", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return loadAvg[LoadAvg.ONE_MIN.ordinal()];
        }
    });

    mixin.gauge("5min", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return loadAvg[LoadAvg.FIVE_MIN.ordinal()];
        }
    });

    mixin.gauge("15min", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return loadAvg[LoadAvg.FIFTEEN_MIN.ordinal()];
        }
    });
}
 
Example #27
Source File: DefaultLookoutClientTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultLookoutClient_addExtMetrics() throws Exception {
    LookoutConfig lookoutConfig = new LookoutConfig();
    DefaultLookoutClient client = new DefaultLookoutClient("demo");
    MetricRegistry r = new DefaultRegistry(lookoutConfig);
    client.addRegistry(r);
    client.registerExtendedMetrics();
    Id id = r.createId("jvm.gc");
    Assert.assertNotNull(client.getRegistry().get(id));
    client.close();
}
 
Example #28
Source File: LookoutStarterTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropwizardMetrics() {
    Id id = registry.createId("test_status").withTag("k1", "v1");
    MixinMetric mixinMetric = registry.mixinMetric(id);
    mixinMetric.distributionSummary("dstest").record(100);
    mixinMetric.counter("counttest").inc();
    mixinMetric.timer("timetest").record(2, TimeUnit.SECONDS);
    mixinMetric.gauge("guagetest", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return 1;
        }
    });

    Assert.assertTrue(registry instanceof CompositeRegistry);
    CompositeRegistry compositeRegistry = (CompositeRegistry) registry;
    DropWizardMetricsRegistry dwr = null;
    for (Registry r : compositeRegistry.getRegistries()) {
        if (r instanceof DropWizardMetricsRegistry) {
            dwr = (DropWizardMetricsRegistry) r;
            break;
        }
    }

    MixinMetric mixinMetric2 = dwr.mixinMetric(id);
    Assert.assertEquals(1, mixinMetric2.counter("counttest").count());
    Assert.assertEquals(1, mixinMetric2.timer("timetest").count());
    Assert.assertEquals(1, mixinMetric2.distributionSummary("dstest").count());

}
 
Example #29
Source File: LookoutSpringBootMetricsImpl.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void reset(String metricName) {
    if (StringUtils.isBlank(metricName)) {
        return;
    }
    metricName = wrapName(LOOKOUT_COUNTER_PREFIX, metricName);
    Id id = this.registry.createId(metricName);
    this.registry.removeMetric(id);
}
 
Example #30
Source File: PromWriterTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrintPromTextFromDefaultRegistry() {
    PromWriter promWriter = new PromWriter();
    DefaultRegistry r = new DefaultRegistry();
    Id id = r.createId("aa.bb.cc").withTag("tagk1", "tagv1").withTag("tagk2", "tagv2");
    Timer timer = r.timer(id);
    timer.record(2, TimeUnit.SECONDS);
    Iterator<Metric> iterator = r.iterator();
    String str = "";
    while (iterator.hasNext()) {
        str += promWriter.printFromIndicator(iterator.next().measure());
    }
    System.out.println(str);
}