Skip to content

Commit

Permalink
jul to slf4j
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan committed Sep 25, 2023
1 parent 2b8f978 commit 3679f70
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package dev.openfeature.contrib.providers.flagd.resolver.process;

import static dev.openfeature.contrib.providers.flagd.resolver.process.model.FeatureFlag.EMPTY_TARGETING_STRING;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.Level;

import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import dev.openfeature.contrib.providers.flagd.resolver.Resolver;
import dev.openfeature.contrib.providers.flagd.resolver.common.Util;
Expand All @@ -24,13 +18,18 @@
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
import dev.openfeature.sdk.exceptions.ParseError;
import dev.openfeature.sdk.exceptions.TypeMismatchError;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import static dev.openfeature.contrib.providers.flagd.resolver.process.model.FeatureFlag.EMPTY_TARGETING_STRING;

/**
* flagd in-process resolver. Resolves feature flags in-process. Flags are retrieved from {@link Storage}, where the
* {@link Storage} maintain flag configurations obtained from known source.
*/
@Log
@Slf4j
public class InProcessResolver implements Resolver {
private final Storage flagStore;
private final Consumer<ProviderState> stateConsumer;
Expand Down Expand Up @@ -70,11 +69,11 @@ public void init() throws Exception {
case STALE:
// todo set stale state
default:
log.log(Level.INFO, String.format("Storage emitted unhandled status: %s", storageState));
log.info(String.format("Storage emitted unhandled status: %s", storageState));
}
}
} catch (InterruptedException e) {
log.log(Level.WARNING, "Storage state watcher interrupted", e);
log.warn("Storage state watcher interrupted", e);
}
});
stateWatcher.setDaemon(true);
Expand All @@ -86,8 +85,9 @@ public void init() throws Exception {

/**
* Shutdown in-process resolver.
*
* @throws InterruptedException if stream can't be closed within deadline.
*/
*/
public void shutdown() throws InterruptedException {
flagStore.shutdown();
this.connected.set(false);
Expand Down Expand Up @@ -174,7 +174,7 @@ private <T> ProviderEvaluation<T> resolve(Class<T> type, String key,
}
} catch (TargetingRuleException e) {
String message = String.format("error evaluating targeting rule for flag %s", key);
log.log(Level.FINE, message, e);
log.debug(message, e);
throw new ParseError(message);
}
}
Expand All @@ -183,13 +183,13 @@ private <T> ProviderEvaluation<T> resolve(Class<T> type, String key,
Object value = flag.getVariants().get(resolvedVariant);
if (value == null) {
String message = String.format("variant %s not found in flag with key %s", resolvedVariant, key);
log.log(Level.FINE, message);
log.debug(message);
throw new TypeMismatchError(message);
}

if (!type.isAssignableFrom(value.getClass()) || !(resolvedVariant instanceof String)) {
String message = "returning default variant for flagKey: %s, type not valid";
log.log(Level.FINE, String.format(message, key));
log.debug(String.format(message, key));
throw new TypeMismatchError(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.networknt.schema.ValidationMessage;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -23,7 +24,7 @@
/**
* flagd feature flag configuration parser.
*/
@Log
@Slf4j
@SuppressFBWarnings(value = {"EI_EXPOSE_REP"},
justification = "Feature flag comes as a Json configuration, hence they must be exposed")
public class FlagParser {
Expand All @@ -42,7 +43,7 @@ public class FlagParser {
static {
try (InputStream schema = FlagParser.class.getClassLoader().getResourceAsStream(SCHEMA_RESOURCE)) {
if (schema == null) {
log.log(Level.WARNING, String.format("Resource %s not found", SCHEMA_RESOURCE));
log.warn(String.format("Resource %s not found", SCHEMA_RESOURCE));
} else {
final ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
Expand All @@ -55,8 +56,7 @@ public class FlagParser {
}
} catch (Throwable e) {
// log only, do not throw
log.log(Level.WARNING,
String.format("Error loading resource %s, schema validation will be skipped", SCHEMA_RESOURCE), e);
log.warn(String.format("Error loading resource %s, schema validation will be skipped", SCHEMA_RESOURCE), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.Connector;
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.StreamPayload;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -15,12 +15,11 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import java.util.logging.Level;

/**
* Feature flag storage.
*/
@Log
@Slf4j
@SuppressFBWarnings(value = {"EI_EXPOSE_REP"},
justification = "Feature flag comes as a Json configuration, hence they must be exposed")
public class FlagStore implements Storage {
Expand All @@ -47,7 +46,7 @@ public void init() {
try {
streamerListener(connector);
} catch (InterruptedException e) {
log.log(Level.WARNING, "connection listener failed", e);
log.warn("connection listener failed", e);
}
});
streamer.setDaemon(true);
Expand All @@ -56,6 +55,7 @@ public void init() {

/**
* Shutdown storage layer.
*
* @throws InterruptedException if stream can't be closed within deadline.
*/
public void shutdown() throws InterruptedException {
Expand Down Expand Up @@ -102,27 +102,27 @@ private void streamerListener(final Connector connector) throws InterruptedExcep
writeLock.unlock();
}
if (!stateBlockingQueue.offer(StorageState.OK)) {
log.log(Level.WARNING, "Failed to convey OK satus, queue is full");
log.warn("Failed to convey OK satus, queue is full");
}
} catch (Throwable e) {
// catch all exceptions and avoid stream listener interruptions
log.log(Level.WARNING, "Invalid flag sync payload from connector", e);
log.warn("Invalid flag sync payload from connector", e);
if (!stateBlockingQueue.offer(StorageState.STALE)) {
log.log(Level.WARNING, "Failed to convey STALE satus, queue is full");
log.warn("Failed to convey STALE satus, queue is full");
}
}
break;
case ERROR:
if (!stateBlockingQueue.offer(StorageState.ERROR)) {
log.log(Level.WARNING, "Failed to convey ERROR satus, queue is full");
log.warn("Failed to convey ERROR satus, queue is full");
}
break;
default:
log.log(Level.INFO, String.format("Payload with unknown type: %s", take.getType()));
log.info(String.format("Payload with unknown type: %s", take.getType()));
}
}

log.log(Level.INFO, "Shutting down store stream listener");
log.info("Shutting down store stream listener");
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.grpc;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import dev.openfeature.contrib.providers.flagd.resolver.common.ChannelBuilder;
import dev.openfeature.contrib.providers.flagd.resolver.process.storage.connector.Connector;
Expand All @@ -16,12 +9,18 @@
import dev.openfeature.flagd.sync.SyncService;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.grpc.ManagedChannel;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Implements the {@link Connector} contract and emit flags obtained from flagd sync gRPC contract.
*/
@Log
@Slf4j
@SuppressFBWarnings(value = {"PREDICTABLE_RANDOM", "EI_EXPOSE_REP"},
justification = "Random is used to generate a variation & flag configurations require exposing")
public class GrpcStreamConnector implements Connector {
Expand All @@ -41,7 +40,7 @@ public class GrpcStreamConnector implements Connector {

/**
* Construct a new GrpcStreamConnector.
*
*
* @param options flagd options
*/
public GrpcStreamConnector(final FlagdOptions options) {
Expand All @@ -58,7 +57,7 @@ public void init() {
try {
observeEventStream(blockingQueue, shutdown, serviceStub);
} catch (InterruptedException e) {
log.log(Level.WARNING, "gRPC event stream interrupted, flag configurations are stale", e);
log.warn("gRPC event stream interrupted, flag configurations are stale", e);
}
});

Expand All @@ -75,6 +74,7 @@ public BlockingQueue<StreamPayload> getStream() {

/**
* Shutdown gRPC stream connector.
*
* @throws InterruptedException if stream can't be closed within deadline.
*/
public void shutdown() throws InterruptedException {
Expand All @@ -91,7 +91,7 @@ public void shutdown() throws InterruptedException {
if (this.channel != null && !this.channel.isShutdown()) {
this.channel.shutdownNow();
this.channel.awaitTermination(this.deadline, TimeUnit.MILLISECONDS);
log.warning(String.format("Unable to shut down channel by %d deadline", this.deadline));
log.warn(String.format("Unable to shut down channel by %d deadline", this.deadline));
}
}
}
Expand Down Expand Up @@ -120,13 +120,12 @@ static void observeEventStream(final BlockingQueue<StreamPayload> writeTo,
}

if (response.getError() != null) {
log.log(Level.WARNING,
String.format("Error from grpc connection, retrying in %dms", retryDelay),
log.warn(String.format("Error from grpc connection, retrying in %dms", retryDelay),
response.getError());

if (!writeTo.offer(
new StreamPayload(StreamPayloadType.ERROR, "Error from stream connection, retrying"))) {
log.log(Level.WARNING, "Failed to convey ERROR satus, queue is full");
log.warn("Failed to convey ERROR satus, queue is full");
}
break;
}
Expand All @@ -136,7 +135,7 @@ static void observeEventStream(final BlockingQueue<StreamPayload> writeTo,
case SYNC_STATE_ALL:
if (!writeTo.offer(
new StreamPayload(StreamPayloadType.DATA, flagsResponse.getFlagConfiguration()))) {
log.log(Level.WARNING, "Stream writing failed");
log.warn("Stream writing failed");
}
break;
case SYNC_STATE_UNSPECIFIED:
Expand All @@ -156,7 +155,7 @@ static void observeEventStream(final BlockingQueue<StreamPayload> writeTo,

// check for shutdown and avoid sleep
if (shutdown.get()) {
log.log(Level.INFO, "Shutdown invoked, exiting event stream listener");
log.info("Shutdown invoked, exiting event stream listener");
return;
}

Expand All @@ -169,6 +168,6 @@ static void observeEventStream(final BlockingQueue<StreamPayload> writeTo,
}

// log as this can happen after awakened from backoff sleep
log.log(Level.INFO, "Shutdown invoked, exiting event stream listener");
log.info("Shutdown invoked, exiting event stream listener");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import dev.openfeature.flagd.sync.SyncService;
import io.grpc.stub.StreamObserver;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;

@Log
@Slf4j
class GrpcStreamHandler implements StreamObserver<SyncService.SyncFlagsResponse> {
private final BlockingQueue<GrpcResponseModel> blockingQueue;

Expand All @@ -18,21 +17,21 @@ class GrpcStreamHandler implements StreamObserver<SyncService.SyncFlagsResponse>
@Override
public void onNext(SyncService.SyncFlagsResponse syncFlagsResponse) {
if (!blockingQueue.offer(new GrpcResponseModel(syncFlagsResponse))) {
log.log(Level.WARNING, "failed to write sync response to queue");
log.warn("failed to write sync response to queue");
}
}

@Override
public void onError(Throwable throwable) {
if (!blockingQueue.offer(new GrpcResponseModel(throwable))) {
log.log(Level.WARNING, "failed to write error response to queue");
log.warn("failed to write error response to queue");
}
}

@Override
public void onCompleted() {
if (!blockingQueue.offer(new GrpcResponseModel(true))) {
log.log(Level.WARNING, "failed to write complete status to queue");
log.warn("failed to write complete status to queue");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluationException;
import io.github.jamsesso.jsonlogic.evaluator.expressions.PreEvaluatedArgumentsExpression;
import lombok.Getter;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.MurmurHash3;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;

@Log
@Slf4j
class Fractional implements PreEvaluatedArgumentsExpression {

public String key() {
Expand Down Expand Up @@ -42,7 +41,7 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
} else {
// fallback to targeting key if present
if (properties.getTargetingKey() == null) {
log.log(Level.FINE, "Missing fallback targeting key");
log.debug("Missing fallback targeting key");
return null;
}

Expand All @@ -61,12 +60,12 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
distribution += fractionProperty.getPercentage();
}
} catch (JsonLogicException e) {
log.log(Level.FINE, "Error parsing fractional targeting rule", e);
log.debug("Error parsing fractional targeting rule", e);
return null;
}

if (distribution != 100) {
log.log(Level.FINE, "Fractional properties do not sum to 100");
log.debug("Fractional properties do not sum to 100");
return null;
}

Expand Down
Loading

0 comments on commit 3679f70

Please sign in to comment.