Skip to content

Commit

Permalink
feat: add logging with slf4j
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Mar 25, 2024
1 parent 4106c9f commit 97e9862
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.12</version>
</dependency>

<!-- runtime scope-->
<!-- transitive deps to shaded libs brought with runtime to be safe -->
Expand All @@ -163,12 +168,6 @@
<version>0.26.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
Expand All @@ -189,6 +188,12 @@
</dependency>

<!-- test scope -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
<version>1.4.14</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.regex.Pattern;
import org.slf4j.Logger;

/** OpenFeature Provider for feature flagging with the Confidence platform */
public class ConfidenceFeatureProvider implements FeatureProvider {
Expand All @@ -38,6 +39,9 @@ public ConfidenceFeatureProvider(Confidence confidence) {
this.confidence = confidence;
}

private static final Logger log =
org.slf4j.LoggerFactory.getLogger(ConfidenceFeatureProvider.class);

/**
* ConfidenceFeatureProvider constructor
*
Expand Down Expand Up @@ -122,6 +126,7 @@ private <T> ProviderEvaluation<T> getCastedEvaluation(

final T castedValue = cast.apply(objectEvaluation.getValue());
if (castedValue == null) {
log.warn("Cannot cast value '{}' to expected type", objectEvaluation.getValue().toString());
throw new TypeMismatchError(
String.format("Cannot cast value '%s' to expected type", objectEvaluation.getValue()));
}
Expand Down Expand Up @@ -157,12 +162,14 @@ public ProviderEvaluation<Value> getObjectEvaluation(
.get();

if (resolveFlagResponse.getResolvedFlagsList().isEmpty()) {
log.warn("No active flag '{}' was found", flagPath.getFlag());
throw new FlagNotFoundError(
String.format("No active flag '%s' was found", flagPath.getFlag()));
}

final String responseFlagName = resolveFlagResponse.getResolvedFlags(0).getFlag();
if (!requestFlagName.equals(responseFlagName)) {
log.warn("Unexpected flag '{}' from remote", responseFlagName.replaceFirst("^flags/", ""));
throw new FlagNotFoundError(
String.format(
"Unexpected flag '%s' from remote", responseFlagName.replaceFirst("^flags/", "")));
Expand All @@ -171,6 +178,9 @@ public ProviderEvaluation<Value> getObjectEvaluation(
final ResolvedFlag resolvedFlag = resolveFlagResponse.getResolvedFlags(0);

if (resolvedFlag.getVariant().isEmpty()) {
log.info(
"The server returned no assignment for the flag. Typically, this happens "
+ "if no configured rules matches the given evaluation context.");
return ProviderEvaluation.<Value>builder()
.value(defaultValue)
.reason(
Expand Down Expand Up @@ -209,12 +219,18 @@ public ProviderEvaluation<Value> getObjectEvaluation(

private static void handleStatusRuntimeException(StatusRuntimeException e) {
if (e.getStatus().getCode() == Code.DEADLINE_EXCEEDED) {
log.error("Deadline exceeded when calling provider backend");
throw new GeneralError("Deadline exceeded when calling provider backend");
} else if (e.getStatus().getCode() == Code.UNAVAILABLE) {
log.error("Provider backend is unavailable");
throw new GeneralError("Provider backend is unavailable");
} else if (e.getStatus().getCode() == Code.UNAUTHENTICATED) {
log.error("UNAUTHENTICATED");
throw new GeneralError("UNAUTHENTICATED");
} else {
log.error(
"Unknown error occurred when calling the provider backend. Grpc status code {}",
e.getStatus().getCode());
throw new GeneralError(
String.format(
"Unknown error occurred when calling the provider backend. Exception: %s",
Expand All @@ -228,6 +244,8 @@ private static Value getValueForPath(List<String> path, Value fullValue) {
final Structure structure = value.asStructure();
if (structure == null) {
// value's inner object actually is no structure
log.warn(
"Illegal attempt to derive field '{}' on non-structure value '{}'", fieldName, value);
throw new TypeMismatchError(
String.format(
"Illegal attempt to derive field '%s' on non-structure value '%s'",
Expand All @@ -239,6 +257,10 @@ private static Value getValueForPath(List<String> path, Value fullValue) {
if (value == null) {
// we know that null indicates absence of a proper value because intended nulls would be an
// instance of type Value
log.warn(
"Illegal attempt to derive non-existing field '{}' on structure value '{}'",
fieldName,
structure);
throw new TypeMismatchError(
String.format(
"Illegal attempt to derive non-existing field '%s' on structure value '%s'",
Expand All @@ -255,6 +277,7 @@ private static FlagPath getPath(String str) {

if (parts.length == 0) {
// this happens for malformed corner cases such as: str = "..."
log.warn("Illegal path string '{}'", str);
throw new GeneralError(String.format("Illegal path string '%s'", str));
} else if (parts.length == 1) {
// str doesn't contain the delimiter
Expand Down

0 comments on commit 97e9862

Please sign in to comment.