Repository: apache/kafka
File: streams/src/main/java/org/apache/kafka/streams/processor/internals/metrics/StreamsMetricsImpl.java (Java)

Code before:
    public final void removeAllThreadLevelSensors(final String threadId) {
        final String key = threadSensorPrefix(threadId);
        synchronized (threadLevelSensors) {
            final Deque<String> sensors = threadLevelSensors.remove(key);
            while (sensors != null && !sensors.isEmpty()) {
                metrics.removeSensor(sensors.pop());
            }
        }
    }

    public final void removeAllThreadLevelMetrics(final String threadId) {
        synchronized (threadLevelMetrics) {
            final Deque<MetricName> names = threadLevelMetrics.remove(threadSensorPrefix(threadId));
            while (names != null && !names.isEmpty()) {
                metrics.removeMetric(names.pop());
            }
        }
    }

    public void removeMetric(final MetricName metricName) {
        metrics.removeMetric(metricName);
    }

    public void removeStoreLevelMetric(final MetricName metricName) {
        metrics.removeMetric(metricName);
        storeLevelMetrics.get(
            storeSensorPrefix(
                metricName.tags().get("thread-id"),
                metricName.tags().get("task-id"),
                metricName.tags().get("scope-state-id")
            )
        ).remove(metricName);
    }

    public Map<String, String> taskLevelTagMap(final String threadId, final String taskId) {
        final Map<String, String> tagMap = new LinkedHashMap<>();
        tagMap.put(THREAD_ID_TAG, threadId);
        tagMap.put(TASK_ID_TAG, taskId);
        return tagMap;
    }

    public Map<String, String> nodeLevelTagMap(final String threadId,
                                               final String taskId,
                                               final String processorNodeName) {
        final Map<String, String> tagMap = new LinkedHashMap<>();
        tagMap.put(THREAD_ID_TAG, threadId);
        tagMap.put(TASK_ID_TAG, taskId);
        tagMap.put(PROCESSOR_NODE_ID_TAG, processorNodeName);
        return tagMap;
    }


Review comment (suggestion):
The original PR did add `removeMetric()` from above, which is not also used by other code, so we need to keep it, and add this new method instead.

Code after:
    }

    public final void removeAllThreadLevelSensors(final String threadId) {
        final String key = threadSensorPrefix(threadId);
        synchronized (threadLevelSensors) {
            final Deque<String> sensors = threadLevelSensors.remove(key);
            while (sensors != null && !sensors.isEmpty()) {
                metrics.removeSensor(sensors.pop());
            }
        }
    }

    public final void removeAllThreadLevelMetrics(final String threadId) {
        synchronized (threadLevelMetrics) {
            final Deque<MetricName> names = threadLevelMetrics.remove(threadSensorPrefix(threadId));
            while (names != null && !names.isEmpty()) {
                metrics.removeMetric(names.pop());
            }
        }
    }

    public void removeMetric(final MetricName metricName) {
        metrics.removeMetric(metricName);
    }

    public void removeStoreLevelMetric(final MetricName metricName) {
        metrics.removeMetric(metricName);

        final List<String> metricsScopeCandidates = metricName.tags().keySet().stream()
            .filter(tag -> !tag.equals(THREAD_ID_TAG) && !tag.equals(TASK_ID_TAG))
            .collect(Collectors.toList());
        if (metricsScopeCandidates.size() != 1) {
            // should never happen
            throw new IllegalStateException("Expected exactly one metric scope tag, but found " + metricsScopeCandidates);
        }

        final Deque<MetricName> metricsForStore = storeLevelMetrics.get(
            storeSensorPrefix(
                metricName.tags().get(THREAD_ID_TAG),
                metricName.tags().get(TASK_ID_TAG),
                metricName.tags().get(metricsScopeCandidates.get(0))
            )
        );

        if (metricsForStore != null) {
            metricsForStore.remove(metricName);
        }
    }

    public Map<String, String> taskLevelTagMap(final String threadId, final String taskId) {
        final Map<String, String> tagMap = new LinkedHashMap<>();