private <M extends Metric> M readMetric(MetricId metricId, Class<M> metricClass) { Metric existingMetrics = lookup(metricId); if (existingMetrics != null) { if (metricClass.isInstance(existingMetrics)) { return (M) existingMetrics; } else { throw new IllegalArgumentException(metricId + " is already used for a different type " + "of metric: " + metricClass.getSimpleName()); } } return null; }
@Override public Counter register(MetricRegistry registry) { return registry.counter(new MetricId(name, description, tags)); } }
@Override public Gauge register(MetricRegistry registry) { return registry.gauge(new MetricId(name, description, tags), statisticConfigs.toArray( new StatisticConfig[statisticConfigs.size()])); }
private <M extends Metric> M getOrCreate(MetricId id, Class<M> metricClass, Supplier<M> metricSupplier) { Metric metric = readMetric(id, metricClass); if (metric == null) { Metric newMetric = metricSupplier.get(); return writeMetricIfNotExists(newMetric, metricClass); } else { return (M) metric; } }
/** * Registers the counter metrics instance. * * @param counter The {@link Counter} instance. * @return A existing or a newly registered {@link Counter} metric. */ public Counter register(Counter counter) { return register(counter, Counter.class); }
long duration = System.nanoTime() - startTime; getInprogressGauge(connectorName, mainTagSet).decrement(); metricRegistry.gauge(new MetricId(connectorName + "_response_time_seconds", "Response Time", allTags), responseTimeStatisticConfigs).setValue(duration / 1E9); metricRegistry.counter(new MetricId(connectorName + "_requests_total", "Total number of requests", allTags)).increment(); metricRegistry.counter(new MetricId(connectorName + "_failed_requests_total", "Total number of failed requests", allTags)).increment();
@Override public Gauge build() { return DefaultMetricRegistry.getInstance().getMetricProvider(). newGauge(new MetricId(name, description, tags), statisticConfigs.toArray( new StatisticConfig[statisticConfigs.size()])); } }
/** * Use {@link Gauge#builder(String)}. * * @param id The {@link MetricId}. * @param statisticConfigs {@link StatisticConfig statistic configurations} to summarize gauge values. * @return A existing or a new {@link Gauge} metric. */ public Gauge gauge(MetricId id, StatisticConfig... statisticConfigs) { return getOrCreate(id, Gauge.class, () -> metricProvider.newGauge(id, statisticConfigs)); }
@Override public PolledGauge register(MetricRegistry registry) { return registry.polledGauge(new MetricId(name, description, tags), obj, valueFunction); }
private void unregister(Metric registerMetric, Class metricClass) { Metric metric = readMetric(registerMetric.getId(), metricClass); if (metric != null) { metrics.remove(registerMetric.getId()); } }
@Override public void beforeRunProgram(boolean service) { if (DefaultMetricRegistry.getInstance() == null) { ConfigRegistry configRegistry = ConfigRegistry.getInstance(); if (!configRegistry.getAsBoolean(CONFIG_METRICS_ENABLED)) { // Create default MetricRegistry with NoOpMetricProvider DefaultMetricRegistry.setInstance(new MetricRegistry(new NoOpMetricProvider())); return; } //load metric provider configured MetricProvider metricProvider = loadMetricProvider(configRegistry); // Initialize Metric Provider metricProvider.init(); // Create default MetricRegistry DefaultMetricRegistry.setInstance(new MetricRegistry(metricProvider)); // Register Ballerina specific metrics registerBallerinaMetrics(); //load metric reporter configured MetricReporter reporter = loadMetricReporter(configRegistry); //initialize metric reporter try { reporter.init(); } catch (InvalidConfigurationException e) { consoleError.println("Invalid configuration error when initializing metrics reporter. " + e.getMessage()); } // Add Metrics Observer ObserveUtils.addObserver(new BallerinaMetricsObserver()); } }
@Override public void execute(Context context) { Metric[] metrics = DefaultMetricRegistry.getInstance().getAllMetrics(); PackageInfo observePackage = context.getProgramFile().getPackageInfo(OBSERVE_PACKAGE_PATH); StructureTypeInfo metricStructInfo = observePackage.getStructInfo(METRIC);
private <M extends Metric> M register(Metric registerMetric, Class<M> metricClass) { Metric metric = readMetric(registerMetric.getId(), metricClass); if (metric == null) { return writeMetricIfNotExists(registerMetric, metricClass); } else { return (M) metric; } }
/** * Registers the polled gauge metrics instance. * * @param gauge The {@link PolledGauge} instance. * @return A existing or a newly registered {@link PolledGauge} metric. */ public PolledGauge register(PolledGauge gauge) { return register(gauge, PolledGauge.class); }
@Override public PolledGauge build() { return DefaultMetricRegistry.getInstance().getMetricProvider(). newPolledGauge(new MetricId(name, description, tags), obj, valueFunction); } }
/** * Use {@link PolledGauge#builder(String, Object, ToDoubleFunction)}. * * @param id The {@link MetricId}. * @param obj State object used to compute a value. * @param valueFunction Function that produces an instantaneous gauge value from the state object. * @param <T> The type of the state object from which the gauge value is extracted. * @return A existing or a new {@link PolledGauge} metric. */ public <T> PolledGauge polledGauge(MetricId id, T obj, ToDoubleFunction<T> valueFunction) { return getOrCreate(id, PolledGauge.class, () -> metricProvider.newPolledGauge(id, obj, valueFunction)); }
Set<Tag> tagSet = new HashSet<>(); Tags.tags(tagSet, tagMap); Metric metric = DefaultMetricRegistry.getInstance().lookup(new MetricId(metricName, "", tagSet));
/** * Registers the gauge metrics instance. * * @param gauge The {@link Gauge} instance. * @return A existing or a newly registered {@link Gauge} metric. */ public Gauge register(Gauge gauge) { return register(gauge, Gauge.class); }
public Counter build() { return DefaultMetricRegistry.getInstance().getMetricProvider(). newCounter(new MetricId(name, description, tags)); }
/** * Use {@link Counter#builder(String)}. * * @param id The {@link MetricId}. * @return A existing or a new {@link Counter} metric. */ public Counter counter(MetricId id) { return getOrCreate(id, Counter.class, () -> metricProvider.newCounter(id)); }