Chat Support
Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For Java / Metrics / Designing Sampled Metrics / Developer's Guide - Java - Sampled Metrics Example
In This Topic
    Developer's Guide - Java - Sampled Metrics Example
    In This Topic

    Loupe uses Sampled Metrics to capture data points at periodic intervals.  In this example, the size of a cache is being tracked.

    Defining a Sampled Metric

    @SampledMetricClass(namespace = "Samples", categoryName = "Database.Engine")
    public class CacheSampledMetric
    {
        private String instanceName;
        private int pages;
        private int size;
        public CacheSampledMetric(String instanceName, int pages, int size) {
            super();
            this.instanceName = instanceName;
            this.pages = pages;
            this.size = size;
        }
       
        // An optional member to be automatically queried for the instance name
        // to use for this sampled metric on this data object instance.
        @SampledMetricInstanceName
        public final String getInstanceName() {
            return instanceName;
        }
       
        // A sampled metric value representing the number of pages at the point in time of
        // execution.
        @SampledMetricValue(counterName = "pages", samplingType = SamplingType.RAW_COUNT,
            caption = "Pages in Cache", description = "Total number of pages in cache")
        public final int getPages() {
            return pages;
        }
       
        // A sampled metric value representing the average size of the cache over time.
        @SampledMetricValue(counterName = "size", samplingType = SamplingType.RAW_COUNT,
            unitCaption = "Bytes", caption = "Cache Size",
            description = "Total number of bytes used by pages in cache")
        public final int getSize() {
            return size;
        }
    }
    

    Recording a Metric Sample

    Using the class defined above, metric samples can be recorded at any interval that is suitable for your application by passing an instance of the object to register at least once (to record the definition) and then to write each time you want to record a sample.

    // register our metric type
    SampledMetric.register(CacheSampledMetric.class);
    // write a metric with the ascribed values
    SampledMetric.write(new CacheSampledMetric("cacheMetric", 122, 1024));
    
    See Also