-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client span keys: suppressing same instrumentation (#3691)
- Loading branch information
Showing
13 changed files
with
775 additions
and
52 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
.../java/io/opentelemetry/instrumentation/api/instrumenter/CompositeSuppressionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
|
||
final class CompositeSuppressionStrategy extends SpanSuppressionStrategy { | ||
private final SpanSuppressionStrategy clientStrategy; | ||
private final SpanSuppressionStrategy producerStrategy; | ||
private final SpanSuppressionStrategy serverStrategy; | ||
private final SpanSuppressionStrategy consumerStrategy; | ||
|
||
CompositeSuppressionStrategy( | ||
SpanSuppressionStrategy client, | ||
SpanSuppressionStrategy producer, | ||
SpanSuppressionStrategy server, | ||
SpanSuppressionStrategy consumer) { | ||
this.clientStrategy = client; | ||
this.producerStrategy = producer; | ||
this.serverStrategy = server; | ||
this.consumerStrategy = consumer; | ||
} | ||
|
||
@Override | ||
Context storeInContext(Context context, SpanKind spanKind, Span span) { | ||
switch (spanKind) { | ||
case CLIENT: | ||
return clientStrategy.storeInContext(context, spanKind, span); | ||
case PRODUCER: | ||
return producerStrategy.storeInContext(context, spanKind, span); | ||
case SERVER: | ||
return serverStrategy.storeInContext(context, spanKind, span); | ||
case CONSUMER: | ||
return consumerStrategy.storeInContext(context, spanKind, span); | ||
case INTERNAL: | ||
return context; | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
boolean shouldSuppress(Context parentContext, SpanKind spanKind) { | ||
switch (spanKind) { | ||
case CLIENT: | ||
return clientStrategy.shouldSuppress(parentContext, spanKind); | ||
case PRODUCER: | ||
return producerStrategy.shouldSuppress(parentContext, spanKind); | ||
case SERVER: | ||
return serverStrategy.shouldSuppress(parentContext, spanKind); | ||
case CONSUMER: | ||
return consumerStrategy.shouldSuppress(parentContext, spanKind); | ||
case INTERNAL: | ||
return false; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../main/java/io/opentelemetry/instrumentation/api/instrumenter/NoopSuppressionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
|
||
final class NoopSuppressionStrategy extends SpanSuppressionStrategy { | ||
|
||
static final SpanSuppressionStrategy INSTANCE = new NoopSuppressionStrategy(); | ||
|
||
@Override | ||
Context storeInContext(Context context, SpanKind spanKind, Span span) { | ||
return context; | ||
} | ||
|
||
@Override | ||
boolean shouldSuppress(Context parentContext, SpanKind spanKind) { | ||
return false; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...entation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/SpanKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
/** Makes span keys for specific instrumentation accessible to enrich and suppress spans. */ | ||
public final class SpanKey { | ||
|
||
private static final ContextKey<Span> SERVER_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-server"); | ||
private static final ContextKey<Span> CONSUMER_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-consumer"); | ||
|
||
// TODO bridge these constants in AgentContextStorage | ||
private static final ContextKey<Span> HTTP_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-http"); | ||
private static final ContextKey<Span> RPC_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-rpc"); | ||
private static final ContextKey<Span> DB_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-db"); | ||
private static final ContextKey<Span> MESSAGING_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-messaging"); | ||
|
||
// this is used instead of above, depending on the configuration value for | ||
// otel.instrumentation.experimental.outgoing-span-suppression-by-type | ||
private static final ContextKey<Span> CLIENT_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-client"); | ||
|
||
private static final ContextKey<Span> PRODUCER_KEY = | ||
ContextKey.named("opentelemetry-traces-span-key-producer"); | ||
|
||
public static final SpanKey SERVER = new SpanKey(SERVER_KEY); | ||
public static final SpanKey CONSUMER = new SpanKey(CONSUMER_KEY); | ||
|
||
static final SpanKey HTTP_CLIENT = new SpanKey(HTTP_KEY); | ||
static final SpanKey RPC_CLIENT = new SpanKey(RPC_KEY); | ||
static final SpanKey DB_CLIENT = new SpanKey(DB_KEY); | ||
static final SpanKey MESSAGING_PRODUCER = new SpanKey(MESSAGING_KEY); | ||
|
||
// this is used instead of above, depending on the configuration value for | ||
// otel.instrumentation.experimental.outgoing-span-suppression-by-type | ||
public static final SpanKey ALL_CLIENTS = new SpanKey(CLIENT_KEY); | ||
public static final SpanKey ALL_PRODUCERS = new SpanKey(PRODUCER_KEY); | ||
|
||
private final ContextKey<Span> key; | ||
|
||
SpanKey(ContextKey<Span> key) { | ||
this.key = key; | ||
} | ||
|
||
public Context storeInContext(Context context, Span span) { | ||
return context.with(key, span); | ||
} | ||
|
||
@Nullable | ||
public Span fromContextOrNull(Context context) { | ||
return context.get(key); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../main/java/io/opentelemetry/instrumentation/api/instrumenter/SpanSuppressionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter; | ||
|
||
import static java.util.Collections.singleton; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
import java.util.Set; | ||
|
||
abstract class SpanSuppressionStrategy { | ||
private static final SpanSuppressionStrategy SERVER_STRATEGY = | ||
new SuppressIfSameSpanKeyStrategy(singleton(SpanKey.SERVER)); | ||
private static final SpanSuppressionStrategy CONSUMER_STRATEGY = | ||
new StoreOnlyStrategy(singleton(SpanKey.CONSUMER)); | ||
private static final SpanSuppressionStrategy ALL_CLIENTS_STRATEGY = | ||
new SuppressIfSameSpanKeyStrategy(singleton(SpanKey.ALL_CLIENTS)); | ||
private static final SpanSuppressionStrategy ALL_PRODUCERS_STRATEGY = | ||
new SuppressIfSameSpanKeyStrategy(singleton(SpanKey.ALL_PRODUCERS)); | ||
|
||
public static final SpanSuppressionStrategy SUPPRESS_ALL_NESTED_OUTGOING_STRATEGY = | ||
new CompositeSuppressionStrategy( | ||
ALL_CLIENTS_STRATEGY, ALL_PRODUCERS_STRATEGY, SERVER_STRATEGY, CONSUMER_STRATEGY); | ||
|
||
private static final SpanSuppressionStrategy NO_CLIENT_SUPPRESSION_STRATEGY = | ||
new CompositeSuppressionStrategy( | ||
NoopSuppressionStrategy.INSTANCE, | ||
NoopSuppressionStrategy.INSTANCE, | ||
SERVER_STRATEGY, | ||
CONSUMER_STRATEGY); | ||
|
||
static SpanSuppressionStrategy from(Set<SpanKey> clientSpanKeys) { | ||
if (clientSpanKeys.isEmpty()) { | ||
return NO_CLIENT_SUPPRESSION_STRATEGY; | ||
} | ||
|
||
SpanSuppressionStrategy clientOrProducerStrategy = | ||
new SuppressIfSameSpanKeyStrategy(clientSpanKeys); | ||
return new CompositeSuppressionStrategy( | ||
clientOrProducerStrategy, clientOrProducerStrategy, SERVER_STRATEGY, CONSUMER_STRATEGY); | ||
} | ||
|
||
abstract Context storeInContext(Context context, SpanKind spanKind, Span span); | ||
|
||
abstract boolean shouldSuppress(Context parentContext, SpanKind spanKind); | ||
} |
Oops, something went wrong.