adutra commented on code in PR #2998: URL: https://github.com/apache/polaris/pull/2998#discussion_r2569185870
########## runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEventMetadataFactory.java: ########## @@ -0,0 +1,138 @@ +/* + * 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; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanContext; +import io.quarkus.security.identity.CurrentIdentityAssociation; +import io.quarkus.security.identity.SecurityIdentity; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Instance; +import jakarta.inject.Inject; +import java.time.Clock; +import java.util.Map; +import java.util.Optional; +import org.apache.polaris.core.auth.PolarisPrincipal; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.service.tracing.RequestIdFilter; +import org.jboss.resteasy.reactive.server.core.CurrentRequestManager; +import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; +import org.jboss.resteasy.reactive.server.jaxrs.ContainerRequestContextImpl; + +@ApplicationScoped +public class PolarisEventMetadataFactory { + + @Inject Clock clock; + @Inject CurrentIdentityAssociation currentIdentityAssociation; + @Inject Instance<RealmContext> realmContext; + + /** + * Creates a new event metadata object. + * + * <p>This method should only be called with the request scope active in production. It may be + * called outside the request scope in tests, in which case some fields may be missing. + */ + public PolarisEventMetadata create() { + return PolarisEventMetadata.builder() + .timestamp(clock.instant()) + .realmId(getRealmId()) + .user(getUser()) + .requestId(getRequestId()) + .openTelemetryContext(getOpenTelemetryContext()) + .build(); + } + + /** + * Creates a copy of the given metadata, with a new timestamp and new OpenTelemetry context. + * + * <p>Contrary to {@link #create()}, this method does not require an active request scope, and is + * safe to use from any thread, e.g. from a task executor thread. + */ + public PolarisEventMetadata copy(PolarisEventMetadata original) { + return PolarisEventMetadata.builder() + .from(original) + .timestamp(clock.instant()) + .openTelemetryContext(getOpenTelemetryContext()) + .build(); + } + + /** + * Extracts the realm ID from the current realm context. + * + * <p>In the unlikely case there is no realm context, e.g. in some tests, returns + * "UNKNOWN_REALM_ID". + */ + private String getRealmId() { + return realmContext.isResolvable() Review Comment: In some tests, events are generated on the "client side", in which case, there is no realm context. (In production code there is always a ream context though.) -- 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]
