nastra commented on code in PR #16250: URL: https://github.com/apache/iceberg/pull/16250#discussion_r3529931146
########## core/src/main/java/org/apache/iceberg/metrics/OtelMetricsReporter.java: ########## @@ -0,0 +1,651 @@ +/* + * 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.iceberg.metrics; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.Meter; +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A {@link MetricsReporter} that exports Iceberg {@link ScanReport} and {@link CommitReport} via + * OpenTelemetry. + * + * <p>The reporter does not own the OpenTelemetry SDK. It obtains the {@link OpenTelemetry} instance + * from {@link GlobalOpenTelemetry}, which the host application (Spark, Flink, Trino, ...) is + * expected to register — typically via {@code + * OpenTelemetrySdk.builder()...buildAndRegisterGlobal()} or via the OpenTelemetry Java agent. If no + * SDK has been registered, OpenTelemetry returns a no-op implementation and metric calls are + * silently dropped, matching the standard OpenTelemetry API contract. + * + * <p>Endpoint, exporter, headers, resource attributes, and exporter intervals are configured by the + * host application or via the standard OpenTelemetry environment variables (e.g. {@code + * OTEL_EXPORTER_OTLP_ENDPOINT}, {@code OTEL_SERVICE_NAME}, {@code OTEL_EXPORTER_OTLP_HEADERS}). + * + * <p>Configure as the Iceberg metrics reporter via catalog properties: + * + * <pre>{@code + * metrics-reporter-impl=org.apache.iceberg.metrics.OtelMetricsReporter + * }</pre> + * + * <h2>Cardinality</h2> + * + * By default every emitted metric carries the following attribute set: + * + * <ul> + * <li>Scan metrics: {@code iceberg.table.name} + * <li>Commit metrics: {@code iceberg.table.name}, {@code iceberg.operation} + * </ul> + * + * <p>The set of attributes is configurable via the {@code iceberg.otel.metrics.attributes} catalog + * property, which takes a comma-separated allowlist of attribute short names. Recognized names are + * {@code table-name}, {@code schema-id}, and {@code operation}; any attribute whose short name is + * not listed is omitted from emitted metric points. + * + * <p>For example, to additionally include {@code iceberg.schema.id} (useful for correlating scan + * performance with schema evolution): + * + * <pre>{@code + * iceberg.otel.metrics.attributes=table-name,schema-id,operation + * }</pre> + * + * <p>Or to exclude {@code iceberg.table.name} entirely in deployments with a very large number of + * tables: + * + * <pre>{@code + * iceberg.otel.metrics.attributes=operation + * }</pre> + * + * <p>Or to emit metrics with no attributes at all (single aggregate time series per metric), use + * {@code none} or an empty string: + * + * <pre>{@code + * iceberg.otel.metrics.attributes=none + * }</pre> + * + * <p>When the property is not set, the default attribute set above is used. + * + * <p>The snapshot id is deliberately <b>not</b> attached as a metric attribute. Snapshot ids are + * monotonically increasing and unique per commit, so including them would create a new time series + * for every commit and risk unbounded cardinality in any time-series backend (Prometheus, + * CloudWatch, Datadog, etc.). + */ +public class OtelMetricsReporter implements MetricsReporter { + private static final Logger LOG = LoggerFactory.getLogger(OtelMetricsReporter.class); + private static final String INSTRUMENTATION_NAME = "org.apache.iceberg"; + + static final String ATTRIBUTES_PROPERTY = "iceberg.otel.metrics.attributes"; + static final String ATTR_NAME_TABLE_NAME = "table-name"; + static final String ATTR_NAME_SCHEMA_ID = "schema-id"; + static final String ATTR_NAME_OPERATION = "operation"; + static final String ATTR_VALUE_NONE = "none"; + private static final Set<String> KNOWN_ATTRIBUTE_NAMES = + ImmutableSet.of(ATTR_NAME_TABLE_NAME, ATTR_NAME_SCHEMA_ID, ATTR_NAME_OPERATION); + + static final AttributeKey<String> ATTR_TABLE_NAME = AttributeKey.stringKey("iceberg.table.name"); + static final AttributeKey<Long> ATTR_SCHEMA_ID = AttributeKey.longKey("iceberg.schema.id"); + static final AttributeKey<String> ATTR_OPERATION = AttributeKey.stringKey("iceberg.operation"); + + private Meter meter; + private boolean includeTableName = true; + private boolean includeSchemaId = false; + private boolean includeOperation = true; + + // Scan metrics instruments + private DoubleHistogram scanPlanningDuration; Review Comment: it might be worth having a small wrapper class so that you don't have to prefix all of the metrics with either scan or commit. That wrapper could have a static method where it converts from `ScanMetricsResult` to that wrapper with all the counters initialized/converted. The same would apply for the commit metrics -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
