Skip to content

Commit 385f404

Browse files
committed
apply
1 parent aa592d2 commit 385f404

59 files changed

Lines changed: 253 additions & 148 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/agents/knowledge/javaagent-singletons-patterns.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@
33
## Quick Reference
44

55
- Use when: reviewing `*Singletons` holder classes and their callers
6-
- Review focus: private fields, accessor naming, eager initialization, static-import call sites
6+
- Review focus: field/accessor naming, eager initialization, static-import call sites
77

88
Javaagent modules keep shared `Instrumenter` instances and related collaborators in a dedicated
99
`Singletons` holder class such as `MyLibrarySingletons`.
1010

1111
## Rules
1212

13-
- Keep exported singleton-holder fields `private`. Do not expose package-private or public
14-
`static` fields from `*Singletons` classes.
1513
- Initialize shared collaborators at class-load time, either with `static final` field
1614
initializers or in a `static {}` block.
1715
- Use `GlobalOpenTelemetry.get()` to obtain the `OpenTelemetry` instance.
1816
- The instrumentation name string should match the Gradle module path:
1917
`"io.opentelemetry.<module-name>"`.
20-
- For exported collaborators, use lower camel case field names and give the accessor method the
21-
exact same name as the field. Do not prefix these accessors with `get`.
18+
- For exported collaborators, keep the field `private`, use a lower camel case field name, and
19+
give the accessor method the exact same name as the field. Do not prefix these accessors with
20+
`get`.
2221
- `instrumenter` -> `instrumenter()`
2322
- `helper` -> `helper()`
2423
- `setter` -> `setter()`
25-
- Callers should static import singleton accessor methods and invoke them unqualified.
24+
- For exported uppercase constant-like fields that represent stable identifiers, immutable
25+
descriptors, or semantic keys/handles such as `VirtualField` and `ContextKey`, it is acceptable
26+
to expose them as `public static final` fields with no accessor.
27+
- `CONTEXT` stays `CONTEXT`
28+
- `REQUEST_INFO` stays `REQUEST_INFO`
29+
- `RESPONSE_STATUS` stays `RESPONSE_STATUS`
30+
- Callers should static import the exported singleton member and use it unqualified:
31+
accessor methods for lower camel collaborators, fields for uppercase constant-like members.
2632
- Keep verb-named helper methods as verbs when they perform work instead of returning a stored
2733
field. This naming rule applies to field accessors.
2834

