darenwkt commented on code in PR #22: URL: https://github.com/apache/flink-connector-prometheus/pull/22#discussion_r2055384980
########## flink-connector-prometheus/src/main/java/org/apache/flink/connector/prometheus/table/PrometheusDynamicSink.java: ########## @@ -0,0 +1,193 @@ +/* + * 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.flink.connector.prometheus.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.base.table.sink.AsyncDynamicTableSink; +import org.apache.flink.connector.base.table.sink.AsyncDynamicTableSinkBuilder; +import org.apache.flink.connector.prometheus.sink.PrometheusRequestSigner; +import org.apache.flink.connector.prometheus.sink.PrometheusSink; +import org.apache.flink.connector.prometheus.sink.PrometheusSinkBuilder; +import org.apache.flink.connector.prometheus.sink.PrometheusSinkConfiguration; +import org.apache.flink.connector.prometheus.sink.prometheus.Types; +import org.apache.flink.table.connector.ChangelogMode; +import org.apache.flink.table.connector.sink.DynamicTableSink; +import org.apache.flink.table.connector.sink.SinkV2Provider; +import org.apache.flink.table.connector.sink.abilities.SupportsPartitioning; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.types.DataType; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.Iterator; +import java.util.Map; +import java.util.Optional; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +public class PrometheusDynamicSink extends AsyncDynamicTableSink<Types.TimeSeries> + implements SupportsPartitioning { + private static final Logger LOG = LoggerFactory.getLogger(PrometheusDynamicSink.class); + + /** Consumed data type of the table. */ + private final DataType physicalDataType; + + private final PrometheusConfig prometheusConfig; + + protected PrometheusDynamicSink( + @Nullable Integer maxBatchSize, + @Nullable Integer maxInFlightRequests, + @Nullable Integer maxBufferedRequests, + @Nullable Long maxBufferSizeInBytes, + @Nullable Long maxTimeInBufferMS, + @Nullable DataType physicalDataType, + PrometheusConfig prometheusConfig) { + + super( + maxBatchSize, // maxBatchSizeInSamples + maxInFlightRequests, + maxBufferedRequests, + maxBufferSizeInBytes, // maxRecordSizeInSamples + maxTimeInBufferMS); + + this.physicalDataType = + Preconditions.checkNotNull(physicalDataType, "Consumed data type must not be null"); + this.prometheusConfig = prometheusConfig; + } + + @Override + public ChangelogMode getChangelogMode(ChangelogMode changelogMode) { + return ChangelogMode.upsert(); + } + + @Override + public SinkRuntimeProvider getSinkRuntimeProvider(Context context) { + + PrometheusSinkBuilder<RowData> builder = + new PrometheusSinkBuilder<RowData>() + .setRetryConfiguration( + PrometheusSinkConfiguration.RetryConfiguration + .DEFAULT_RETRY_CONFIGURATION) + .setElementConverter( + new RowDataElementConverter(physicalDataType, prometheusConfig)) + .setPrometheusRemoteWriteUrl(prometheusConfig.getRemoteWriteEndpointUrl()); + + Optional.ofNullable(prometheusConfig.getRequestSignerIdentifier()) + .ifPresent(region -> builder.setRequestSigner(getRequestSigner(prometheusConfig))); Review Comment: I agree, on top of your point, moving it to getRequestSigner is better here especially since we want to separate any Amazon config away from flink-connector-prometheus package. Updated and added unit test for this ########## flink-connector-prometheus/src/main/java/org/apache/flink/connector/prometheus/table/PrometheusDynamicSink.java: ########## @@ -0,0 +1,193 @@ +/* + * 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.flink.connector.prometheus.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.base.table.sink.AsyncDynamicTableSink; +import org.apache.flink.connector.base.table.sink.AsyncDynamicTableSinkBuilder; +import org.apache.flink.connector.prometheus.sink.PrometheusRequestSigner; +import org.apache.flink.connector.prometheus.sink.PrometheusSink; +import org.apache.flink.connector.prometheus.sink.PrometheusSinkBuilder; +import org.apache.flink.connector.prometheus.sink.PrometheusSinkConfiguration; +import org.apache.flink.connector.prometheus.sink.prometheus.Types; +import org.apache.flink.table.connector.ChangelogMode; +import org.apache.flink.table.connector.sink.DynamicTableSink; +import org.apache.flink.table.connector.sink.SinkV2Provider; +import org.apache.flink.table.connector.sink.abilities.SupportsPartitioning; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.types.DataType; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.Iterator; +import java.util.Map; +import java.util.Optional; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +public class PrometheusDynamicSink extends AsyncDynamicTableSink<Types.TimeSeries> + implements SupportsPartitioning { + private static final Logger LOG = LoggerFactory.getLogger(PrometheusDynamicSink.class); + + /** Consumed data type of the table. */ + private final DataType physicalDataType; + + private final PrometheusConfig prometheusConfig; + + protected PrometheusDynamicSink( + @Nullable Integer maxBatchSize, + @Nullable Integer maxInFlightRequests, + @Nullable Integer maxBufferedRequests, + @Nullable Long maxBufferSizeInBytes, + @Nullable Long maxTimeInBufferMS, + @Nullable DataType physicalDataType, + PrometheusConfig prometheusConfig) { + + super( + maxBatchSize, // maxBatchSizeInSamples + maxInFlightRequests, + maxBufferedRequests, + maxBufferSizeInBytes, // maxRecordSizeInSamples + maxTimeInBufferMS); + + this.physicalDataType = + Preconditions.checkNotNull(physicalDataType, "Consumed data type must not be null"); + this.prometheusConfig = prometheusConfig; + } + + @Override + public ChangelogMode getChangelogMode(ChangelogMode changelogMode) { + return ChangelogMode.upsert(); + } + + @Override + public SinkRuntimeProvider getSinkRuntimeProvider(Context context) { + + PrometheusSinkBuilder<RowData> builder = + new PrometheusSinkBuilder<RowData>() + .setRetryConfiguration( + PrometheusSinkConfiguration.RetryConfiguration + .DEFAULT_RETRY_CONFIGURATION) + .setElementConverter( + new RowDataElementConverter(physicalDataType, prometheusConfig)) + .setPrometheusRemoteWriteUrl(prometheusConfig.getRemoteWriteEndpointUrl()); + + Optional.ofNullable(prometheusConfig.getRequestSignerIdentifier()) + .ifPresent(region -> builder.setRequestSigner(getRequestSigner(prometheusConfig))); + Optional.ofNullable(maxBatchSize).ifPresent(builder::setMaxBatchSizeInSamples); + Optional.ofNullable(maxBufferSizeInBytes).ifPresent(builder::setMaxRecordSizeInSamples); + + PrometheusSink<RowData> prometheusSink = builder.build(); + return SinkV2Provider.of(prometheusSink); + } + + @Override + public DynamicTableSink copy() { + return new PrometheusDynamicSink( + maxBatchSize, + maxInFlightRequests, + maxBufferedRequests, + maxBufferSizeInBytes, + maxTimeInBufferMS, + physicalDataType, + prometheusConfig); + } + + @Override + public String asSummaryString() { + return "Prometheus"; + } + + @Override + public void applyStaticPartition(Map<String, String> partitions) { + // We don't need to do anything here because the Prometheus sink handles a static partition + // just like a normal partition. + } + + private PrometheusRequestSigner getRequestSigner(PrometheusConfig prometheusConfig) { + ServiceLoader<PrometheusDynamicRequestSignerFactory> loader = + ServiceLoader.load(PrometheusDynamicRequestSignerFactory.class); + Iterator<PrometheusDynamicRequestSignerFactory> factories = loader.iterator(); + String requestSignerIdentifier = prometheusConfig.getRequestSignerIdentifier(); + + while (true) { + try { + if (!factories.hasNext()) { + break; + } + + PrometheusDynamicRequestSignerFactory factory = factories.next(); Review Comment: I have converted it to a Stream to make this more readable -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org