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

tracing and endpoint baseclass forward from main #21

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
package akka.javasdk.testkit.impl

import akka.javasdk.Metadata
import akka.javasdk.Tracing
import akka.javasdk.impl.InternalContext
import akka.javasdk.testkit.MockRegistry
import akka.javasdk.timedaction.CommandContext
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.trace.Tracer

/**
* INTERNAL API Used by the generated testkit
* INTERNAL API Used by the testkit
*/
final class TestKitCommandContextTimed(metadata: Metadata, mockRegistry: MockRegistry = MockRegistry.EMPTY)
extends AbstractTestKitContext(mockRegistry)
Expand All @@ -29,5 +28,5 @@ final class TestKitCommandContextTimed(metadata: Metadata, mockRegistry: MockReg

override def metadata() = metadata

override def getTracer: Tracer = OpenTelemetry.noop().getTracer("noop")
override def tracing(): Tracing = TestKitTracing
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package akka.javasdk.testkit.impl

import akka.javasdk.Metadata
import akka.javasdk.Tracing
import akka.javasdk.eventsourcedentity.CommandContext
import akka.javasdk.impl.InternalContext

Expand All @@ -22,6 +23,8 @@ final class TestKitEventSourcedEntityCommandContext(
this(metadata = metadata, commandName = "stubCommandName")
}

override def tracing(): Tracing = TestKitTracing

}

object TestKitEventSourcedEntityCommandContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import akka.javasdk.eventsourcedentity.EventSourcedEntityContext
import akka.javasdk.testkit.MockRegistry

