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 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
35 changes: 27 additions & 8 deletions providers/flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,21 @@ 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/).
flagd provider support OpenTelemetry traces for gRPC backed remote evaluations.

For manual instrumentation, flagd provider can be constructed with an `OpenTelemetry` instance.
There are two ways you can configure OpenTelemetry for the provider,

- [Using automatic instrumentation](https://opentelemetry.io/docs/instrumentation/java/automatic/)
- [Using manual instrumentation](https://opentelemetry.io/docs/instrumentation/java/manual/)

When using automatic instrumentation, traces for gRPC will be automatically added by the OpenTelemetry Java library.
These traces however will not include extra attributes added when using manual instrumentation.

When using manual instrumentation, you have two options to construct flagd provider to enable traces.

First(preferred) option is by constructing the provider with an OpenTelemetry instance,

```java
FlagdOptions options =
Expand All @@ -162,8 +171,18 @@ FlagdOptions options =
FlagdProvider flagdProvider = new FlagdProvider(options);
```

Please refer [OpenTelemetry example](https://opentelemetry.io/docs/instrumentation/java/manual/#example) for best
practice guideline.
Second option is useful if you have set up a GlobalOpenTelemetry in your runtime.
You can allow flagd to derive the OpenTelemetry instance by enabling `withGlobalTelemetry` option.

```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.

Telemetry configuration combined with [flagd telemetry ](https://github.com/open-feature/flagd/blob/main/docs/configuration/flagd_telemetry.md)
allows distributed tracing.
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