scwhittle commented on code in PR #37662:
URL: https://github.com/apache/beam/pull/37662#discussion_r2911661536


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/logging/DataflowWorkerLoggingHandler.java:
##########
@@ -131,28 +222,315 @@ public DataflowWorkerLoggingHandler(String filename, 
long sizeLimit) throws IOEx
     createOutputStream();
   }
 
-  public synchronized void setLogMdc(boolean enabled) {
-    this.logCustomMdc = enabled;
+  public void setLogMdc(boolean enabled) {
+    logCustomMdc = enabled;
   }
 
-  @Override
-  public synchronized void publish(LogRecord record) {
-    DataflowExecutionState currrentDataflowState = null;
-    ExecutionState currrentState = 
ExecutionStateTracker.getCurrentExecutionState();
-    if (currrentState instanceof DataflowExecutionState) {
-      currrentDataflowState = (DataflowExecutionState) currrentState;
-    }
-    // It's okay to pass in the null state, publish() handles and tests this.
-    publish(currrentDataflowState, record);
+  private void initializeLabelMaps(PipelineOptions options) {
+    DataflowPipelineOptions dataflowOptions = 
options.as(DataflowPipelineOptions.class);
+    DataflowWorkerHarnessOptions harnessOptions = 
options.as(DataflowWorkerHarnessOptions.class);
+    @Nullable String jobId = harnessOptions.getJobId();
+    if (jobId == null || jobId.isEmpty()) {
+      jobId = GceMetadataUtil.fetchDataflowJobId();
+    }
+    jobId = middleCrop(jobId, LABEL_MAX_LENGTH);
+
+    @Nullable String jobName = dataflowOptions.getJobName();
+    if (jobName == null || jobName.isEmpty()) {
+      jobName = GceMetadataUtil.fetchDataflowJobName();
+    }
+    jobName = middleCrop(jobName, LABEL_MAX_LENGTH);
+
+    String region = dataflowOptions.getRegion();
+    if (region.isEmpty()) {
+      region = GceMetadataUtil.fetchDataflowRegion();
+    }
+    region = middleCrop(region, LABEL_MAX_LENGTH);
+
+    // Note that the id in the options is a string name, not the numeric VM id.
+    @Nullable String workerName = harnessOptions.getWorkerId();
+    if (workerName == null || workerName.isEmpty()) {
+      workerName = GceMetadataUtil.fetchDataflowWorkerName();
+    }
+    workerName = middleCrop(workerName, LABEL_MAX_LENGTH);
+
+    String workerId = middleCrop(GceMetadataUtil.fetchDataflowWorkerId(), 
LABEL_MAX_LENGTH);
+
+    @Nullable String projectId = harnessOptions.getProject();
+    if (projectId == null || projectId.isEmpty()) {
+      projectId = checkNotNull(ServiceOptions.getDefaultProjectId());
+    }
+    projectId = middleCrop(projectId, LABEL_MAX_LENGTH);
+
+    ImmutableMap.Builder<String, String> defaultLabelsBuilder = new 
ImmutableMap.Builder<>();
+    defaultLabelsBuilder.put("compute.googleapis.com/resource_type", 
"instance");
+    defaultLabelsBuilder.put("compute.googleapis.com/resource_name", 
workerName);
+    defaultLabelsBuilder.put("compute.googleapis.com/resource_id", workerId);
+    defaultLabelsBuilder.put("dataflow.googleapis.com/region", region);
+    defaultLabelsBuilder.put("dataflow.googleapis.com/job_name", jobName);
+    defaultLabelsBuilder.put("dataflow.googleapis.com/job_id", jobId);
+    defaultLabels = defaultLabelsBuilder.buildOrThrow();
+
+    ImmutableMap.Builder<String, String> resourceLabelBuilder = new 
ImmutableMap.Builder<>();
+    resourceLabelBuilder.put("job_id", jobId);
+    resourceLabelBuilder.put("job_name", jobName);
+    resourceLabelBuilder.put("project_id", projectId);
+    resourceLabelBuilder.put("region", region);
+    // We add the step when constructing the resource as it can change.
+    defaultResourceLabels = resourceLabelBuilder.buildOrThrow();
+    steplessMonitoredResource =
+        MonitoredResource.newBuilder(RESOURCE_TYPE)
+            .setLabels(defaultResourceLabels)
+            .addLabel(STEP_RESOURCE_LABEL, "")
+            .build();
+  }
+
+  private static String middleCrop(String value, int maxSize) {

Review Comment:
   done



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to