use of com.palantir.logsafe.Arg in project atlasdb by palantir.
the class CheckAndSetQueriesTest method valuesCreatedAtCorrectLogSafetyLevelsForUpdates.
@Test
public void valuesCreatedAtCorrectLogSafetyLevelsForUpdates() {
CqlQuery query = CheckAndSetQueries.getQueryForRequest(UPDATE_REQUEST);
AtomicReference<Object[]> objects = new AtomicReference<>();
query.logSlowResult((format, args) -> objects.set(args), Stopwatch.createStarted());
Object[] loggedObjects = objects.get();
Map<String, Boolean> argumentSafety = new HashMap<>();
Arrays.stream(loggedObjects).forEach(object -> {
Arg<?> arg = (Arg<?>) object;
argumentSafety.put(arg.getName(), arg.isSafeForLogging());
});
assertThat(argumentSafety).containsEntry("row", false).containsEntry("column", false).containsEntry("cassandraTimestamp", true).containsEntry("oldValue", false).containsEntry("newValue", false).containsEntry("unsafeTableRef", false).doesNotContainKey(// the table wasn't marked as safe
"tableRef");
assertThat(query.toString()).isEqualTo("UPDATE \"ns__table\" SET value=0x626262" + " WHERE key=0x616263 AND column1=0x313233 AND column2=-1 IF value=0x616161;");
}
use of com.palantir.logsafe.Arg in project atlasdb by palantir.
the class CheckAndSetQueriesTest method valuesCreatedAtCorrectLogSafetyLevelsForNewCells.
@Test
public void valuesCreatedAtCorrectLogSafetyLevelsForNewCells() {
CqlQuery query = CheckAndSetQueries.getQueryForRequest(NEW_CELL_REQUEST);
AtomicReference<Object[]> objects = new AtomicReference<>();
query.logSlowResult((format, args) -> objects.set(args), Stopwatch.createStarted());
Object[] loggedObjects = objects.get();
Map<String, Boolean> argumentSafety = new HashMap<>();
Arrays.stream(loggedObjects).forEach(object -> {
Arg<?> arg = (Arg<?>) object;
argumentSafety.put(arg.getName(), arg.isSafeForLogging());
});
assertThat(argumentSafety).containsEntry("row", false).containsEntry("column", false).containsEntry("cassandraTimestamp", true).containsEntry("newValue", false).containsEntry("unsafeTableRef", false).doesNotContainKey(// the table wasn't marked as safe
"tableRef");
assertThat(query.toString()).isEqualTo("INSERT INTO \"ns__table\" (key, column1, column2, value)" + " VALUES (0x616263, 0x313233, -1, 0x70747074) IF NOT EXISTS;");
}
use of com.palantir.logsafe.Arg in project dialogue by palantir.
the class ApacheHttpClientChannelsTest method close_doesnt_fail_inflight_requests.
@Test
public void close_doesnt_fail_inflight_requests() throws Exception {
ClientConfiguration conf = TestConfigurations.create("http://foo");
Channel channel;
try (ApacheHttpClientChannels.CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(conf, "client")) {
channel = ApacheHttpClientChannels.createSingleUri("http://foo", client);
ListenableFuture<Response> response = channel.execute(TestEndpoint.POST, Request.builder().build());
assertThatThrownBy(() -> Futures.getUnchecked(response)).getCause().isInstanceOfSatisfying(UnknownHostException.class, throwable -> assertThat(throwable.getSuppressed()[0]).satisfies(diagnosticThrowable -> assertThat(diagnosticThrowable.getStackTrace()).as("Diagnostic exception should have an empty stack trace").isEmpty()).isInstanceOfSatisfying(SafeLoggable.class, safeLoggable -> {
assertThat(Lists.transform(safeLoggable.getArgs(), Arg::getName)).as("Expected a diagnostic exception").containsExactlyInAnyOrder("durationMillis", "connectTimeout", "socketTimeout", "clientName", "serviceName", "endpointName", "requestTraceId", "requestSpanId", "hostIndex");
}));
}
ListenableFuture<Response> again = channel.execute(TestEndpoint.POST, Request.builder().build());
assertThatThrownBy(() -> Futures.getUnchecked(again)).hasCauseInstanceOf(UnknownHostException.class);
}
use of com.palantir.logsafe.Arg in project safe-logging by palantir.
the class LoggerGenerator method generateLoggerImplementation.
private static JavaFile generateLoggerImplementation() {
TypeSpec.Builder loggerBuilder = TypeSpec.classBuilder(LOGGER_NAME).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addJavadoc("Safe-logging logger API used to produce log events with safe-classified {@link Arg} values.").addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", LoggerGenerator.class.getName()).build()).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "TooManyArguments").build()).addField(FieldSpec.builder(BRIDGE_NAME, DELEGATE).addModifiers(Modifier.PRIVATE, Modifier.FINAL).build()).addMethod(MethodSpec.constructorBuilder().addJavadoc("Internal package-private constructor for {@link SafeLoggerFactory}.").addParameter(ParameterSpec.builder(BRIDGE_NAME, DELEGATE).build()).addStatement("this.$1N = $2T.requireNonNull($1N, $3S)", DELEGATE, Objects.class, "SafeLoggerBridge is required").build());
for (MethodSpec spec : createLoggerBridge().methodSpecs) {
MethodSpec.Builder methodBuilder = spec.toBuilder();
methodBuilder.modifiers.clear();
boolean isVoidMethod = ClassName.VOID.equals(spec.returnType);
CodeBlock args = spec.parameters.stream().map(param -> CodeBlock.of("$N", param.name)).collect(CodeBlock.joining(", "));
methodBuilder.addModifiers(Modifier.PUBLIC).addStatement("$L$N.$N($L)", isVoidMethod ? "" : "return ", DELEGATE, spec.name, args);
loggerBuilder.addMethod(methodBuilder.build());
}
return JavaFile.builder(LOGGER_NAME.packageName(), loggerBuilder.build()).skipJavaLangImports(true).addFileComment(copyright(2021)).build();
}
Aggregations