[ 
https://issues.apache.org/jira/browse/BEAM-6905?focusedWorklogId=219095&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-219095
 ]

ASF GitHub Bot logged work on BEAM-6905:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 27/Mar/19 03:43
            Start Date: 27/Mar/19 03:43
    Worklog Time Spent: 10m 
      Work Description: kennknowles commented on pull request #8131: 
[BEAM-6905] Add support for user distribution metrics.
URL: https://github.com/apache/beam/pull/8131#discussion_r269396293
 
 

 ##########
 File path: 
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/fn/control/UserDistributionMonitoringInfoToCounterUpdateTransformer.java
 ##########
 @@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.runners.dataflow.worker.fn.control;
+
+import static org.apache.beam.model.pipeline.v1.MetricsApi.monitoringInfoSpec;
+
+import com.google.api.services.dataflow.model.CounterMetadata;
+import com.google.api.services.dataflow.model.CounterStructuredName;
+import com.google.api.services.dataflow.model.CounterStructuredNameAndMetadata;
+import com.google.api.services.dataflow.model.CounterUpdate;
+import com.google.api.services.dataflow.model.DistributionUpdate;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.beam.model.pipeline.v1.MetricsApi.IntDistributionData;
+import org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo;
+import org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfoSpecs.Enum;
+import org.apache.beam.runners.core.metrics.SpecMonitoringInfoValidator;
+import 
org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowStepContext;
+import 
org.apache.beam.runners.dataflow.worker.MetricsToCounterUpdateConverter.Origin;
+import 
org.apache.beam.runners.dataflow.worker.counters.DataflowCounterUpdateExtractor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class for transforming MonitoringInfo's containing User counter values, to 
relevant CounterUpdate
+ * proto.
+ */
+class UserDistributionMonitoringInfoToCounterUpdateTransformer
+    implements MonitoringInfoToCounterUpdateTransformer {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BeamFnMapTaskExecutor.class);
+
+  private final Map<String, DataflowStepContext> transformIdMapping;
+
+  private final SpecMonitoringInfoValidator specValidator;
+
+  public UserDistributionMonitoringInfoToCounterUpdateTransformer(
+      final SpecMonitoringInfoValidator specMonitoringInfoValidator,
+      final Map<String, DataflowStepContext> transformIdMapping) {
+    this.transformIdMapping = transformIdMapping;
+    this.specValidator = specMonitoringInfoValidator;
+  }
+
+  static final String BEAM_METRICS_USER_DISTRIBUTION_PREFIX =
+      Enum.USER_DISTRIBUTION_COUNTER
+          .getValueDescriptor()
+          .getOptions()
+          .getExtension(monitoringInfoSpec)
+          .getUrn();
+
+  private Optional<String> validate(MonitoringInfo monitoringInfo) {
+    Optional<String> validatorResult = specValidator.validate(monitoringInfo);
+    if (validatorResult.isPresent()) {
+      return validatorResult;
+    }
+
+    String urn = monitoringInfo.getUrn();
+    if (!urn.startsWith(BEAM_METRICS_USER_DISTRIBUTION_PREFIX)) {
+      throw new RuntimeException(
+          String.format(
+              "Received unexpected counter urn. Expected urn starting with: 
%s, received: %s",
+              BEAM_METRICS_USER_DISTRIBUTION_PREFIX, urn));
+    }
+
+    final String ptransform = monitoringInfo.getLabelsMap().get("PTRANSFORM");
+    DataflowStepContext stepContext = transformIdMapping.get(ptransform);
+    if (stepContext == null) {
+      return Optional.of(
+          "Encountered user-counter MonitoringInfo with unknown ptransformId: "
+              + monitoringInfo.toString());
+    }
+    return Optional.empty();
+  }
+
+  /**
+   * Transforms user counter MonitoringInfo to relevant CounterUpdate.
+   *
+   * @return Relevant CounterUpdate or null if transformation failed.
+   */
+  @Override
+  public CounterUpdate transform(MonitoringInfo monitoringInfo) {
+    Optional<String> validationResult = validate(monitoringInfo);
+    if (validationResult.isPresent()) {
+      LOG.info(validationResult.get());
+      return null;
+    }
+
+    IntDistributionData value =
+        
monitoringInfo.getMetric().getDistributionData().getIntDistributionData();
+    String urn = monitoringInfo.getUrn();
+
+    final String ptransform = monitoringInfo.getLabelsMap().get("PTRANSFORM");
+
+    CounterStructuredNameAndMetadata name = new 
CounterStructuredNameAndMetadata();
+
+    String nameWithNamespace =
+        
urn.substring(BEAM_METRICS_USER_DISTRIBUTION_PREFIX.length()).replace("^:", "");
+
+    final int lastColonIndex = nameWithNamespace.lastIndexOf(':');
+    String counterName = nameWithNamespace.substring(lastColonIndex + 1);
+    String counterNamespace =
+        lastColonIndex == -1 ? "" : nameWithNamespace.substring(0, 
lastColonIndex);
+
+    DataflowStepContext stepContext = transformIdMapping.get(ptransform);
+    name.setName(
+            new CounterStructuredName()
+                .setOrigin(Origin.USER.toString())
+                .setName(counterName)
+                
.setOriginalStepName(stepContext.getNameContext().originalName())
+                .setOriginNamespace(counterNamespace))
+        .setMetadata(new CounterMetadata().setKind("DISTRIBUTION"));
 
 Review comment:
   Just to clarify - Dataflow considers "distribution" to be a _kind_ of 
"counter"?
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 219095)
    Time Spent: 3h 20m  (was: 3h 10m)

> Add support for User Distribution metrics for Python Streaming
> --------------------------------------------------------------
>
>                 Key: BEAM-6905
>                 URL: https://issues.apache.org/jira/browse/BEAM-6905
>             Project: Beam
>          Issue Type: Bug
>          Components: runner-dataflow, sdk-py-harness
>            Reporter: Mikhail Gryzykhin
>            Assignee: Mikhail Gryzykhin
>            Priority: Major
>          Time Spent: 3h 20m
>  Remaining Estimate: 0h
>
> # Add new UserDistributionCounter urn to metrics.proto
>  # Report UserDistribution metric in python SDK
>  # Plumb User Distribution metric in Dataflow runner.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to