Skip to content
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

feat: Allow global otel configuration extraction #505

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 14 additions & 5 deletions providers/flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ By default, the provider is configured to
use [least recently used (lru)](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/LRUMap.html)
caching with up to 1000 entries.

### OpenTelemetry support (RPC only)
### OpenTelemetry tracing (RPC only)

OpenTelemetry support can be enabled either through [automatic instrumentation](https://opentelemetry.io/docs/instrumentation/java/automatic/)
or with [manual instrumentation](https://opentelemetry.io/docs/instrumentation/java/manual/).
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -162,8 +162,17 @@ FlagdOptions options =
FlagdProvider flagdProvider = new FlagdProvider(options);
```

Please refer [OpenTelemetry example](https://opentelemetry.io/docs/instrumentation/java/manual/#example) for best
practice guideline.
Alternatively, if you already have set up `GlobalOpenTelemetry` in your provider runtime, then allow flagd to derive tracer using `withGlobalTelemetry` option.

Telemetry configuration combined with [flagd telemetry ](https://github.com/open-feature/flagd/blob/main/docs/configuration/flagd_telemetry.md)
allows distributed tracing.
```java
FlagdOptions options =
FlagdOptions.builder()
.withGlobalTelemetry(true)
.build();

FlagdProvider flagdProvider = new FlagdProvider(options);
```

Please refer [OpenTelemetry example](https://opentelemetry.io/docs/instrumentation/java/manual/#example) for best practice guideline.

Provider telemetry combined with [flagd OpenTelemetry](https://flagd.dev/reference/monitoring/#opentelemetry) allows you to have distributed traces.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.openfeature.contrib.providers.flagd;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -151,5 +152,15 @@ public FlagdOptionsBuilder offlineFlagSourcePath(@Nonnull final String offlineFl
private FlagdOptionsBuilder isOffline(final boolean isOffline) {
return this;
}

/**
* Enable OpenTelemetry instance extraction from GlobalOpenTelemetry. Note that, this is only useful if global
* configurations are registered.
*/
public FlagdOptionsBuilder withGlobalTelemetry(final boolean b) {
this.openTelemetry = GlobalOpenTelemetry.get();

return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
public final class ResolveFactory {
/**
* Factory method to initialize the resolving strategy.
*
* @param options Options.
* @return the ResolveStrategy based on the provided options.
*/
public static ResolveStrategy getStrategy(FlagdOptions options) {
if (options.getOpenTelemetry() != null) {
return new TracedResolving(options.getOpenTelemetry());
}

return new SimpleResolving();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.openfeature.contrib.providers.flagd.resolver.grpc.strategy;

import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import io.opentelemetry.api.OpenTelemetry;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ResolveFactoryTest {

@Test
public void simpleResolverAsDefault() {
// given
final FlagdOptions options = FlagdOptions.builder().build();

// when
final ResolveStrategy strategy = ResolveFactory.getStrategy(options);

// then
assertEquals(SimpleResolving.class, strategy.getClass());
}


@Test
public void tracedResolverWhenOTelSdkIsSet() {
// given
final OpenTelemetry telemetry = Mockito.mock(OpenTelemetry.class);

final FlagdOptions options = FlagdOptions.builder().openTelemetry(telemetry).build();

// when
final ResolveStrategy strategy = ResolveFactory.getStrategy(options);

// then
assertEquals(TracedResolving.class, strategy.getClass());
}

@Test
public void tracedResolverWhenGlobalTelemetryIsSet() {
// given
final FlagdOptions options = FlagdOptions.builder().withGlobalTelemetry(true).build();

// when
final ResolveStrategy strategy = ResolveFactory.getStrategy(options);

// then
assertEquals(TracedResolving.class, strategy.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.openfeature.contrib.providers.flagd.resolver.grpc.strategy;


import com.google.protobuf.Message;
import dev.openfeature.contrib.providers.flagd.resolver.grpc.strategy.TracedResolving;
import dev.openfeature.flagd.grpc.Schema;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
Expand All @@ -20,7 +18,7 @@
class TracedResolvingTest {

@Test
public void basicTest(){
public void basicTest() {
// given
final String key = "flagA";
final OpenTelemetry openTelemetry = mock(OpenTelemetry.class);
Expand Down