/**
* INTERNAL API Used by the generated testkit
* INTERNAL API Used by the testkit
*/
final class TestKitEventSourcedEntityContext(
override val entityId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package akka.javasdk.testkit.impl
import akka.javasdk.eventsourcedentity.EventContext

/**
* INTERNAL API Used by the generated testkit
* INTERNAL API Used by the testkit
*/
final class TestKitEventSourcedEntityEventContext extends EventContext {
override def entityId = "testkit-entity-id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
package akka.javasdk.testkit.impl

import akka.javasdk.Metadata
import akka.javasdk.Tracing
import akka.javasdk.keyvalueentity.CommandContext
import akka.javasdk.keyvalueentity.KeyValueEntityContext
import akka.javasdk.testkit.MockRegistry

/**
* INTERNAL API Used by the generated testkit
* INTERNAL API Used by the testkit
*/
final class TestKitKeyValueEntityCommandContext(
override val entityId: String,
Expand All @@ -26,4 +27,6 @@ final class TestKitKeyValueEntityCommandContext(
this(entityId = entityId, metadata = metadata, commandName = "stubCommandName")
}

override def tracing(): Tracing = TestKitTracing

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import akka.javasdk.keyvalueentity.KeyValueEntityContext
import akka.javasdk.testkit.MockRegistry

/**
* INTERNAL API Used by the generated testkit
* INTERNAL API Used by the testkit
*/
final class TestKitKeyValueEntityContext(override val entityId: String, mockRegistry: MockRegistry = MockRegistry.EMPTY)
extends AbstractTestKitContext(mockRegistry)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.javasdk.testkit.impl

import akka.javasdk.Tracing
import io.opentelemetry.api.trace.Span

import java.util.Optional

/**
* INTERNAL API
*/
object TestKitTracing extends Tracing {

override def startSpan(name: String): Optional[Span] = Optional.empty()

override def parentSpan(): Optional[Span] = Optional.empty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import akka.javasdk.annotations.JWT;
import akka.javasdk.annotations.http.HttpEndpoint;
import akka.javasdk.annotations.http.Get;
import akka.javasdk.http.RequestContext;
import akka.javasdk.http.AbstractHttpEndpoint;

import java.util.concurrent.CompletionStage;
import static java.util.concurrent.CompletableFuture.completedStage;
Expand All @@ -19,17 +19,12 @@
// tag::bearer-token[]
@HttpEndpoint("/hello")
@JWT(validate = JWT.JwtMethodMode.BEARER_TOKEN, bearerTokenIssuers = "my-issuer-123", staticClaims = { @JWT.StaticClaim(claim = "sub", pattern = "my-subject-123")})
public class HelloJwtEndpoint {
public class HelloJwtEndpoint extends AbstractHttpEndpoint {
// end::bearer-token[]

RequestContext context;
public HelloJwtEndpoint(RequestContext context){
this.context = context;
}

@Get("/")
public CompletionStage<String> helloWorld() {
var claims = context.getJwtClaims();
var claims = requestContext().getJwtClaims();
var issuer = claims.issuer().get();
var sub = claims.subject().get();
return completedStage("issuer: " + issuer + ", subject: " + sub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@HttpEndpoint("/missingjwt")
public class MissingJwtEndpoint {

// Note: leaving this with injected request context rather than extend AbstractHttpEndpoint to keep
// some test coverage
RequestContext context;
public MissingJwtEndpoint(RequestContext context){
this.context = context;
Expand Down

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions akka-javasdk/src/main/java/akka/javasdk/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ public interface Metadata extends Iterable<Metadata.MetadataEntry> {
*/
CloudEvent asCloudEvent(String id, URI source, String type);


/**
* Get the trace context associated with this request metadata.
* @return The trace context.
*/
TraceContext traceContext();

/**
* Merge the given Metadata entries with this Metadata. If the same key is present in both, both values will be kept.
*
Expand Down
48 changes: 0 additions & 48 deletions akka-javasdk/src/main/java/akka/javasdk/TraceContext.java

This file was deleted.

38 changes: 38 additions & 0 deletions akka-javasdk/src/main/java/akka/javasdk/Tracing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.javasdk;

import akka.annotation.DoNotInherit;
import io.opentelemetry.api.trace.Span;

import java.util.Optional;

/**
* Factory for manually creating open telemetry spans in addition to those automatically provided by
* the runtime and SDK.
*
* <p>Not for user extension. Injectable into endpoint constructors or available through component
* command contexts.
*/
@DoNotInherit
public interface Tracing {
/**
* If tracing is enabled, create and start a new custom span with the given name, setting a parent
* for the span is done automatically so that the span is a child of the incoming request or
* component call.
*
* @return Optional of the span if tracing is enabled, empty option if tracing is not enabled.
*/
Optional<Span> startSpan(String name);

/**
* If tracing is enabled, this returns the current parent span, to use for propagating trace
* parent through third party integrations. This span should only be used for observing, ending it
* or marking it as failed etc. is managed by the SDK and the runtime.
*
* @see {{@link #startSpan(String)}} for creating a custom span tied to some logic in a service.
*/
Optional<Span> parentSpan();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* <li>{@link io.opentelemetry.api.trace.Span}</li>
* <li>Custom types provided by a {@link akka.javasdk.DependencyProvider} from the service setup</li>
* </ul>
* <p>If the annotated class extends {@link akka.javasdk.http.AbstractHttpEndpoint} the request context
* is available without constructor injection.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import akka.javasdk.CloudEvent;
import akka.javasdk.MetadataContext;
import akka.javasdk.Tracing;
import io.opentelemetry.api.trace.Tracer;

import java.util.Optional;
Expand All @@ -19,11 +20,6 @@ public interface MessageContext extends MetadataContext {
*/
Optional<String> eventSubject();

/**
* Get an OpenTelemetry tracer for the current message. This will allow for building and automatic
* exporting of spans.
*
* @return A tracer for the current message, if tracing is configured. Otherwise, a noops tracer.
*/
Tracer getTracer();
/** Access to tracing for custom app specific tracing. */
Tracing tracing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package akka.javasdk.eventsourcedentity;

import akka.javasdk.MetadataContext;
import akka.javasdk.Tracing;

/** An event sourced command context. */
public interface CommandContext extends MetadataContext {
Expand Down Expand Up @@ -35,4 +36,7 @@ public interface CommandContext extends MetadataContext {
* @return The entity id.
*/
String entityId();

/** Access to tracing for custom app specific tracing. */
Tracing tracing();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.javasdk.http;

import akka.annotation.InternalApi;

/**
* Optional base class for HTTP endpoints giving access to a request context without additional constructor parameters
*/
abstract public class AbstractHttpEndpoint {

volatile private RequestContext context;

/**
* INTERNAL API
*
* @hidden
*/
@InternalApi
final public void _internalSetRequestContext(RequestContext context) {
this.context = context;
}

/**
* Always available from request handling methods, not available from the constructor.
*/
protected final RequestContext requestContext() {
if (context == null) {
throw new IllegalStateException("The request context can only be accessed from the request handling methods of the endpoint.");
}
return context;
}

}
Loading
Loading