hkwi commented on code in PR #4836:
URL: https://github.com/apache/polaris/pull/4836#discussion_r3445523127


##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/opentelemetry/OpenTelemetryEventListener.java:
##########
@@ -0,0 +1,345 @@
+/*
+ * 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.polaris.service.events.listeners.opentelemetry;
+
+import static io.opentelemetry.api.common.AttributeKey.booleanKey;
+import static io.opentelemetry.api.common.AttributeKey.longKey;
+import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
+import static io.opentelemetry.api.common.AttributeKey.stringKey;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.api.common.AttributesBuilder;
+import io.opentelemetry.api.logs.Logger;
+import io.opentelemetry.api.logs.Severity;
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.api.trace.SpanContext;
+import io.opentelemetry.api.trace.TraceFlags;
+import io.opentelemetry.api.trace.TraceState;
+import io.opentelemetry.context.Context;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.polaris.core.admin.model.GrantResource;
+import org.apache.polaris.service.events.AttributeKey;
+import org.apache.polaris.service.events.EventAttributes;
+import org.apache.polaris.service.events.PolarisEvent;
+import org.apache.polaris.service.events.PolarisEventMetadata;
+import org.apache.polaris.service.events.listeners.PolarisEventListener;
+import org.slf4j.LoggerFactory;
+
+@ApplicationScoped
+@Identifier("opentelemetry")
+public class OpenTelemetryEventListener implements PolarisEventListener {
+  static final String INSTRUMENTATION_SCOPE_NAME = "org.apache.polaris.events";
+
+  static final String EVENT_TYPE_ATTRIBUTE_NAME = "polaris.event.type";
+  static final String EVENT_CATEGORY_ATTRIBUTE_NAME = "polaris.event.category";
+  static final String REALM_ID_ATTRIBUTE_NAME = "polaris.realm.id";
+  static final String REQUEST_ID_ATTRIBUTE_NAME = "polaris.request.id";
+  static final String ACTOR_NAME_ATTRIBUTE_NAME = "polaris.actor.name";
+  static final String ACTOR_ROLES_ATTRIBUTE_NAME = "polaris.actor.roles";
+  static final String PRINCIPAL_NAME_ATTRIBUTE_NAME = "polaris.principal.name";
+
+  static final String CATALOG_NAME_ATTRIBUTE_NAME = "polaris.catalog.name";
+  static final String NAMESPACE_ATTRIBUTE_NAME = "polaris.namespace";
+  static final String NAMESPACE_FQN_ATTRIBUTE_NAME = "polaris.namespace.fqn";
+  static final String PARENT_NAMESPACE_FQN_ATTRIBUTE_NAME = 
"polaris.parent_namespace.fqn";
+  static final String TABLE_NAME_ATTRIBUTE_NAME = "polaris.table.name";
+  static final String TABLE_IDENTIFIER_ATTRIBUTE_NAME = 
"polaris.table.identifier";
+  static final String VIEW_NAME_ATTRIBUTE_NAME = "polaris.view.name";
+  static final String VIEW_IDENTIFIER_ATTRIBUTE_NAME = 
"polaris.view.identifier";
+  static final String PRINCIPAL_ROLE_NAME_ATTRIBUTE_NAME = 
"polaris.principal_role.name";
+  static final String CATALOG_ROLE_NAME_ATTRIBUTE_NAME = 
"polaris.catalog_role.name";
+  static final String PRIVILEGE_ATTRIBUTE_NAME = "polaris.privilege";
+  static final String GRANT_RESOURCE_ATTRIBUTE_NAME = "polaris.grant.resource";
+  static final String GRANT_RESOURCE_TYPE_ATTRIBUTE_NAME = 
"polaris.grant.resource.type";
+  static final String ADD_GRANT_REQUEST_ATTRIBUTE_NAME = 
"polaris.grant.add_request";
+  static final String REVOKE_GRANT_REQUEST_ATTRIBUTE_NAME = 
"polaris.grant.revoke_request";
+  static final String CASCADE_ATTRIBUTE_NAME = "polaris.cascade";
+  static final String WAREHOUSE_ATTRIBUTE_NAME = "polaris.warehouse";
+  static final String ACCESS_DELEGATION_MODE_ATTRIBUTE_NAME = 
"polaris.access_delegation_mode";
+  static final String IF_NONE_MATCH_ATTRIBUTE_NAME = "polaris.if_none_match";
+  static final String SNAPSHOTS_ATTRIBUTE_NAME = "polaris.snapshots";
+  static final String PURGE_REQUESTED_ATTRIBUTE_NAME = 
"polaris.purge_requested";
+  static final String TASK_ENTITY_ID_ATTRIBUTE_NAME = "polaris.task.entity_id";
+  static final String TASK_ATTEMPT_ATTRIBUTE_NAME = "polaris.task.attempt";
+  static final String TASK_SUCCESS_ATTRIBUTE_NAME = "polaris.task.success";
+  static final String HTTP_METHOD_ATTRIBUTE_NAME = "polaris.http.method";
+  static final String REQUEST_URI_ATTRIBUTE_NAME = "polaris.http.request_uri";
+  static final String NAMESPACE_NAME_ATTRIBUTE_NAME = "polaris.namespace.name";
+  static final String GENERIC_TABLE_NAME_ATTRIBUTE_NAME = 
"polaris.generic_table.name";
+  static final String POLICY_NAME_ATTRIBUTE_NAME = "polaris.policy.name";
+  static final String POLICY_TYPE_ATTRIBUTE_NAME = "polaris.policy.type";
+  static final String TARGET_NAME_ATTRIBUTE_NAME = "polaris.target.name";
+  static final String DETACH_ALL_ATTRIBUTE_NAME = "polaris.detach_all";
+
+  private static final org.slf4j.Logger LOGGER =
+      LoggerFactory.getLogger(OpenTelemetryEventListener.class);
+
+  private final Logger openTelemetryLogger;
+  private final ObjectMapper objectMapper;
+
+  @Inject
+  public OpenTelemetryEventListener(OpenTelemetry openTelemetry, ObjectMapper 
objectMapper) {
+    this.openTelemetryLogger =
+        
openTelemetry.getLogsBridge().loggerBuilder(INSTRUMENTATION_SCOPE_NAME).build();
+    this.objectMapper = objectMapper;
+  }
+
+  @Override
+  public void onEvent(PolarisEvent event) {
+    var logRecordBuilder =
+        openTelemetryLogger
+            .logRecordBuilder()
+            .setTimestamp(event.metadata().timestamp())
+            .setSeverity(Severity.INFO)
+            .setSeverityText("INFO")
+            .setEventName(event.type().name())
+            .setBody(event.type().name())
+            .setAllAttributes(toLogAttributes(event));
+    toOpenTelemetryContext(event).ifPresent(logRecordBuilder::setContext);
+    logRecordBuilder.emit();
+  }
+
+  private Attributes toLogAttributes(PolarisEvent event) {
+    AttributesBuilder attributes = Attributes.builder();
+    attributes.put(EVENT_TYPE_ATTRIBUTE_NAME, event.type().name());
+    attributes.put(EVENT_CATEGORY_ATTRIBUTE_NAME, 
event.type().category().name());
+
+    PolarisEventMetadata metadata = event.metadata();
+    attributes.put(REALM_ID_ATTRIBUTE_NAME, metadata.realmId());
+    metadata
+        .requestId()
+        .ifPresent(requestId -> attributes.put(REQUEST_ID_ATTRIBUTE_NAME, 
requestId));
+    metadata
+        .user()
+        .ifPresent(
+            principal -> {
+              attributes.put(ACTOR_NAME_ATTRIBUTE_NAME, principal.getName());
+              attributes.put(
+                  stringArrayKey(ACTOR_ROLES_ATTRIBUTE_NAME), 
List.copyOf(principal.getRoles()));
+            });
+    metadata.openTelemetryContext().forEach(attributes::put);
+
+    putStringAttribute(
+        attributes, event, CATALOG_NAME_ATTRIBUTE_NAME, 
EventAttributes.CATALOG_NAME);
+    putStringAttribute(
+        attributes,
+        event,
+        NAMESPACE_ATTRIBUTE_NAME,
+        EventAttributes.NAMESPACE,
+        Namespace::toString);
+    putStringAttribute(
+        attributes, event, NAMESPACE_FQN_ATTRIBUTE_NAME, 
EventAttributes.NAMESPACE_FQN);
+    putStringAttribute(
+        attributes,
+        event,
+        PARENT_NAMESPACE_FQN_ATTRIBUTE_NAME,
+        EventAttributes.PARENT_NAMESPACE_FQN);
+    putStringAttribute(attributes, event, TABLE_NAME_ATTRIBUTE_NAME, 
EventAttributes.TABLE_NAME);
+    putStringAttribute(
+        attributes, event, TABLE_IDENTIFIER_ATTRIBUTE_NAME, 
EventAttributes.TABLE_IDENTIFIER);
+    derivedTableIdentifier(event)
+        .ifPresent(
+            tableIdentifier -> attributes.put(TABLE_IDENTIFIER_ATTRIBUTE_NAME, 
tableIdentifier));
+    putStringAttribute(attributes, event, VIEW_NAME_ATTRIBUTE_NAME, 
EventAttributes.VIEW_NAME);
+    putStringAttribute(
+        attributes, event, VIEW_IDENTIFIER_ATTRIBUTE_NAME, 
EventAttributes.VIEW_IDENTIFIER);
+    derivedViewIdentifier(event)
+        .ifPresent(
+            viewIdentifier -> attributes.put(VIEW_IDENTIFIER_ATTRIBUTE_NAME, 
viewIdentifier));
+    putStringAttribute(
+        attributes, event, PRINCIPAL_NAME_ATTRIBUTE_NAME, 
EventAttributes.PRINCIPAL_NAME);
+    putStringAttribute(
+        attributes, event, PRINCIPAL_ROLE_NAME_ATTRIBUTE_NAME, 
EventAttributes.PRINCIPAL_ROLE_NAME);

Review Comment:
   Thanks, updated in 4b97f326. The event attribute forwarding is now 
represented by a `PolarisOtelAttribute` enum and `toLogAttributes` iterates 
over the enum values, with typed helper forwarders for string, boolean, long, 
JSON, and derived attributes.



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