Skip to content

MINOR: Use thread name and task for sensor name#5111

Merged
guozhangwang merged 3 commits into
apache:trunkfrom
bbejeck:MINOR_add_thread_and_task_to_sensor_name
Jun 1, 2018
Merged

MINOR: Use thread name and task for sensor name#5111
guozhangwang merged 3 commits into
apache:trunkfrom
bbejeck:MINOR_add_thread_and_task_to_sensor_name

Conversation

@bbejeck

@bbejeck bbejeck commented May 31, 2018

Copy link
Copy Markdown
Member

Changes to keep the operation name as is and make the sensor name unique.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@bbejeck

bbejeck commented May 31, 2018

Copy link
Copy Markdown
Member Author

@mjsax mjsax added the streams label May 31, 2018
@mjsax

mjsax commented May 31, 2018

Copy link
Copy Markdown
Member

This is a follow up to #5009 that incorrectly changes not only sensor names, but also metric names that should not change.

@mjsax mjsax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vvcephei

Copy link
Copy Markdown
Contributor

What do you think about this instead (so we can avoid mutable taskName in this multithreaded class):

diff --git a/streams/src/main/java/org/apache/kafka/streams/processor/internals/metrics/StreamsMetricsImpl.java b/streams/src/main/java/org/apache/kafka/streams/processor/internals/metrics/StreamsMetricsImpl.java
index bc2e15012..6a3b22cc2 100644
--- a/streams/src/main/java/org/apache/kafka/streams/processor/internals/metrics/StreamsMetricsImpl.java
+++ b/streams/src/main/java/org/apache/kafka/streams/processor/internals/metrics/StreamsMetricsImpl.java
@@ -253,12 +253,30 @@ public class StreamsMetricsImpl implements StreamsMetrics {
                                                 final String operationName,
                                                 final Sensor.RecordingLevel recordingLevel,
                                                 final String... tags) {
-        return addLatencyAndThroughputSensor(
-            scopeName,
-            entityName,
-            threadName + "." + taskName + "." + operationName,
+
+        final Map<String, String> tagMap = constructTags(scopeName, entityName, tags);
+        final Map<String, String> allTagMap = constructTags(scopeName, "all", tags);
+
+        // first add the global operation metrics if not yet, with the global tags only
+        final Sensor parent = metrics.sensor(
+            sensorName(threadName + "." + taskName + "." + operationName, null),
+            recordingLevel
+        );
+        addLatencyMetrics(scopeName, parent, operationName, allTagMap);
+        addThroughputMetrics(scopeName, parent, operationName, allTagMap);
+
+        // add the operation metrics with additional tags
+        final Sensor sensor = metrics.sensor(
+            sensorName(threadName + "." + taskName + "." + operationName, entityName),
             recordingLevel,
-            tags);
+            parent
+        );
+        addLatencyMetrics(scopeName, sensor, operationName, tagMap);
+        addThroughputMetrics(scopeName, sensor, operationName, tagMap);
+
+        parentSensors.put(sensor, parent);
+
+        return sensor;
     }
 
     /**
@@ -292,13 +310,27 @@ public class StreamsMetricsImpl implements StreamsMetrics {
                                       final String operationName,
                                       final Sensor.RecordingLevel recordingLevel,
                                       final String... tags) {
-        return addThroughputSensor(
-            scopeName,
-            entityName,
-            threadName + "." + taskName + "." + operationName,
+        final Map<String, String> tagMap = constructTags(scopeName, entityName, tags);
+        final Map<String, String> allTagMap = constructTags(scopeName, "all", tags);
+
+        // first add the global operation metrics if not yet, with the global tags only
+        final Sensor parent = metrics.sensor(
+            sensorName(threadName + "." + taskName + "." + operationName, null),
+            recordingLevel
+        );
+        addThroughputMetrics(scopeName, parent, operationName, allTagMap);
+
+        // add the operation metrics with additional tags
+        final Sensor sensor = metrics.sensor(
+            sensorName(threadName + "." + taskName + "." + operationName, entityName),
             recordingLevel,
-            tags
+            parent
         );
+        addThroughputMetrics(scopeName, sensor, operationName, tagMap);
+
+        parentSensors.put(sensor, parent);
+
+        return sensor;
     }

assertNotNull(metrics.getSensor("name-mock.0_0." + throughputOperation));

for (final String opName : latencyOperations) {
StreamsTestUtils.getMetricByNameFilterByTags(metrics.metrics(), "mock.0_0." + opName + "-latency-avg", groupName, metricTags);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L116 and 118 also have changes that should be reverted.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I'm not sure about that. Those lines are getting the sensor by name which still uses the combination of thread.name() + taskID + operationName or am I missing something?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! yeah, you're right. Sorry!

@vvcephei vvcephei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch, @bbejeck.

I'm concerned about the unprotected mutable state this introduces. If multithreaded access to the methods in question occurs, then the task name may be overwritten by the second caller before the first caller creates the sensor name, resulting in incorrect execution.

Also, there is one place where we need to revert a couple more lines from #5009 to return the tests to their original state.

@bbejeck

bbejeck commented Jun 1, 2018

Copy link
Copy Markdown
Member Author

updated this.

@mjsax @vvcephei have another look and let me know your thoughts on this change.

final Sensor.RecordingLevel recordingLevel,
final String... tags) {

return addThroughputSensor(null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant addLatencyAndThroughputSensor

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh! yes, good catch

@vvcephei vvcephei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one typo, otherwise LGTM! Thanks again.

@guozhangwang guozhangwang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @bbejeck

@guozhangwang
guozhangwang merged commit cb2f024 into apache:trunk Jun 1, 2018
ying-zheng pushed a commit to ying-zheng/kafka that referenced this pull request Jul 6, 2018
Changes to keep the operation name as is and make the sensor name unique.

Reviewers: John Roesler <john@confluent.io>, Matthias J. Sax <matthias@confluent.io>, Guozhang Wang <wangguoz@gmail.com>
@bbejeck
bbejeck deleted the MINOR_add_thread_and_task_to_sensor_name branch July 10, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants