-
Notifications
You must be signed in to change notification settings - Fork 122
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
Provide a way to log successful and failed requests #719
Open
ikhoon
wants to merge
3
commits into
line:main
Choose a base branch
from
ikhoon:logging-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+628
−18
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import static com.google.common.base.Strings.isNullOrEmpty; | ||
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.API_V0_PATH_PREFIX; | ||
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.API_V1_PATH_PREFIX; | ||
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.DOCS_PATH; | ||
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.HEALTH_CHECK_PATH; | ||
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.METRICS_PATH; | ||
import static com.linecorp.centraldogma.server.auth.AuthProvider.BUILTIN_WEB_BASE_PATH; | ||
|
@@ -38,6 +39,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.Executor; | ||
|
@@ -88,6 +90,7 @@ | |
import com.linecorp.armeria.server.ServerPort; | ||
import com.linecorp.armeria.server.ServiceNaming; | ||
import com.linecorp.armeria.server.ServiceRequestContext; | ||
import com.linecorp.armeria.server.TransientServiceOption; | ||
import com.linecorp.armeria.server.auth.AuthService; | ||
import com.linecorp.armeria.server.auth.Authorizer; | ||
import com.linecorp.armeria.server.docs.DocService; | ||
|
@@ -96,6 +99,7 @@ | |
import com.linecorp.armeria.server.file.HttpFile; | ||
import com.linecorp.armeria.server.healthcheck.HealthCheckService; | ||
import com.linecorp.armeria.server.logging.AccessLogWriter; | ||
import com.linecorp.armeria.server.logging.LoggingService; | ||
import com.linecorp.armeria.server.metric.MetricCollectingService; | ||
import com.linecorp.armeria.server.metric.PrometheusExpositionService; | ||
import com.linecorp.armeria.server.thrift.THttpService; | ||
|
@@ -147,11 +151,11 @@ | |
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; | ||
import io.micrometer.core.instrument.binder.system.DiskSpaceMetrics; | ||
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics; | ||
import io.micrometer.core.instrument.binder.system.ProcessorMetrics; | ||
import io.micrometer.core.instrument.binder.system.UptimeMetrics; | ||
|
@@ -206,6 +210,8 @@ public static CentralDogma forConfig(File configFile) throws IOException { | |
private PrometheusMeterRegistry meterRegistry; | ||
@Nullable | ||
private SessionManager sessionManager; | ||
@Nullable | ||
private JvmGcMetrics jvmGcMetrics; | ||
|
||
CentralDogma(CentralDogmaConfig cfg) { | ||
this.cfg = requireNonNull(cfg, "cfg"); | ||
|
@@ -385,13 +391,13 @@ private CommandExecutor startCommandExecutor( | |
logger.info("Starting plugins on the leader replica .."); | ||
pluginsForLeaderOnly | ||
.start(cfg, pm, exec, meterRegistry, purgeWorker).handle((unused, cause) -> { | ||
if (cause == null) { | ||
logger.info("Started plugins on the leader replica."); | ||
} else { | ||
logger.error("Failed to start plugins on the leader replica..", cause); | ||
} | ||
return null; | ||
}); | ||
if (cause == null) { | ||
logger.info("Started plugins on the leader replica."); | ||
} else { | ||
logger.error("Failed to start plugins on the leader replica..", cause); | ||
} | ||
return null; | ||
}); | ||
} | ||
}; | ||
|
||
|
@@ -521,9 +527,67 @@ private Server startServer(ProjectManager pm, CommandExecutor executor, | |
|
||
sb.service("/title", webAppTitleFile(cfg.webAppTitle(), SystemInfo.hostname()).asService()); | ||
|
||
sb.service(HEALTH_CHECK_PATH, HealthCheckService.of()); | ||
final RequestLogConfig requestLogConfig = cfg.requestLogConfig(); | ||
boolean logHealthCheck = false; | ||
boolean logMetrics = false; | ||
if (requestLogConfig != null) { | ||
final Function<? super HttpService, LoggingService> loggingService = | ||
LoggingService.builder() | ||
.logger(requestLogConfig.loggerName()) | ||
.requestLogLevel(requestLogConfig.requestLogLevel()) | ||
.successfulResponseLogLevel(requestLogConfig.successfulResponseLogLevel()) | ||
.failureResponseLogLevel(requestLogConfig.failureResponseLogLevel()) | ||
.successSamplingRate(requestLogConfig.successSamplingRate()) | ||
.failureSamplingRate(requestLogConfig.failureSamplingRate()) | ||
.newDecorator(); | ||
final Set<RequestLogGroup> requestLogGroups = requestLogConfig.targetGroups(); | ||
if (requestLogGroups.contains(RequestLogGroup.ALL)) { | ||
sb.decorator(loggingService); | ||
logHealthCheck = true; | ||
logMetrics = true; | ||
} else { | ||
for (RequestLogGroup logGroup : requestLogGroups) { | ||
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. How about also supporting the specified path? e.g. |
||
switch (logGroup) { | ||
case API: | ||
sb.decoratorUnder("/api", loggingService); | ||
break; | ||
case METRICS: | ||
sb.decorator(METRICS_PATH, loggingService); | ||
logMetrics = true; | ||
break; | ||
case HEALTH: | ||
sb.decorator(HEALTH_CHECK_PATH, loggingService); | ||
logHealthCheck = true; | ||
break; | ||
case DOCS: | ||
sb.decoratorUnder(DOCS_PATH, loggingService); | ||
break; | ||
case WEB: | ||
for (String webResources : ImmutableList.of("/web", "/vendor", | ||
"/scripts", "/styles")) { | ||
sb.decoratorUnder(webResources, loggingService); | ||
} | ||
break; | ||
case ALL: | ||
// Should never reach here. | ||
throw new Error(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
final HealthCheckService healthCheckService; | ||
if (logHealthCheck) { | ||
healthCheckService = | ||
HealthCheckService.builder() | ||
.transientServiceOptions(TransientServiceOption.WITH_SERVICE_LOGGING) | ||
.build(); | ||
} else { | ||
healthCheckService = HealthCheckService.of(); | ||
} | ||
sb.service(HEALTH_CHECK_PATH, healthCheckService); | ||
|
||
sb.serviceUnder("/docs/", | ||
sb.serviceUnder(DOCS_PATH, | ||
DocService.builder() | ||
.exampleHeaders(CentralDogmaService.class, | ||
HttpHeaders.of(HttpHeaderNames.AUTHORIZATION, | ||
|
@@ -532,7 +596,7 @@ private Server startServer(ProjectManager pm, CommandExecutor executor, | |
|
||
configureHttpApi(sb, pm, executor, watchService, mds, authProvider, sessionManager); | ||
|
||
configureMetrics(sb, meterRegistry); | ||
configureMetrics(sb, meterRegistry, logMetrics); | ||
|
||
// Configure access log format. | ||
final String accessLogFormat = cfg.accessLogFormat(); | ||
|
@@ -776,9 +840,18 @@ private static Function<? super HttpService, EncodingService> contentEncodingDec | |
.build(delegate); | ||
} | ||
|
||
private void configureMetrics(ServerBuilder sb, PrometheusMeterRegistry registry) { | ||
private void configureMetrics(ServerBuilder sb, PrometheusMeterRegistry registry, boolean logMetrics) { | ||
sb.meterRegistry(registry); | ||
sb.service(METRICS_PATH, new PrometheusExpositionService(registry.getPrometheusRegistry())); | ||
final PrometheusExpositionService expositionService; | ||
if (logMetrics) { | ||
expositionService = PrometheusExpositionService.builder(registry.getPrometheusRegistry()) | ||
.transientServiceOptions( | ||
TransientServiceOption.WITH_SERVICE_LOGGING) | ||
.build(); | ||
} else { | ||
expositionService = PrometheusExpositionService.of(registry.getPrometheusRegistry()); | ||
} | ||
sb.service(METRICS_PATH, expositionService); | ||
sb.decorator(MetricCollectingService.newDecorator(MeterIdPrefixFunction.ofDefault("api"))); | ||
|
||
// Bind system metrics. | ||
|
@@ -787,7 +860,8 @@ private void configureMetrics(ServerBuilder sb, PrometheusMeterRegistry registry | |
new ClassLoaderMetrics().bindTo(registry); | ||
new UptimeMetrics().bindTo(registry); | ||
new DiskSpaceMetrics(cfg.dataDir()).bindTo(registry); | ||
new JvmGcMetrics().bindTo(registry); | ||
jvmGcMetrics = new JvmGcMetrics(); | ||
jvmGcMetrics.bindTo(registry); | ||
new JvmMemoryMetrics().bindTo(registry); | ||
new JvmThreadMetrics().bindTo(registry); | ||
|
||
|
@@ -817,6 +891,11 @@ private void doStop() { | |
meterRegistry = null; | ||
} | ||
|
||
if (jvmGcMetrics != null) { | ||
jvmGcMetrics.close(); | ||
jvmGcMetrics = null; | ||
} | ||
|
||
logger.info("Stopping the Central Dogma .."); | ||
if (!doStop(server, executor, pm, repositoryWorker, purgeWorker, sessionManager)) { | ||
logger.warn("Stopped the Central Dogma with failure."); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
server/src/main/java/com/linecorp/centraldogma/server/RequestLogConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.centraldogma.server; | ||
|
||
import static com.google.common.base.MoreObjects.firstNonNull; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.MoreObjects; | ||
|
||
import com.linecorp.armeria.common.logging.LogLevel; | ||
|
||
/** | ||
* A request log configuration. | ||
*/ | ||
public final class RequestLogConfig { | ||
|
||
private final Set<RequestLogGroup> targetGroups; | ||
|
||
private final LogLevel requestLogLevel; | ||
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. Shouldn't we allow to set different settings per |
||
private final LogLevel successfulResponseLogLevel; | ||
private final LogLevel failureResponseLogLevel; | ||
|
||
private final float successSamplingRate; | ||
private final float failureSamplingRate; | ||
private final String loggerName; | ||
|
||
/** | ||
* Create a new instance. | ||
* | ||
* @param targetGroups the target {@link RequestLogGroup}s which should be logged. | ||
* @param loggerName the logger name to use when logging. | ||
* @param requestLogLevel the {@link LogLevel} to use when logging incoming requests. | ||
* @param successfulResponseLogLevel the {@link LogLevel} to use when logging successful responses | ||
* (e.g., an HTTP status is less than 400). | ||
* @param failureResponseLogLevel the {@link LogLevel} to use when logging failed responses | ||
* (e.g., an HTTP status is 4xx or 5xx). | ||
* @param successSamplingRate The rate at which to sample success requests to log. Any number between | ||
* {@code 0.0} and {@code 1.0} will cause a random sample of the requests to | ||
* be logged. | ||
* @param failureSamplingRate The rate at which to sample failed requests to log. Any number between | ||
* {@code 0.0} and {@code 1.0} will cause a random sample of the requests to | ||
* be logged. | ||
*/ | ||
public RequestLogConfig( | ||
@JsonProperty(value = "targetGroups", required = true) Set<RequestLogGroup> targetGroups, | ||
@JsonProperty("loggerName") @Nullable String loggerName, | ||
@JsonProperty("requestLogLevel") @Nullable LogLevel requestLogLevel, | ||
@JsonProperty("successfulResponseLogLevel") @Nullable LogLevel successfulResponseLogLevel, | ||
@JsonProperty("failureResponseLogLevel") @Nullable LogLevel failureResponseLogLevel, | ||
@JsonProperty("successSamplingRate") @Nullable Float successSamplingRate, | ||
@JsonProperty("failureSamplingRate") @Nullable Float failureSamplingRate) { | ||
this.targetGroups = requireNonNull(targetGroups, "targetGroups"); | ||
this.loggerName = firstNonNull(loggerName, "com.linecorp.centraldogma.request.log"); | ||
this.requestLogLevel = firstNonNull(requestLogLevel, LogLevel.TRACE); | ||
this.successfulResponseLogLevel = firstNonNull(successfulResponseLogLevel, LogLevel.TRACE); | ||
this.failureResponseLogLevel = firstNonNull(failureResponseLogLevel, LogLevel.WARN); | ||
this.successSamplingRate = firstNonNull(successSamplingRate, 1.0f); | ||
this.failureSamplingRate = firstNonNull(failureSamplingRate, 1.0f); | ||
} | ||
|
||
/** | ||
* Returns the target {@link RequestLogGroup}s which should be logged. | ||
*/ | ||
@JsonProperty | ||
public Set<RequestLogGroup> targetGroups() { | ||
return targetGroups; | ||
} | ||
|
||
/** | ||
* Returns the logger name to use when logging. | ||
*/ | ||
@JsonProperty | ||
public String loggerName() { | ||
return loggerName; | ||
} | ||
|
||
/** | ||
* Returns the {@link LogLevel} to use when logging requests. | ||
*/ | ||
@JsonProperty | ||
public LogLevel requestLogLevel() { | ||
return requestLogLevel; | ||
} | ||
|
||
/** | ||
* Returns the {@link LogLevel} to use when logging successful responses | ||
* (e.g., an HTTP status is less than 400). | ||
*/ | ||
@JsonProperty | ||
public LogLevel successfulResponseLogLevel() { | ||
return successfulResponseLogLevel; | ||
} | ||
|
||
/** | ||
* Returns the {@link LogLevel} to use when logging failed responses | ||
* (e.g., an HTTP status is 4xx or 5xx). | ||
*/ | ||
@JsonProperty | ||
public LogLevel failureResponseLogLevel() { | ||
return failureResponseLogLevel; | ||
} | ||
|
||
/** | ||
* Returns the rate at which to sample success requests to log. Any number between {@code 0.0} and | ||
* {@code 1.0} will cause a random sample of the requests to be logged. | ||
*/ | ||
@JsonProperty | ||
public float successSamplingRate() { | ||
return successSamplingRate; | ||
} | ||
|
||
/** | ||
* The rate at which to sample failed requests to log. Any number between {@code 0.0} and {@code 1.0} | ||
* will cause a random sample of the requests to be logged. | ||
*/ | ||
@JsonProperty | ||
public float failureSamplingRate() { | ||
return failureSamplingRate; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof RequestLogConfig)) { | ||
return false; | ||
} | ||
final RequestLogConfig that = (RequestLogConfig) o; | ||
return targetGroups.equals(that.targetGroups) && | ||
requestLogLevel == that.requestLogLevel && | ||
successfulResponseLogLevel == that.successfulResponseLogLevel && | ||
failureResponseLogLevel == that.failureResponseLogLevel && | ||
Float.compare(that.successSamplingRate, successSamplingRate) == 0 && | ||
Float.compare(that.failureSamplingRate, failureSamplingRate) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(targetGroups, requestLogLevel, successfulResponseLogLevel, failureResponseLogLevel, | ||
successSamplingRate, failureSamplingRate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("targetGroups", targetGroups) | ||
.add("requestLogLevel", requestLogLevel) | ||
.add("successfulResponseLogLevel", successfulResponseLogLevel) | ||
.add("failureResponseLogLevel", failureResponseLogLevel) | ||
.add("successSamplingRate", successSamplingRate) | ||
.add("failureSamplingRate", failureSamplingRate) | ||
.toString(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/main/java/com/linecorp/centraldogma/server/RequestLogGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.centraldogma.server; | ||
|
||
import com.linecorp.armeria.server.docs.DocService; | ||
import com.linecorp.armeria.server.healthcheck.HealthCheckService; | ||
import com.linecorp.armeria.server.metric.PrometheusExpositionService; | ||
import com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants; | ||
|
||
/** | ||
* A target service group which requests are logged. | ||
*/ | ||
public enum RequestLogGroup { | ||
/** | ||
* The services served under {@code "/api"}. | ||
*/ | ||
API, | ||
/** | ||
* The {@link PrometheusExpositionService} served at {@value HttpApiV1Constants#METRICS_PATH}. | ||
*/ | ||
METRICS, | ||
/** | ||
* The {@link HealthCheckService} served at {@value HttpApiV1Constants#HEALTH_CHECK_PATH}. | ||
*/ | ||
HEALTH, | ||
/** | ||
* The {@link DocService} served under {@value HttpApiV1Constants#DOCS_PATH}. | ||
*/ | ||
DOCS, | ||
/** | ||
* The static file services served under {@code "/web"}, {@code "/vendor"}, {@code "/scripts"} and | ||
* {@code "/styles"}. | ||
*/ | ||
WEB, | ||
/** | ||
* The group that represents all {@link RequestLogGroup}s. | ||
*/ | ||
ALL; | ||
} |
219 changes: 219 additions & 0 deletions
219
server/src/test/java/com/linecorp/centraldogma/server/RequestLogTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.centraldogma.server; | ||
|
||
import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; | ||
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
|
||
import com.linecorp.armeria.client.BlockingWebClient; | ||
import com.linecorp.armeria.client.WebClient; | ||
import com.linecorp.armeria.client.WebClientBuilder; | ||
import com.linecorp.armeria.common.HttpHeaderNames; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.common.logging.LogLevel; | ||
import com.linecorp.centraldogma.client.CentralDogma; | ||
import com.linecorp.centraldogma.internal.Jackson; | ||
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.read.ListAppender; | ||
|
||
class RequestLogTest { | ||
private static String LOGGER_NAME = "com.linecorp.centraldogma.test"; | ||
|
||
private ListAppender<ILoggingEvent> logWatcher; | ||
|
||
private void setUp() { | ||
logWatcher = new ListAppender<>(); | ||
logWatcher.start(); | ||
final Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME); | ||
logger.setLevel(Level.DEBUG); | ||
logger.addAppender(logWatcher); | ||
} | ||
|
||
@Test | ||
void jsonSerialization() throws Exception { | ||
final String json = "{\n" + | ||
" \"targetGroups\": [ \"API\", \"HEALTH\" ],\n" + | ||
" \"loggerName\": \"centraldogma.test\",\n" + | ||
" \"requestLogLevel\": \"DEBUG\",\n" + | ||
" \"successfulResponseLogLevel\": \"DEBUG\",\n" + | ||
" \"failureResponseLogLevel\": \"ERROR\",\n" + | ||
" \"successSamplingRate\": 0.4,\n" + | ||
" \"failureSamplingRate\": 0.6\n" + | ||
'}'; | ||
final RequestLogConfig requestLogConfig = Jackson.readValue(json, RequestLogConfig.class); | ||
assertThat(requestLogConfig.targetGroups()) | ||
.containsExactlyInAnyOrder(RequestLogGroup.API, RequestLogGroup.HEALTH); | ||
assertThat(requestLogConfig.loggerName()).isEqualTo("centraldogma.test"); | ||
assertThat(requestLogConfig.requestLogLevel()).isEqualTo(LogLevel.DEBUG); | ||
assertThat(requestLogConfig.successfulResponseLogLevel()).isEqualTo(LogLevel.DEBUG); | ||
assertThat(requestLogConfig.failureResponseLogLevel()).isEqualTo(LogLevel.ERROR); | ||
assertThat(requestLogConfig.successSamplingRate()).isEqualTo(0.4f); | ||
assertThat(requestLogConfig.failureSamplingRate()).isEqualTo(0.6f); | ||
assertThatJson(Jackson.writeValueAsString(requestLogConfig)) | ||
.when(IGNORING_ARRAY_ORDER) | ||
.isEqualTo(json); | ||
} | ||
|
||
@CsvSource({ "API, /api/v1/projects", "HEALTH, /monitor/l7check", "METRICS, /monitor/metrics", | ||
"DOCS, /docs/index.html", "WEB, /styles/main.css" }) | ||
@ParameterizedTest | ||
void shouldLogRequests(RequestLogGroup logGroup, String path) throws Exception { | ||
final CentralDogmaExtension dogmaExtension = newDogmaExtension(logGroup); | ||
final BlockingWebClient client = dogmaExtension.httpClient().blocking(); | ||
setUp(); | ||
assertThat(client.get(path).status()).isEqualTo(HttpStatus.OK); | ||
|
||
await().untilAsserted(() -> { | ||
assertThat(logWatcher.list) | ||
.anyMatch(event -> { | ||
return event.getLevel().equals(Level.DEBUG) && | ||
event.getMessage().contains("{} Request: {}") && | ||
event.getFormattedMessage().contains(path); | ||
}); | ||
}); | ||
dogmaExtension.stop(); | ||
} | ||
|
||
@CsvSource({ "/api/v1/projects", "/monitor/l7check", "/monitor/metrics", | ||
"/docs/index.html", "/styles/main.css" }) | ||
@ParameterizedTest | ||
void shouldLogAllRequests(String path) throws Exception { | ||
final CentralDogmaExtension dogmaExtension = newDogmaExtension(RequestLogGroup.ALL); | ||
final BlockingWebClient client = dogmaExtension.httpClient().blocking(); | ||
setUp(); | ||
assertThat(client.get(path).status()).isEqualTo(HttpStatus.OK); | ||
|
||
await().untilAsserted(() -> { | ||
assertThat(logWatcher.list) | ||
.anyMatch(event -> { | ||
return event.getLevel().equals(Level.DEBUG) && | ||
event.getMessage().contains("{} Request: {}") && | ||
event.getFormattedMessage().contains(path); | ||
}); | ||
}); | ||
dogmaExtension.stop(); | ||
} | ||
|
||
@Test | ||
void noSamplingSuccess() throws Exception { | ||
final CentralDogmaExtension dogmaExtension = newDogmaExtension(0, 1.0f, RequestLogGroup.ALL); | ||
final BlockingWebClient client = dogmaExtension.httpClient().blocking(); | ||
setUp(); | ||
assertThat(client.get("/api/v1/projects").status()).isEqualTo(HttpStatus.OK); | ||
|
||
Thread.sleep(2000); | ||
assertThat(logWatcher.list) | ||
.noneMatch(event -> { | ||
return event.getLevel().equals(Level.DEBUG) && | ||
event.getMessage().contains("{} Request: {}") && | ||
event.getFormattedMessage().contains("/api/v1/projects"); | ||
}); | ||
dogmaExtension.stop(); | ||
} | ||
|
||
@Test | ||
void noSamplingFailure() throws Exception { | ||
final CentralDogmaExtension dogmaExtension = newDogmaExtension(1.0f, 0, RequestLogGroup.ALL); | ||
final BlockingWebClient unauthorizedClient = WebClient.of(dogmaExtension.httpClient().uri()).blocking(); | ||
setUp(); | ||
assertThat(unauthorizedClient.get("/api/v1/projects").status()).isEqualTo(HttpStatus.UNAUTHORIZED); | ||
|
||
Thread.sleep(2000); | ||
assertThat(logWatcher.list) | ||
.noneMatch(event -> { | ||
return event.getLevel().equals(Level.DEBUG) && | ||
event.getMessage().contains("{} Request: {}") && | ||
event.getFormattedMessage().contains("/api/v1/projects"); | ||
}); | ||
dogmaExtension.stop(); | ||
} | ||
|
||
@CsvSource({ "API, /api/v1/projects", "HEALTH, /monitor/l7check", "METRICS, /monitor/metrics", | ||
"DOCS, /docs/index.html", "WEB, /styles/main.css" }) | ||
@ParameterizedTest | ||
void shouldNotLogTargetRequests(RequestLogGroup logGroup, String path) throws Exception { | ||
final RequestLogGroup[] logGroups = | ||
Arrays.stream(RequestLogGroup.values()) | ||
.filter(group -> group != RequestLogGroup.ALL && group != logGroup) | ||
.toArray(RequestLogGroup[]::new); | ||
final CentralDogmaExtension dogmaExtension = newDogmaExtension(logGroups); | ||
final BlockingWebClient client = dogmaExtension.httpClient().blocking(); | ||
setUp(); | ||
|
||
assertThat(client.get(path).status()).isEqualTo(HttpStatus.OK); | ||
Thread.sleep(2000); | ||
assertThat(logWatcher.list) | ||
.noneMatch(event -> { | ||
return event.getLevel().equals(Level.DEBUG) && | ||
event.getMessage().contains("{} Request: {}") && | ||
event.getFormattedMessage().contains(path); | ||
}); | ||
dogmaExtension.stop(); | ||
} | ||
|
||
private CentralDogmaExtension newDogmaExtension(RequestLogGroup... logGroup) throws Exception { | ||
return newDogmaExtension(1.0f, 1.0f, logGroup); | ||
} | ||
|
||
private CentralDogmaExtension newDogmaExtension(float successSamplingRate, float failureSamplingRate, | ||
RequestLogGroup... logGroup) throws Exception { | ||
final CentralDogmaExtension dogmaExtension = new CentralDogmaExtension() { | ||
@Override | ||
protected void configure(CentralDogmaBuilder builder) { | ||
builder.webAppEnabled(true); | ||
final RequestLogConfig requestLogConfig = | ||
new RequestLogConfig(ImmutableSet.copyOf(logGroup), | ||
LOGGER_NAME, | ||
LogLevel.DEBUG, | ||
LogLevel.INFO, | ||
LogLevel.ERROR, | ||
successSamplingRate, | ||
failureSamplingRate); | ||
builder.requestLogConfig(requestLogConfig); | ||
} | ||
|
||
@Override | ||
protected void configureHttpClient(WebClientBuilder builder) { | ||
builder.addHeader(HttpHeaderNames.AUTHORIZATION, "Bearer anonymous"); | ||
} | ||
|
||
@Override | ||
protected void scaffold(CentralDogma client) { | ||
client.createProject("foo").join(); | ||
} | ||
}; | ||
dogmaExtension.start(); | ||
dogmaExtension.before(null); | ||
setUp(); | ||
return dogmaExtension; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Question: Is it possible that users may want to configure different logging parameters for different paths?
e.g.
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.
Oh... It is so nice! 👍
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.
We can implement it by adding different
LoggingService
s.