Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-13846

Add an overloaded metricOrElseCreate function in Metrics

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 3.3.0
    • metrics

    Description

      The `Metrics` registry is often used by concurrent threads, however it's get/create APIs are not well suited for it. A common pattern from the user today is:

      metric = metrics.metric(metricName);
      
      if (metric == null) {
        try {
          metrics.createMetric(..)
        } catch (IllegalArgumentException e){
          // another thread may create the metric at the mean time
        }
      } 
      

      Otherwise the caller would need to synchronize the whole block trying to get the metric. However, the `createMetric` function call itself indeed synchronize internally on updating the metric map.

      So we could consider adding a metricOrElseCreate function which is similar to createMetric, but instead of throwing an illegal argument exception within the internal synchronization block, it would just return the already existing metric.

      Attachments

        Activity

          People

            sagarrao Sagar Rao
            guozhang Guozhang Wang
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: