-
Notifications
You must be signed in to change notification settings - Fork 329
Generate Request IDs (if not specified); Return Request ID as a Header #2602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
49a182b
f44d70a
1c10523
6632c21
eee3e69
0bd7a21
ea02c79
5a75b0d
6f90986
1eee9ff
f44b890
588a26d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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.tracing; | ||
|
|
||
| import jakarta.annotation.Priority; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.ws.rs.container.ContainerRequestContext; | ||
| import jakarta.ws.rs.container.ContainerRequestFilter; | ||
| import jakarta.ws.rs.container.PreMatching; | ||
| import jakarta.ws.rs.ext.Provider; | ||
| import org.apache.polaris.service.config.FilterPriorities; | ||
| import org.apache.polaris.service.logging.LoggingConfiguration; | ||
|
|
||
| @PreMatching | ||
| @ApplicationScoped | ||
| @Priority(FilterPriorities.REQUEST_ID_FILTER) | ||
| @Provider | ||
| public class RequestIdFilter implements ContainerRequestFilter { | ||
|
|
||
| public static final String REQUEST_ID_KEY = "requestId"; | ||
|
|
||
| @Inject LoggingConfiguration loggingConfiguration; | ||
| @Inject RequestIdGenerator requestIdGenerator; | ||
|
|
||
| @Override | ||
| public void filter(ContainerRequestContext rc) { | ||
| var requestId = rc.getHeaderString(loggingConfiguration.requestIdHeaderName()); | ||
adutra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (requestId == null) { | ||
| requestId = requestIdGenerator.generateRequestId(); | ||
| } | ||
| rc.setProperty(REQUEST_ID_KEY, requestId); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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.tracing; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| @ApplicationScoped | ||
| public class RequestIdGenerator { | ||
adutra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static final Long COUNTER_SOFT_MAX = Long.MAX_VALUE / 2; | ||
|
|
||
| record State(String uuid, long counter) { | ||
|
|
||
| State() { | ||
| this(UUID.randomUUID().toString(), 1); | ||
| } | ||
|
|
||
| String requestId() { | ||
| return String.format("%s_%019d", uuid, counter); | ||
| } | ||
|
|
||
| State increment() { | ||
| return counter >= COUNTER_SOFT_MAX ? new State() : new State(uuid, counter + 1); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
| } | ||
| } | ||
|
|
||
| final AtomicReference<State> state = new AtomicReference<>(new State()); | ||
|
|
||
| public String generateRequestId() { | ||
| return state.getAndUpdate(State::increment).requestId(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public void setCounter(long counter) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this method, |
||
| state.set(new State(state.get().uuid, counter)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * 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.tracing; | ||
|
|
||
| import static org.apache.polaris.service.tracing.RequestIdFilter.REQUEST_ID_KEY; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.ws.rs.container.ContainerRequestContext; | ||
| import jakarta.ws.rs.container.ContainerResponseContext; | ||
| import jakarta.ws.rs.container.ContainerResponseFilter; | ||
| import jakarta.ws.rs.ext.Provider; | ||
| import org.apache.polaris.service.logging.LoggingConfiguration; | ||
|
|
||
| @ApplicationScoped | ||
| @Provider | ||
| public class RequestIdResponseFilter implements ContainerResponseFilter { | ||
|
|
||
| @Inject LoggingConfiguration loggingConfiguration; | ||
|
|
||
| @Override | ||
| public void filter( | ||
| ContainerRequestContext requestContext, ContainerResponseContext responseContext) { | ||
| responseContext | ||
| .getHeaders() | ||
| .add( | ||
| loggingConfiguration.requestIdHeaderName(), requestContext.getProperty(REQUEST_ID_KEY)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * 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.tracing; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class RequestIdGeneratorTest { | ||
|
|
||
| private RequestIdGenerator requestIdGenerator; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| requestIdGenerator = new RequestIdGenerator(); | ||
| } | ||
|
|
||
| @Test | ||
| void testGenerateRequestId_ReturnsValidFormat() { | ||
| String requestId = requestIdGenerator.generateRequestId(); | ||
|
|
||
| assertThat(requestId).isNotNull(); | ||
| assertThat(requestId).matches(this::isValidRequestIdFormat); | ||
| // First call should increment counter to 1 | ||
| assertThat(extractCounterFromRequestId(requestId)).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void testGenerateRequestId_ReturnsUniqueIds() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test could be made multi-threaded. |
||
| Set<String> generatedIds = new HashSet<>(); | ||
|
|
||
| // Generate multiple request IDs and verify they're all unique | ||
| for (int i = 0; i < 1000; i++) { | ||
| String requestId = requestIdGenerator.generateRequestId(); | ||
| assertThat(generatedIds).doesNotContain(requestId); | ||
| generatedIds.add(requestId); | ||
| } | ||
|
|
||
| assertThat(generatedIds).hasSize(1000); | ||
| } | ||
|
|
||
| @Test | ||
| void testCounterIncrementsSequentially() { | ||
| // requestIdGenerator.setCounter(0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: dangling comment. |
||
|
|
||
| String firstId = requestIdGenerator.generateRequestId(); | ||
| String secondId = requestIdGenerator.generateRequestId(); | ||
| String thirdId = requestIdGenerator.generateRequestId(); | ||
|
|
||
| assertThat(extractCounterFromRequestId(firstId)).isEqualTo(1); | ||
| assertThat(extractCounterFromRequestId(secondId)).isEqualTo(2); | ||
| assertThat(extractCounterFromRequestId(thirdId)).isEqualTo(3); | ||
| } | ||
|
|
||
| @Test | ||
| void testCounterRotationAtSoftMax() { | ||
| // Set counter close to soft max | ||
| long softMax = RequestIdGenerator.COUNTER_SOFT_MAX; | ||
| requestIdGenerator.setCounter(softMax); | ||
|
|
||
| String beforeRotation = requestIdGenerator.generateRequestId(); | ||
| String afterRotation = requestIdGenerator.generateRequestId(); | ||
|
|
||
| // The UUID part should be different after rotation | ||
| String beforeUuidPart = beforeRotation.substring(0, beforeRotation.lastIndexOf('_')); | ||
| String afterUuidPart = afterRotation.substring(0, afterRotation.lastIndexOf('_')); | ||
| assertNotEquals(beforeUuidPart, afterUuidPart); | ||
|
|
||
| assertThat(extractCounterFromRequestId(beforeRotation)).isEqualTo(softMax); | ||
| // Counter reset to 1 (after increment from 0) | ||
| assertThat(extractCounterFromRequestId(afterRotation)).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void testSetCounterChangesNextGeneratedId() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this test has any value, |
||
| requestIdGenerator.setCounter(100); | ||
|
|
||
| String requestId = requestIdGenerator.generateRequestId(); | ||
|
|
||
| // Should increment from set value | ||
| assertThat(extractCounterFromRequestId(requestId)).isEqualTo(100); | ||
| } | ||
|
|
||
| private boolean isValidRequestIdFormat(String str) { | ||
| try { | ||
| String[] requestIdParts = str.split("_"); | ||
| String uuid = requestIdParts[0]; | ||
| String counter = requestIdParts[1]; | ||
| UUID.fromString(uuid); | ||
| Long.parseLong(counter); | ||
| return true; | ||
| } catch (IllegalArgumentException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private long extractCounterFromRequestId(String requestId) { | ||
| return Long.parseLong(requestId.split("_")[1]); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: ->
REALM_CONTEXT_FILTER - 1to make it more readable?