gemini-code-assist[bot] commented on code in PR #38362: URL: https://github.com/apache/beam/pull/38362#discussion_r3181753642
########## sdks/java/io/datadog/src/main/java/org/apache/beam/sdk/io/datadog/DatadogWriteSchemaTransformProvider.java: ########## @@ -0,0 +1,279 @@ +/* + * 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.sdk.io.datadog; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull; + +import com.google.auto.service.AutoService; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; + +@AutoService(SchemaTransformProvider.class) +public class DatadogWriteSchemaTransformProvider + extends TypedSchemaTransformProvider<DatadogWriteSchemaTransformConfiguration> { + private static final String IDENTIFIER = "beam:schematransform:org.apache.beam:datadog_write:v1"; + static final String INPUT = "input"; + static final String OUTPUT = "output"; + static final String ERROR = "errors"; + public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {}; + public static final TupleTag<Void> OUTPUT_TAG = new TupleTag<Void>() {}; + public static final TupleTag<DatadogEvent> EVENT_TAG = new TupleTag<DatadogEvent>() {}; + + @Override + protected Class<DatadogWriteSchemaTransformConfiguration> configurationClass() { + return DatadogWriteSchemaTransformConfiguration.class; + } + + /** Returns the expected {@link SchemaTransform} of the configuration. */ + @Override + protected SchemaTransform from(DatadogWriteSchemaTransformConfiguration configuration) { + return new DatadogWriteSchemaTransform(configuration); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} identifier method. */ + @Override + public String identifier() { + return IDENTIFIER; + } + + /** Implementation of the {@link TypedSchemaTransformProvider} input collection names method. */ + @Override + public List<String> inputCollectionNames() { + return Collections.singletonList(INPUT); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} output collection names method. */ + @Override + public List<String> outputCollectionNames() { + return Collections.singletonList(ERROR); + } + + /** + * An implementation of {@link SchemaTransform} for Datadog Write jobs configured using {@link + * DatadogWriteSchemaTransformConfiguration}. + */ + static class DatadogWriteSchemaTransform extends SchemaTransform { + private final DatadogWriteSchemaTransformConfiguration configuration; + + DatadogWriteSchemaTransform(DatadogWriteSchemaTransformConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public PCollectionRowTuple expand(PCollectionRowTuple input) { + // Validate configuration parameters + configuration.validate(); + + // Obtain input rows + PCollection<Row> inputRows = input.get(INPUT); + + // Check for errors + boolean handleErrors = ErrorHandling.hasOutput(configuration.getErrorHandling()); + + Schema inputSchema = inputRows.getSchema(); + Schema dynamicErrorSchema = + Schema.builder() + .addNullableRowField("failed_row", inputSchema) + .addNullableField("payload", Schema.FieldType.STRING) + .addNullableField("statusCode", Schema.FieldType.INT32) + .addNullableField("statusMessage", Schema.FieldType.STRING) + .build(); + + PCollectionTuple convertResult = + inputRows.apply( + "Convert to DatadogEvent", + ParDo.of(new RowToEventFn(handleErrors, ERROR_TAG, dynamicErrorSchema)) + .withOutputTags(EVENT_TAG, TupleTagList.of(ERROR_TAG))); + + PCollection<DatadogEvent> datadogEvents = + convertResult.get(EVENT_TAG).setCoder(DatadogEventCoder.of()); + PCollection<Row> conversionErrors = + convertResult + .get(ERROR_TAG) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + // Configure DatadogIO.Write + DatadogIO.Write.Builder builder = + DatadogIO.writeBuilder(configuration.getMinBatchCount()) + .withUrl(configuration.getUrl()) + .withApiKey(configuration.getApiKey()); + + Integer batchCount = configuration.getBatchCount(); + if (batchCount != null) { + builder = builder.withBatchCount(batchCount); + } + Long maxBufferSize = configuration.getMaxBufferSize(); + if (maxBufferSize != null) { + builder = builder.withMaxBufferSize(maxBufferSize); + } + Integer parallelism = configuration.getParallelism(); + if (parallelism != null) { + builder = builder.withParallelism(parallelism); + } + + DatadogIO.Write write = builder.build(); + + // Apply DatadogIO.Write + PCollection<DatadogWriteError> writeErrors = datadogEvents.apply("Write To Datadog", write); Review Comment:  If `errorHandling` is not specified (null), the `writeErrors` PCollection is ignored. Since `DatadogIO.Write` returns errors in a PCollection rather than throwing exceptions, write failures will be silent. You should add a transform to fail the pipeline if any errors occur and no error output is configured. ```java // Apply DatadogIO.Write PCollection<DatadogWriteError> writeErrors = datadogEvents.apply("Write To Datadog", write); // Handle errors ErrorHandling errorHandling = configuration.getErrorHandling(); if (errorHandling == null) { writeErrors.apply("Fail on Write Error", org.apache.beam.sdk.transforms.ParDo.of(new org.apache.beam.sdk.transforms.DoFn<DatadogWriteError, Void>() { @ProcessElement public void processElement(@Element DatadogWriteError error) { throw new RuntimeException("Datadog write failed: " + error.statusMessage()); } })); } ``` ########## sdks/java/io/datadog/src/main/java/org/apache/beam/sdk/io/datadog/DatadogWriteSchemaTransformProvider.java: ########## @@ -0,0 +1,279 @@ +/* + * 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.sdk.io.datadog; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull; + +import com.google.auto.service.AutoService; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; + +@AutoService(SchemaTransformProvider.class) +public class DatadogWriteSchemaTransformProvider + extends TypedSchemaTransformProvider<DatadogWriteSchemaTransformConfiguration> { + private static final String IDENTIFIER = "beam:schematransform:org.apache.beam:datadog_write:v1"; + static final String INPUT = "input"; + static final String OUTPUT = "output"; + static final String ERROR = "errors"; + public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {}; + public static final TupleTag<Void> OUTPUT_TAG = new TupleTag<Void>() {}; + public static final TupleTag<DatadogEvent> EVENT_TAG = new TupleTag<DatadogEvent>() {}; + + @Override + protected Class<DatadogWriteSchemaTransformConfiguration> configurationClass() { + return DatadogWriteSchemaTransformConfiguration.class; + } + + /** Returns the expected {@link SchemaTransform} of the configuration. */ + @Override + protected SchemaTransform from(DatadogWriteSchemaTransformConfiguration configuration) { + return new DatadogWriteSchemaTransform(configuration); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} identifier method. */ + @Override + public String identifier() { + return IDENTIFIER; + } + + /** Implementation of the {@link TypedSchemaTransformProvider} input collection names method. */ + @Override + public List<String> inputCollectionNames() { + return Collections.singletonList(INPUT); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} output collection names method. */ + @Override + public List<String> outputCollectionNames() { + return Collections.singletonList(ERROR); + } + + /** + * An implementation of {@link SchemaTransform} for Datadog Write jobs configured using {@link + * DatadogWriteSchemaTransformConfiguration}. + */ + static class DatadogWriteSchemaTransform extends SchemaTransform { + private final DatadogWriteSchemaTransformConfiguration configuration; + + DatadogWriteSchemaTransform(DatadogWriteSchemaTransformConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public PCollectionRowTuple expand(PCollectionRowTuple input) { + // Validate configuration parameters + configuration.validate(); + + // Obtain input rows + PCollection<Row> inputRows = input.get(INPUT); + + // Check for errors + boolean handleErrors = ErrorHandling.hasOutput(configuration.getErrorHandling()); + + Schema inputSchema = inputRows.getSchema(); + Schema dynamicErrorSchema = + Schema.builder() + .addNullableRowField("failed_row", inputSchema) + .addNullableField("payload", Schema.FieldType.STRING) + .addNullableField("statusCode", Schema.FieldType.INT32) + .addNullableField("statusMessage", Schema.FieldType.STRING) + .build(); + + PCollectionTuple convertResult = + inputRows.apply( + "Convert to DatadogEvent", + ParDo.of(new RowToEventFn(handleErrors, ERROR_TAG, dynamicErrorSchema)) + .withOutputTags(EVENT_TAG, TupleTagList.of(ERROR_TAG))); + + PCollection<DatadogEvent> datadogEvents = + convertResult.get(EVENT_TAG).setCoder(DatadogEventCoder.of()); + PCollection<Row> conversionErrors = + convertResult + .get(ERROR_TAG) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + // Configure DatadogIO.Write + DatadogIO.Write.Builder builder = + DatadogIO.writeBuilder(configuration.getMinBatchCount()) + .withUrl(configuration.getUrl()) + .withApiKey(configuration.getApiKey()); + + Integer batchCount = configuration.getBatchCount(); + if (batchCount != null) { + builder = builder.withBatchCount(batchCount); + } + Long maxBufferSize = configuration.getMaxBufferSize(); + if (maxBufferSize != null) { + builder = builder.withMaxBufferSize(maxBufferSize); + } + Integer parallelism = configuration.getParallelism(); + if (parallelism != null) { + builder = builder.withParallelism(parallelism); + } + + DatadogIO.Write write = builder.build(); + + // Apply DatadogIO.Write + PCollection<DatadogWriteError> writeErrors = datadogEvents.apply("Write To Datadog", write); + + // Handle errors + ErrorHandling errorHandling = configuration.getErrorHandling(); + if (errorHandling != null) { + PCollection<Row> writeErrorRows = + writeErrors + .apply( + "Convert Write Errors to Rows", + org.apache.beam.sdk.transforms.MapElements.into( + org.apache.beam.sdk.values.TypeDescriptors.rows()) + .via( + error -> + Row.withSchema(dynamicErrorSchema) + .addValue(null) + .addValue(error.payload()) + .addValue(error.statusCode()) + .addValue(error.statusMessage()) + .build())) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + PCollection<Row> allErrors = + org.apache.beam.sdk.values.PCollectionList.of(conversionErrors) + .and(writeErrorRows) + .apply("Flatten Errors", org.apache.beam.sdk.transforms.Flatten.pCollections()) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + return PCollectionRowTuple.of(errorHandling.getOutput(), allErrors); Review Comment:  The output key used in `PCollectionRowTuple.of` should match the name declared in `outputCollectionNames()`, which is `ERROR` ("errors"). While `errorHandling.getOutput()` provides the user-defined name for the YAML context, the `SchemaTransform` itself must use its internal identifier for the output port. The YAML layer handles the mapping to the user-defined name. ```suggestion return PCollectionRowTuple.of(ERROR, allErrors); ``` ########## sdks/java/io/datadog/src/main/java/org/apache/beam/sdk/io/datadog/DatadogWriteSchemaTransformProvider.java: ########## @@ -0,0 +1,279 @@ +/* + * 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.sdk.io.datadog; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull; + +import com.google.auto.service.AutoService; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; + +@AutoService(SchemaTransformProvider.class) +public class DatadogWriteSchemaTransformProvider + extends TypedSchemaTransformProvider<DatadogWriteSchemaTransformConfiguration> { + private static final String IDENTIFIER = "beam:schematransform:org.apache.beam:datadog_write:v1"; + static final String INPUT = "input"; + static final String OUTPUT = "output"; + static final String ERROR = "errors"; + public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {}; + public static final TupleTag<Void> OUTPUT_TAG = new TupleTag<Void>() {}; + public static final TupleTag<DatadogEvent> EVENT_TAG = new TupleTag<DatadogEvent>() {}; + + @Override + protected Class<DatadogWriteSchemaTransformConfiguration> configurationClass() { + return DatadogWriteSchemaTransformConfiguration.class; + } + + /** Returns the expected {@link SchemaTransform} of the configuration. */ + @Override + protected SchemaTransform from(DatadogWriteSchemaTransformConfiguration configuration) { + return new DatadogWriteSchemaTransform(configuration); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} identifier method. */ + @Override + public String identifier() { + return IDENTIFIER; + } + + /** Implementation of the {@link TypedSchemaTransformProvider} input collection names method. */ + @Override + public List<String> inputCollectionNames() { + return Collections.singletonList(INPUT); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} output collection names method. */ + @Override + public List<String> outputCollectionNames() { + return Collections.singletonList(ERROR); + } + + /** + * An implementation of {@link SchemaTransform} for Datadog Write jobs configured using {@link + * DatadogWriteSchemaTransformConfiguration}. + */ + static class DatadogWriteSchemaTransform extends SchemaTransform { + private final DatadogWriteSchemaTransformConfiguration configuration; + + DatadogWriteSchemaTransform(DatadogWriteSchemaTransformConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public PCollectionRowTuple expand(PCollectionRowTuple input) { + // Validate configuration parameters + configuration.validate(); + + // Obtain input rows + PCollection<Row> inputRows = input.get(INPUT); + + // Check for errors + boolean handleErrors = ErrorHandling.hasOutput(configuration.getErrorHandling()); + + Schema inputSchema = inputRows.getSchema(); + Schema dynamicErrorSchema = + Schema.builder() + .addNullableRowField("failed_row", inputSchema) + .addNullableField("payload", Schema.FieldType.STRING) + .addNullableField("statusCode", Schema.FieldType.INT32) + .addNullableField("statusMessage", Schema.FieldType.STRING) + .build(); + + PCollectionTuple convertResult = + inputRows.apply( + "Convert to DatadogEvent", + ParDo.of(new RowToEventFn(handleErrors, ERROR_TAG, dynamicErrorSchema)) + .withOutputTags(EVENT_TAG, TupleTagList.of(ERROR_TAG))); + + PCollection<DatadogEvent> datadogEvents = + convertResult.get(EVENT_TAG).setCoder(DatadogEventCoder.of()); + PCollection<Row> conversionErrors = + convertResult + .get(ERROR_TAG) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + // Configure DatadogIO.Write + DatadogIO.Write.Builder builder = + DatadogIO.writeBuilder(configuration.getMinBatchCount()) + .withUrl(configuration.getUrl()) + .withApiKey(configuration.getApiKey()); + + Integer batchCount = configuration.getBatchCount(); + if (batchCount != null) { + builder = builder.withBatchCount(batchCount); + } + Long maxBufferSize = configuration.getMaxBufferSize(); + if (maxBufferSize != null) { + builder = builder.withMaxBufferSize(maxBufferSize); + } + Integer parallelism = configuration.getParallelism(); + if (parallelism != null) { + builder = builder.withParallelism(parallelism); + } + + DatadogIO.Write write = builder.build(); + + // Apply DatadogIO.Write + PCollection<DatadogWriteError> writeErrors = datadogEvents.apply("Write To Datadog", write); + + // Handle errors + ErrorHandling errorHandling = configuration.getErrorHandling(); + if (errorHandling != null) { + PCollection<Row> writeErrorRows = + writeErrors + .apply( + "Convert Write Errors to Rows", + org.apache.beam.sdk.transforms.MapElements.into( + org.apache.beam.sdk.values.TypeDescriptors.rows()) + .via( + error -> + Row.withSchema(dynamicErrorSchema) + .addValue(null) + .addValue(error.payload()) + .addValue(error.statusCode()) + .addValue(error.statusMessage()) + .build())) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + PCollection<Row> allErrors = + org.apache.beam.sdk.values.PCollectionList.of(conversionErrors) + .and(writeErrorRows) + .apply("Flatten Errors", org.apache.beam.sdk.transforms.Flatten.pCollections()) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + return PCollectionRowTuple.of(errorHandling.getOutput(), allErrors); + } else { + return PCollectionRowTuple.empty(input.getPipeline()); + } + } + } + + static final Schema WRITE_ERROR_SCHEMA = + Schema.builder() + .addNullableField("payload", Schema.FieldType.STRING) + .addNullableField("statusCode", Schema.FieldType.INT32) + .addNullableField("statusMessage", Schema.FieldType.STRING) + .build(); + + static final Schema DATADOG_EVENT_SCHEMA = + Schema.builder() + .addNullableField("ddsource", Schema.FieldType.STRING) + .addNullableField("ddtags", Schema.FieldType.STRING) + .addNullableField("hostname", Schema.FieldType.STRING) + .addNullableField("service", Schema.FieldType.STRING) + .addNullableField("message", Schema.FieldType.STRING) + .build(); + + static Row eventToRow(DatadogEvent event) { + return Row.withSchema(DATADOG_EVENT_SCHEMA) + .addValue(event.ddsource()) + .addValue(event.ddtags()) + .addValue(event.hostname()) + .addValue(event.service()) + .addValue(event.message()) + .build(); + } + + static DatadogEvent rowToEvent(Row row) { + DatadogEvent.Builder builder = DatadogEvent.newBuilder(); + Schema schema = row.getSchema(); + + String ddsource = schema.hasField("ddsource") ? row.getString("ddsource") : null; + if (ddsource != null) { + builder.withSource(ddsource); + } + String ddtags = schema.hasField("ddtags") ? row.getString("ddtags") : null; + if (ddtags != null) { + builder.withTags(ddtags); + } + String hostname = schema.hasField("hostname") ? row.getString("hostname") : null; + if (hostname != null) { + builder.withHostname(hostname); + } + String service = schema.hasField("service") ? row.getString("service") : null; + if (service != null) { + builder.withService(service); + } + String message = schema.hasField("message") ? row.getString("message") : null; + builder.withMessage(checkNotNull(message, "Message is required.")); + + return builder.build(); + } + + static class RowToEventFn extends DoFn<Row, DatadogEvent> { + private final boolean handleErrors; + private final TupleTag<Row> errorOutputTag; + private final Schema errorSchema; + + RowToEventFn(boolean handleErrors, TupleTag<Row> errorOutputTag, Schema errorSchema) { + this.handleErrors = handleErrors; + this.errorOutputTag = errorOutputTag; + this.errorSchema = errorSchema; + } + + @ProcessElement + public void processElement(ProcessContext c) { + try { + c.output(rowToEvent(c.element())); + } catch (Exception e) { + if (handleErrors) { + c.output( + errorOutputTag, + Row.withSchema(errorSchema) + .addValue(c.element()) + .addValue(c.element().toString()) Review Comment:  Using `c.element().toString()` as the error payload might produce very large strings for complex rows, which could exceed Datadog's limits or be difficult to parse. Consider using a more structured format like JSON if available, or only including key identifying fields. ########## sdks/python/apache_beam/yaml/integration_tests.py: ########## @@ -20,19 +20,46 @@ import contextlib import copy import glob +import gzip +import http.server +import io import itertools +import json import logging import os import random import secrets import sqlite3 import string +import struct +import threading import unittest import uuid from datetime import datetime from datetime import timezone import mock + +from apache_beam.coders import Coder +from apache_beam.coders.coder_impl import CoderImpl + + +class BigEndianIntegerCoderImpl(CoderImpl): + def encode_to_stream(self, value, stream, nested): + stream.write(struct.pack('>i', value)) + + def decode_from_stream(self, stream, nested): + return struct.unpack('>i', stream.read(4))[0] + + +class BigEndianIntegerCoder(Coder): + def get_impl(self): + return BigEndianIntegerCoderImpl() + + +Coder.register_urn( + 'beam:coders:javasdk:0.1', + None, lambda payload, components, context: BigEndianIntegerCoder()) Review Comment:  The `BigEndianIntegerCoder` and its registration seem unrelated to the Datadog IO implementation. If this is not required for the Datadog integration tests, it should be removed from this pull request to maintain a clean scope. ########## sdks/java/io/datadog/src/main/java/org/apache/beam/sdk/io/datadog/DatadogWriteSchemaTransformProvider.java: ########## @@ -0,0 +1,279 @@ +/* + * 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.sdk.io.datadog; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull; + +import com.google.auto.service.AutoService; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; + +@AutoService(SchemaTransformProvider.class) +public class DatadogWriteSchemaTransformProvider + extends TypedSchemaTransformProvider<DatadogWriteSchemaTransformConfiguration> { + private static final String IDENTIFIER = "beam:schematransform:org.apache.beam:datadog_write:v1"; + static final String INPUT = "input"; + static final String OUTPUT = "output"; + static final String ERROR = "errors"; + public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {}; + public static final TupleTag<Void> OUTPUT_TAG = new TupleTag<Void>() {}; + public static final TupleTag<DatadogEvent> EVENT_TAG = new TupleTag<DatadogEvent>() {}; + + @Override + protected Class<DatadogWriteSchemaTransformConfiguration> configurationClass() { + return DatadogWriteSchemaTransformConfiguration.class; + } + + /** Returns the expected {@link SchemaTransform} of the configuration. */ + @Override + protected SchemaTransform from(DatadogWriteSchemaTransformConfiguration configuration) { + return new DatadogWriteSchemaTransform(configuration); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} identifier method. */ + @Override + public String identifier() { + return IDENTIFIER; + } + + /** Implementation of the {@link TypedSchemaTransformProvider} input collection names method. */ + @Override + public List<String> inputCollectionNames() { + return Collections.singletonList(INPUT); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} output collection names method. */ + @Override + public List<String> outputCollectionNames() { + return Collections.singletonList(ERROR); + } + + /** + * An implementation of {@link SchemaTransform} for Datadog Write jobs configured using {@link + * DatadogWriteSchemaTransformConfiguration}. + */ + static class DatadogWriteSchemaTransform extends SchemaTransform { + private final DatadogWriteSchemaTransformConfiguration configuration; + + DatadogWriteSchemaTransform(DatadogWriteSchemaTransformConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public PCollectionRowTuple expand(PCollectionRowTuple input) { + // Validate configuration parameters + configuration.validate(); + + // Obtain input rows + PCollection<Row> inputRows = input.get(INPUT); + + // Check for errors + boolean handleErrors = ErrorHandling.hasOutput(configuration.getErrorHandling()); + + Schema inputSchema = inputRows.getSchema(); + Schema dynamicErrorSchema = + Schema.builder() + .addNullableRowField("failed_row", inputSchema) + .addNullableField("payload", Schema.FieldType.STRING) + .addNullableField("statusCode", Schema.FieldType.INT32) + .addNullableField("statusMessage", Schema.FieldType.STRING) + .build(); + + PCollectionTuple convertResult = + inputRows.apply( + "Convert to DatadogEvent", + ParDo.of(new RowToEventFn(handleErrors, ERROR_TAG, dynamicErrorSchema)) + .withOutputTags(EVENT_TAG, TupleTagList.of(ERROR_TAG))); + + PCollection<DatadogEvent> datadogEvents = + convertResult.get(EVENT_TAG).setCoder(DatadogEventCoder.of()); + PCollection<Row> conversionErrors = + convertResult + .get(ERROR_TAG) + .setCoder(org.apache.beam.sdk.coders.RowCoder.of(dynamicErrorSchema)); + + // Configure DatadogIO.Write + DatadogIO.Write.Builder builder = + DatadogIO.writeBuilder(configuration.getMinBatchCount()) Review Comment:  The `minBatchCount` is passed directly to `writeBuilder`, but it is marked as `@Nullable` in the configuration. If `DatadogIO.writeBuilder` expects a primitive `int` or a non-null `Integer`, this will cause a `NullPointerException`. It is safer to use a default value or set it via a `withMinBatchCount` method if available, similar to how other parameters are handled. ```java DatadogIO.Write.Builder builder = DatadogIO.writeBuilder() .withUrl(configuration.getUrl()) .withApiKey(configuration.getApiKey()); Integer minBatchCount = configuration.getMinBatchCount(); if (minBatchCount != null) { builder = builder.withMinBatchCount(minBatchCount); } ``` ########## sdks/java/io/datadog/src/main/java/org/apache/beam/sdk/io/datadog/DatadogWriteSchemaTransformConfiguration.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.sdk.io.datadog; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument; + +import com.google.auto.value.AutoValue; +import javax.annotation.Nullable; +import org.apache.beam.sdk.schemas.AutoValueSchema; +import org.apache.beam.sdk.schemas.annotations.DefaultSchema; +import org.apache.beam.sdk.schemas.annotations.SchemaFieldDescription; +import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Strings; + +/** + * Configuration for writing to Datadog. + * + * <p>This class is meant to be used with {@link DatadogWriteSchemaTransformProvider}. + */ +@DefaultSchema(AutoValueSchema.class) +@AutoValue +public abstract class DatadogWriteSchemaTransformConfiguration { + + public void validate() { + String invalidConfigMessage = "Invalid Datadog Write configuration: "; + checkArgument( + !Strings.isNullOrEmpty(getUrl()), invalidConfigMessage + "url must be specified."); + checkArgument( + !Strings.isNullOrEmpty(getApiKey()), invalidConfigMessage + "apiKey must be specified."); Review Comment:  The null checks for `url` and `apiKey` are redundant because these fields are not marked as `@Nullable`, and `AutoValue` will automatically throw an `IllegalStateException` during `build()` if they are null. However, checking for empty strings is still valid. Consider simplifying this or ensuring consistency with the expected exception type in tests. -- 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]