@@ -48,6 +54,18 @@ public final class MyLibrarySingletons {
4854
}
4955
```
5056

57+
Uppercase field exception:
58+
59+
```java
60+
public final class MyLibrarySingletons {
61+
62+
public static final VirtualField<Request, Context> REQUEST_CONTEXT =
63+
VirtualField.find(Request.class, Context.class);
64+
65+
private MyLibrarySingletons() {}
66+
}
67+
```
68+
5169
Caller:
5270

5371
```java
@@ -63,12 +81,26 @@ class MyInstrumentation implements TypeInstrumentation {
6381
}
6482
```
6583

84+
Caller for uppercase field:
85+
86+
```java
87+
import static io.opentelemetry.javaagent.instrumentation.example.MyLibrarySingletons.REQUEST_CONTEXT;
88+
89+
class MyInstrumentation implements TypeInstrumentation {
90+
void doSomething(Request request, Context context) {
91+
REQUEST_CONTEXT.set(request, context);
92+
}
93+
}
94+
```
95+
6696
## What to Flag in Review
6797

68-
- Exposed singleton-holder fields such as `public static final Instrumenter ...`.
98+
- Exposed lower camel collaborator fields such as `public static final Instrumenter ...`.
99+
- Private + accessor wrappers around uppercase semantic keys/handles when a direct `public static
100+
final` field would be clearer and matches the naming guidance.
69101
- Accessor methods named `getInstrumenter()`, `getHelper()`, `getSetter()`, and similar when they
70102
simply return a backing field.
71-
- Call sites that qualify singleton field-accessor calls with the holder class instead of static
72-
importing the accessor method.
103+
- Call sites that qualify singleton member usage with the holder class instead of static importing
104+
the accessor or field.
73105
- Mismatches between a field name and its accessor, such as `private static final Helper helper;`
74106
with `public static Helper getHelper()`.

instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboSingletons.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.apache.dubbo.rpc.Filter;
1313

1414
public final class DubboSingletons {
15-
public static final Filter CLIENT_FILTER;
16-
public static final Filter SERVER_FILTER;
15+
private static final Filter clientFilter;
16+
private static final Filter serverFilter;
1717

1818
static {
1919
DubboTelemetry telemetry =
@@ -22,8 +22,16 @@ public final class DubboSingletons {
2222
ServicePeerAttributesExtractor.create(
2323
new DubboClientNetworkAttributesGetter(), GlobalOpenTelemetry.get()))
2424
.build();
25-
CLIENT_FILTER = telemetry.newClientFilter();
26-
SERVER_FILTER = telemetry.newServerFilter();
25+
clientFilter = telemetry.newClientFilter();
26+
serverFilter = telemetry.newServerFilter();
27+
}
28+
29+
public static Filter clientFilter() {
30+
return clientFilter;
31+
}
32+
33+
public static Filter serverFilter() {
34+
return serverFilter;
2735
}
2836

2937
private DubboSingletons() {}

instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7;
77

8+
import static io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.DubboSingletons.clientFilter;
9+
810
import org.apache.dubbo.common.extension.Activate;
911
import org.apache.dubbo.rpc.Filter;
1012
import org.apache.dubbo.rpc.Invocation;
@@ -19,7 +21,7 @@ public final class OpenTelemetryClientFilter implements Filter {
1921
private final Filter delegate;
2022

2123
public OpenTelemetryClientFilter() {
22-
delegate = DubboSingletons.CLIENT_FILTER;
24+
delegate = clientFilter();
2325
}
2426

2527
@Override

instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7;
77

8+
import static io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.DubboSingletons.serverFilter;
9+
810
import org.apache.dubbo.common.extension.Activate;
911
import org.apache.dubbo.rpc.Filter;
1012
import org.apache.dubbo.rpc.Invocation;
@@ -19,7 +21,7 @@ public final class OpenTelemetryServerFilter implements Filter {
1921
private final Filter delegate;
2022

2123
public OpenTelemetryServerFilter() {
22-
delegate = DubboSingletons.SERVER_FILTER;
24+
delegate = serverFilter();
2325
}
2426

2527
@Override

instrumentation/armeria/armeria-1.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/armeria/v1_3/ArmeriaServerBuilderInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.armeria.v1_3;
77

8+
import static io.opentelemetry.javaagent.instrumentation.armeria.v1_3.ArmeriaSingletons.serverDecorator;
89
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
910
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
1011
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -35,7 +36,7 @@ public static class BuildAdvice {
3536

3637
@Advice.OnMethodEnter
3738
public static void onEnter(@Advice.This ServerBuilder builder) {
38-
builder.decorator(ArmeriaSingletons.SERVER_DECORATOR);
39+
builder.decorator(serverDecorator());
3940
}
4041
}
4142
}

instrumentation/armeria/armeria-1.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/armeria/v1_3/ArmeriaSingletons.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
// Holds singleton references to decorators to match against during suppression.
2121
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/903
2222
public final class ArmeriaSingletons {
23-
public static final Function<HttpClient, HttpClient> CLIENT_DECORATOR;
23+
private static final Function<HttpClient, HttpClient> clientDecorator;
2424

25-
public static final Function<HttpService, HttpService> SERVER_DECORATOR;
25+
private static final Function<HttpService, HttpService> serverDecorator;
2626

2727
static {
2828
CommonConfig config = AgentCommonConfig.get();
@@ -41,10 +41,18 @@ public final class ArmeriaSingletons {
4141
.configure(config);
4242
ArmeriaServerTelemetry serverTelemetry = serverBuilder.build();
4343

44-
CLIENT_DECORATOR = clientTelemetry.createDecorator();
44+
clientDecorator = clientTelemetry.createDecorator();
4545
Function<HttpService, HttpService> libraryDecorator =
4646
serverTelemetry.createDecorator().compose(ResponseCustomizingDecorator::new);
47-
SERVER_DECORATOR = service -> new ServerDecorator(service, libraryDecorator.apply(service));
47+
serverDecorator = service -> new ServerDecorator(service, libraryDecorator.apply(service));
48+
}
49+
50+
public static Function<HttpClient, HttpClient> clientDecorator() {
51+
return clientDecorator;
52+
}
53+
54+
public static Function<HttpService, HttpService> serverDecorator() {
55+
return serverDecorator;
4856
}
4957

5058
private ArmeriaSingletons() {}

instrumentation/armeria/armeria-1.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/armeria/v1_3/ArmeriaWebClientBuilderInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.armeria.v1_3;
77

8+
import static io.opentelemetry.javaagent.instrumentation.armeria.v1_3.ArmeriaSingletons.clientDecorator;
89
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
910
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
1011
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -35,7 +36,7 @@ public static class BuildAdvice {
3536

3637
@Advice.OnMethodEnter
3738
public static void build(@Advice.This WebClientBuilder builder) {
38-
builder.decorator(ArmeriaSingletons.CLIENT_DECORATOR);
39+
builder.decorator(clientDecorator());
3940
}
4041
}
4142
}

instrumentation/elasticsearch/elasticsearch-rest-common-5.0/library/src/main/java/io/opentelemetry/instrumentation/elasticsearch/rest/common/v5_0/internal/ElasticsearchDbAttributesGetter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String getDbQueryText(ElasticsearchRestRequest request) {
5858
&& epDefinition.isSearchEndpoint()
5959
&& httpEntity != null
6060
&& httpEntity.isRepeatable()) {
61-
// Retrieve HTTP body for search-type Elasticsearch requests when CAPTURE_SEARCH_QUERY is
61+
// Retrieve HTTP body for search-type Elasticsearch requests when captureSearchQuery is
6262
// enabled.
6363
try (BufferedReader reader =
6464
new BufferedReader(new InputStreamReader(httpEntity.getContent(), UTF_8))) {

instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcClientBuilderBuildInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass;
99
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
1010
import static io.opentelemetry.javaagent.instrumentation.grpc.v1_6.GrpcSingletons.MANAGED_CHANNEL_BUILDER_INSTRUMENTED;
11+
import static io.opentelemetry.javaagent.instrumentation.grpc.v1_6.GrpcSingletons.clientInterceptor;
1112
import static net.bytebuddy.matcher.ElementMatchers.declaresField;
1213
import static net.bytebuddy.matcher.ElementMatchers.named;
1314

@@ -45,7 +46,7 @@ public static void addInterceptor(
4546
@Advice.This ManagedChannelBuilder<?> builder,
4647
@Advice.FieldValue("interceptors") List<ClientInterceptor> interceptors) {
4748
if (!Boolean.TRUE.equals(MANAGED_CHANNEL_BUILDER_INSTRUMENTED.get(builder))) {
48-
interceptors.add(0, GrpcSingletons.CLIENT_INTERCEPTOR);
49+
interceptors.add(0, clientInterceptor());
4950
MANAGED_CHANNEL_BUILDER_INSTRUMENTED.set(builder, true);
5051
}
5152
}

instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcContextInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;
77

8+
import static io.opentelemetry.javaagent.instrumentation.grpc.v1_6.GrpcSingletons.storage;
89
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
910
import static net.bytebuddy.matcher.ElementMatchers.named;
1011
import static net.bytebuddy.matcher.ElementMatchers.returns;
@@ -37,7 +38,7 @@ public static class ContextBridgeAdvice {
3738
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class)
3839
@Nullable
3940
public static Context.Storage onEnter() {
40-
return GrpcSingletons.getStorage();
41+
return storage();
4142
}
4243

4344
@AssignReturned.ToReturned

0 commit comments

Comments
 (0)