Skip to content

Allow configuring log level #26

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

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ openapi.validation.specification-file-path=/tmp/openapi-spec/openapi.json
# If it is within src/main/resources/folder/my-spec.json use
openapi.validation.specification-file-path=folder/my-spec.json

# Custom log level for violations (ignore, info, warn, error)
# Default: info
openapi.validation.violation-log-level=error

# Comma separated list of paths to be excluded from validation. Default is no excluded paths
openapi.validation.excluded-paths=/_readiness,/_liveness,/_metrics
# Allows to exclude requests based on headers. Default is no excluded headers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
openapi.validation.sample-rate=1
openapi.validation.specification-file-path=openapi.yaml
openapi.validation.violation-log-level=
openapi.validation.excluded-paths=/_readiness,/_liveness,/_metrics
openapi.validation.validation-report-throttle-wait-seconds=10
openapi.validation.validation-report-metric-name=openapi_error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.getyourguide.openapi.validation.api.metrics;

import com.getyourguide.openapi.validation.api.log.LogLevel;
import com.getyourguide.openapi.validation.api.metrics.client.MetricsClient;
import com.getyourguide.openapi.validation.api.model.OpenApiViolation;
import java.util.ArrayList;
Expand All @@ -16,6 +17,10 @@ public class DefaultMetricsReporter implements MetricsReporter {

@Override
public void reportViolation(OpenApiViolation violation) {
if (violation.getLevel() == LogLevel.IGNORE) {
return;
}

metricsClient.increment(buildMetricName(".error"), createTagsForViolation(violation));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public void log(OpenApiViolation violation) {
case INFO -> log.info(violation.getLogMessage());
case WARN -> log.warn(violation.getLogMessage());
case ERROR -> log.error(violation.getLogMessage());
// keeping ignored as debug for now
case IGNORE -> log.debug(violation.getLogMessage());
default -> { /* do nothing */ }
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ private LevelResolver buildLevelResolver(
Map<String, LogLevel> levelResolverLevels,
LogLevel levelResolverDefaultLevel
) {
var builder = LevelResolver.create();
var builder = LevelResolver.create()
// This is needed if your spec uses composition via allOf, anyOf or oneOf.
.withLevel("validation.schema.additionalProperties", ValidationReport.Level.IGNORE);
if (levelResolverLevels != null && !levelResolverLevels.isEmpty()) {
builder.withLevels(
levelResolverLevels.entrySet().stream()
Expand All @@ -154,10 +156,10 @@ private LevelResolver buildLevelResolver(
)
);
}
return builder
// this will cause all messages to be warn by default
.withDefaultLevel(mapLevel(levelResolverDefaultLevel).orElse(ValidationReport.Level.INFO))
.build();

mapLevel(levelResolverDefaultLevel).ifPresent(builder::withDefaultLevel);

return builder.build();
}

private Optional<ValidationReport.Level> mapLevel(LogLevel level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.getyourguide.openapi.validation.OpenApiValidationApplicationProperties.PROPERTY_PREFIX;

import com.getyourguide.openapi.validation.api.exclusions.ExcludedHeader;
import com.getyourguide.openapi.validation.api.log.LogLevel;
import com.getyourguide.openapi.validation.api.metrics.MetricTag;
import com.getyourguide.openapi.validation.util.CommaSeparatedStringsUtil;
import java.util.Arrays;
Expand All @@ -27,6 +28,7 @@ public class OpenApiValidationApplicationProperties {

private Double sampleRate;
private String specificationFilePath;
private LogLevel violationLogLevel;
private Integer validationReportThrottleWaitSeconds;
private String validationReportMetricName;
private String validationReportMetricAdditionalTags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public ValidationReportHandler validationReportHandler(
public ValidatorConfiguration validatorConfiguration() {
return new ValidatorConfigurationBuilder()
// .levelResolverLevel("validation.request.body.schema.additionalProperties", LogLevel.IGNORE)
.levelResolverDefaultLevel(LogLevel.INFO)
.levelResolverDefaultLevel(
properties.getViolationLogLevel() != null ? properties.getViolationLogLevel() : LogLevel.INFO
)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"type": "java.lang.String",
"description": "Path to the OpenAPI specification file. If set, the system will search for this file within resources and also as a path based on the current (working) directory. The property defaults to null. If null, the program searches for either an 'openapi.yaml/json' or a 'spec.yaml/json' file under resources."
},
{
"name": "openapi.validation.violation-log-level",
"type": "com.getyourguide.openapi.validation.api.log.LogLevel",
"description": "Log level to be used for all violations. Default: info\nNote: This has no effect if a custom ValidatorConfiguration is provided, then it needs to be configured there."
},
{
"name": "openapi.validation.excluded-paths",
"type": "java.lang.String",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.getyourguide.openapi.validation.api.exclusions.ExcludedHeader;
import com.getyourguide.openapi.validation.api.log.LogLevel;
import com.getyourguide.openapi.validation.api.metrics.MetricTag;
import java.util.List;
import java.util.Set;
Expand All @@ -14,6 +15,7 @@ class OpenApiValidationApplicationPropertiesTest {

private static final Double SAMPLE_RATE = 0.001;
private static final String SPECIFICATION_FILE_PATH = "/tmp/openapi.yaml";
private static final LogLevel LOG_LEVEL = LogLevel.ERROR;
private static final Integer VALIDATION_REPORT_THROTTLE_WAIT_SECONDS = 10;
private static final String VALIDATION_REPORT_METRIC_NAME = "openapi_validation";
private static final String VALIDATION_REPORT_METRIC_ADDITONAL_TAGS_STRING = "service=payment,team=chk";
Expand All @@ -25,6 +27,7 @@ void getters() {
var loggingConfiguration = new OpenApiValidationApplicationProperties(
SAMPLE_RATE,
SPECIFICATION_FILE_PATH,
LOG_LEVEL,
VALIDATION_REPORT_THROTTLE_WAIT_SECONDS,
VALIDATION_REPORT_METRIC_NAME,
VALIDATION_REPORT_METRIC_ADDITONAL_TAGS_STRING,
Expand All @@ -36,6 +39,7 @@ void getters() {

assertEquals(SAMPLE_RATE, loggingConfiguration.getSampleRate());
assertEquals(SPECIFICATION_FILE_PATH, loggingConfiguration.getSpecificationFilePath());
assertEquals(LOG_LEVEL, loggingConfiguration.getViolationLogLevel());
assertEquals(VALIDATION_REPORT_THROTTLE_WAIT_SECONDS,
loggingConfiguration.getValidationReportThrottleWaitSeconds());
assertEquals(VALIDATION_REPORT_METRIC_NAME, loggingConfiguration.getValidationReportMetricName());
Expand Down