Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to MicroProfile Metrics

Introduction to MicroProfile Metrics

HASUNUMA Kenji

May 26, 2018
Tweet

More Decks by HASUNUMA Kenji

Other Decks in Programming

Transcript

  1. Introduction to MicroProfile Metrics HASUNUMA Kenji GlassFish Users Group Japan

    k.hasunuma@coppermine.jp Twitter: @khasunuma #jjug_ccc #ccc_i1
  2. Why not JMX? • JXM/RMI is not easy to deal

    with • Integration with non-Java is too hard • Microservices may include 
 Java and non-Java environments -> REST based Metrics API #jjug_ccc #ccc_i1 @khasunuma
  3. MicroProfie Metrics • Included MicroProfile 1.2 or later • Built

    on JAX-RS and CDI • Expose system and user metrics • Using generic text format #jjug_ccc #ccc_i1 @khasunuma
  4. Key features • REST endpoint • Registry -- i.e. Scope


    Base, Vendor and Application • Format • Prometheus text format: text/plain • JSON format: application/json #jjug_ccc #ccc_i1 @khasunuma
  5. REST endpoint • /metrics by default
 e.g. http://localhost:8080/metrics • With

    Registry (see later) • /metrics/base • /metrics/vendor • /metrics/application #jjug_ccc #ccc_i1 @khasunuma
  6. Registry Store of metrics values: • Base -- JVM metrics,

    Required • Typically obtained from MBean • Vendor -- Vendor specific metrics • Application -- User metrics #jjug_ccc #ccc_i1 @khasunuma
  7. Format (Prometheus) # TYPE base:cpu_system_load_average gauge # HELP base:cpu_system_load_average Displays...

    base:cpu_system_load_average -1.0 # TYPE base:thread_count counter # HELP base:thread_count Displays... base:thread_count 90 # TYPE base:classloader_current_loaded_class_count counter # HELP base:classloader_current_loaded_class_count Displays... base:classloader_current_loaded_class_count 11150 # TYPE base:classloader_total_loaded_class_count counter # HELP base:classloader_total_loaded_class_count Displays... base:classloader_total_loaded_class_count 11193 # TYPE vendor:system_cpu_load gauge # HELP vendor:system_cpu_load Display... vendor:system_cpu_load 0.1491192437579042 #jjug_ccc #ccc_i1 @khasunuma
  8. Format (JSON) { "base": { "cpu.systemLoadAverage": -1, "thread.count": 90, "classloader.currentLoadedClass.count":

    11150, "classloader.totalLoadedClass.count": 11193, }, "vendor": {"system.cpu.load": 0.1491192437579042} } #jjug_ccc #ccc_i1 @khasunuma
  9. How to expose metrics 1. Denote metric(s) as field, method,

    constructor, parameter or class • The way depends on metric types 2. Fire an event to update metric(s) 3. Show metrics using REST endpoint #jjug_ccc #ccc_i1 @khasunuma
  10. To denote metrics @Inject @Metric Counter counter; Same as ...

    - Meter - Histogram - Timer @Gauge int gauge() { ... return value; } and more ... #jjug_ccc #ccc_i1 @khasunuma
  11. To update metrics • Counter#inc() or Counter#dec() • Meter#mark() •

    Histogram#update() • Timer#time() & Timer.Context#close() • Call annotated method:
 @Counted, @Metered, @Timed #jjug_ccc #ccc_i1 @khasunuma
  12. Example: Counter @Path("ping") @ApplicationScoped public class PingResource { // Expose

    metrics as counter // Metrics is exposed as entity name with package // (Metric name can be customized) @Inject @Metric private Counter counter; // Increment the counter each method call @GET public String ping() { counter.inc(); return "pong"; } } #jjug_ccc #ccc_i1 @khasunuma
  13. Example: Counter $ curl http://localhost:8080/app/rest/ping pong $ curl -H "Accept:

    application/json" http://localhost:8080/ metrics {"vendor":{"system.cpu.load":0.06088091680189212}, "base":{"classloader.totalLoadedClass.count": 15949,"cpu.systemLoadAverage":-1.0,"thread.count": 158,"classloader.currentLoadedClass.count": 15944,"jvm.uptime":80989,"memory.committedNonHeap": 127795200,"gc.PS MarkSweep.count":4,"memory.committedHeap": 415760384,"thread.max.count":160,"gc.PS Scavenge.count": 21,"cpu.availableProcessors":4,"thread.daemon.count": 144,"classloader.totalUnloadedClass.count": 5,"memory.usedNonHeap":113395160,"memory.maxHeap": 477626368,"memory.usedHeap":77223776,"gc.PS MarkSweep.time": 1394,"memory.maxNonHeap":-1,"gc.PS Scavenge.time":325} "application":{"com.example.app.PingResource.counter":1}} #jjug_ccc #ccc_i1 @khasunuma
  14. What's Metrics? • New feature of MicroProfile 1.2+ • API

    for expose metrics value • Using REST for polyglot environment • Built on JAX-RS and CDI • Simple, flexible and generic #jjug_ccc #ccc_i1 @khasunuma