private void initializeAmbryCacheStats() {

    HashMap<String, Gauge> metricMap = new HashMap<>();
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "Enabled"), (Gauge<Integer>) () -> cacheEnabled ? 1 : 0);
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "MaxNumCacheEntries"), (Gauge<Long>) () -> maxNumCacheEntries);
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "HitRate"), (Gauge<Double>) () -> ambryCache.stats().hitRate());
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "MissRate"), (Gauge<Double>) () -> ambryCache.stats().missRate());
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "NumCacheEntries"), (Gauge<Long>) ambryCache::estimatedSize);
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "EvictionCount"), (Gauge<Long>) () -> ambryCache.stats().evictionCount());
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "HitCount"), (Gauge<Long>) () -> ambryCache.stats().hitCount());
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "MissCount"), (Gauge<Long>) () -> ambryCache.stats().missCount());
    metricMap.put(MetricRegistry.name(AmbryCache.class, cacheId + "RequestCount"), (Gauge<Long>) () -> ambryCache.stats().requestCount());
    for (Map.Entry<String, Gauge> entry : metricMap.entrySet()) {
      if (metricRegistry.getMetrics().containsKey(entry.getKey()) == false) {
        try {
          metricRegistry.register(entry.getKey(), entry.getValue());
        } catch (IllegalArgumentException e) {
          // This means someone else registered the metric with the same name before we could.
          // No problem. We'll just use that metric.
          logger.warn(e.toString());
        }
      }
    }

    getErrorCount = metricRegistry.counter(MetricRegistry.name(AmbryCache.class, cacheId + "GetErrorCount"));
    putErrorCount = metricRegistry.counter(MetricRegistry.name(AmbryCache.class, cacheId + "PutErrorCount"));
    deleteErrorCount = metricRegistry.counter(MetricRegistry.name(AmbryCache.class, cacheId + "DeleteErrorCount"));
    getLatencyMs = metricRegistry.histogram(MetricRegistry.name(AmbryCache.class, cacheId + "GetLatencyMs"));
    putLatencyMs = metricRegistry.histogram(MetricRegistry.name(AmbryCache.class, cacheId + "PutLatencyMs"));
    deleteLatencyMs = metricRegistry.histogram(MetricRegistry.name(AmbryCache.class, cacheId + "DeleteLatencyMs"));
    getRequestRate = metricRegistry.meter(MetricRegistry.name(AmbryCache.class, cacheId + "GetRequestRate"));
    putRequestRate = metricRegistry.meter(MetricRegistry.name(AmbryCache.class, cacheId + "PutRequestRate"));
    deleteRequestRate = metricRegistry.meter(MetricRegistry.name(AmbryCache.class, cacheId + "DeleteRequestRate"));
    logger.info("[{}] Initialized metrics for cache", cacheId);
  }
Initialize metrics for this instance of AmbryCache
ambry-commons/src/main/java/com/github/ambry/commons/AmbryCache.java