Skip to content

Commit 0b3aab1

Browse files
author
Liudmila Molkova
authored
Clientcore: remove client options (Azure#44764)
* remove ClientOptions from clientcore
1 parent 7318939 commit 0b3aab1

31 files changed

+342
-415
lines changed

sdk/clientcore/core/spotbugs-exclude.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<Class name="io.clientcore.core.http.HttpPipelineBuilderJavaDocCodeSnippets" />
8080
<Class name="io.clientcore.core.http.pipeline.AddHeadersPolicyJavadocCodeSnippets" />
8181
<Class name="io.clientcore.core.http.pipeline.SetRequestIdPolicyJavadocCodeSnippets" />
82-
<Class name="io.clientcore.core.http.pipeline.SetUserAgentPolicyJavadocCodeSnippets" />
82+
<Class name="io.clientcore.core.http.pipeline.UserAgentPolicyJavadocCodeSnippets" />
8383
<Class name="io.clientcore.core.instrumentation.TelemetryForLibraryDevelopersJavaDocCodeSnippets" />
8484
<Class name="io.clientcore.core.models.CloudEventJavaDocCodeSnippet" />
8585
<Class name="io.clientcore.core.models.ContextJavaDocCodeSnippets" />
@@ -383,7 +383,7 @@
383383
</Match>
384384
<Match>
385385
<Bug pattern="UC_USELESS_VOID_METHOD" />
386-
<Class name="io.clientcore.core.http.pipeline.SetUserAgentPolicyJavadocCodeSnippets" />
386+
<Class name="io.clientcore.core.http.pipeline.UserAgentPolicyJavadocCodeSnippets" />
387387
</Match>
388388
<Match>
389389
<Bug pattern="UPM_UNCALLED_PRIVATE_METHOD" />

sdk/clientcore/core/src/main/java/io/clientcore/core/http/pipeline/HttpInstrumentationPolicy.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import io.clientcore.core.http.models.HttpRequest;
1111
import io.clientcore.core.http.models.Response;
1212
import io.clientcore.core.implementation.http.HttpRequestAccessHelper;
13-
import io.clientcore.core.implementation.instrumentation.LibraryInstrumentationOptionsAccessHelper;
13+
import io.clientcore.core.implementation.instrumentation.SdkInstrumentationOptionsAccessHelper;
1414
import io.clientcore.core.instrumentation.Instrumentation;
1515
import io.clientcore.core.instrumentation.InstrumentationContext;
16-
import io.clientcore.core.instrumentation.LibraryInstrumentationOptions;
16+
import io.clientcore.core.instrumentation.SdkInstrumentationOptions;
1717
import io.clientcore.core.instrumentation.logging.ClientLogger;
1818
import io.clientcore.core.instrumentation.logging.LogLevel;
1919
import io.clientcore.core.instrumentation.logging.LoggingEvent;
@@ -145,26 +145,25 @@
145145
public final class HttpInstrumentationPolicy implements HttpPipelinePolicy {
146146
private static final ClientLogger LOGGER = new ClientLogger(HttpInstrumentationPolicy.class);
147147
private static final HttpInstrumentationOptions DEFAULT_OPTIONS = new HttpInstrumentationOptions();
148-
private static final String LIBRARY_NAME;
149-
private static final String LIBRARY_VERSION;
150-
private static final LibraryInstrumentationOptions LIBRARY_OPTIONS;
148+
private static final String SDK_NAME;
149+
private static final String SDK_VERSION;
150+
private static final SdkInstrumentationOptions SDK_OPTIONS;
151151
private static final TraceContextSetter<HttpHeaders> SETTER
152152
= (headers, name, value) -> headers.set(HttpHeaderName.fromString(name), value);
153153

154154
static {
155155
Map<String, String> properties = getProperties("core.properties");
156-
LIBRARY_NAME = properties.getOrDefault("name", "unknown");
157-
LIBRARY_VERSION = properties.getOrDefault("version", "unknown");
158-
LibraryInstrumentationOptions libOptions
159-
= new LibraryInstrumentationOptions(LIBRARY_NAME).setLibraryVersion(LIBRARY_VERSION)
160-
.setSchemaUrl("https://opentelemetry.io/schemas/1.29.0");
156+
SDK_NAME = properties.getOrDefault("name", "unknown");
157+
SDK_VERSION = properties.getOrDefault("version", "unknown");
158+
SdkInstrumentationOptions sdkOptions = new SdkInstrumentationOptions(SDK_NAME).setSdkVersion(SDK_VERSION)
159+
.setSchemaUrl("https://opentelemetry.io/schemas/1.29.0");
161160

162161
// HTTP tracing is special - we suppress nested public API spans, but
163162
// preserve nested HTTP ones.
164163
// We might want to make it configurable for other cases, but let's hide the API for now.
165-
LibraryInstrumentationOptionsAccessHelper.disableSpanSuppression(libOptions);
164+
SdkInstrumentationOptionsAccessHelper.disableSpanSuppression(sdkOptions);
166165

167-
LIBRARY_OPTIONS = libOptions;
166+
SDK_OPTIONS = sdkOptions;
168167
}
169168

170169
private static final int MAX_BODY_LOG_SIZE = 1024 * 16;
@@ -203,7 +202,7 @@ public final class HttpInstrumentationPolicy implements HttpPipelinePolicy {
203202
* @param instrumentationOptions Application telemetry options.
204203
*/
205204
public HttpInstrumentationPolicy(HttpInstrumentationOptions instrumentationOptions) {
206-
this.instrumentation = Instrumentation.create(instrumentationOptions, LIBRARY_OPTIONS);
205+
this.instrumentation = Instrumentation.create(instrumentationOptions, SDK_OPTIONS);
207206
this.tracer = instrumentation.getTracer();
208207
this.meter = instrumentation.getMeter();
209208
this.httpRequestDuration = meter.createDoubleHistogram(REQUEST_DURATION_METRIC_NAME,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package io.clientcore.core.http.pipeline;
5+
6+
import io.clientcore.core.annotations.Metadata;
7+
import io.clientcore.core.annotations.MetadataProperties;
8+
9+
/**
10+
* The {@code UserAgentOptions} class is used to configure the user agent string for HTTP requests.
11+
*/
12+
@Metadata(properties = MetadataProperties.FLUENT)
13+
public final class UserAgentOptions {
14+
private String sdkName;
15+
private String sdkVersion;
16+
private String applicationId;
17+
18+
/**
19+
* Creates a new instance of {@link UserAgentOptions}.
20+
*/
21+
public UserAgentOptions() {
22+
}
23+
24+
/**
25+
* Sets the SDK name to be used in the user agent.
26+
*
27+
* @param sdkName The SDK name to set.
28+
* @return The updated {@link UserAgentOptions} instance.
29+
*/
30+
public UserAgentOptions setSdkName(String sdkName) {
31+
this.sdkName = sdkName;
32+
return this;
33+
}
34+
35+
/**
36+
* Sets the SDK version to be used in the user agent.
37+
*
38+
* @param sdkVersion The SDK version to set.
39+
* @return The updated {@link UserAgentOptions} instance.
40+
*/
41+
public UserAgentOptions setSdkVersion(String sdkVersion) {
42+
this.sdkVersion = sdkVersion;
43+
return this;
44+
}
45+
46+
/**
47+
* Sets the application ID to be used in the user agent.
48+
*
49+
* @param applicationId The application ID to set.
50+
* @return The updated {@link UserAgentOptions} instance.
51+
*/
52+
public UserAgentOptions setApplicationId(String applicationId) {
53+
this.applicationId = applicationId;
54+
return this;
55+
}
56+
57+
/**
58+
* Gets the SDK name for the user agent.
59+
*
60+
* @return The SDK name.
61+
*/
62+
public String getSdkName() {
63+
return sdkName;
64+
}
65+
66+
/**
67+
* Gets the SDK version for the user agent.
68+
*
69+
* @return The SDK version.
70+
*/
71+
public String getSdkVersion() {
72+
return sdkVersion;
73+
}
74+
75+
/**
76+
* Gets the application ID for the user agent.
77+
*
78+
* @return The application ID.
79+
*/
80+
public String getApplicationId() {
81+
return applicationId;
82+
}
83+
}

sdk/clientcore/core/src/main/java/io/clientcore/core/http/pipeline/UserAgentPolicy.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
* Once added to the pipeline, requests will have their "User-Agent" header set to "MyApp/1.0" by the
2929
* {@code UserAgentPolicy}.</p>
3030
*
31-
* <!-- src_embed io.clientcore.core.http.pipeline.SetUserAgentPolicy.constructor -->
31+
* <!-- src_embed io.clientcore.core.http.pipeline.UserAgentPolicy.constructor -->
3232
* <pre>
3333
* UserAgentPolicy policy = new UserAgentPolicy&#40;&quot;MyApp&#47;1.0&quot;&#41;;
3434
* </pre>
35-
* <!-- end io.clientcore.core.http.pipeline.SetUserAgentPolicy.constructor -->
35+
* <!-- end io.clientcore.core.http.pipeline.UserAgentPolicy.constructor -->
3636
*
3737
* @see io.clientcore.core.http.pipeline
3838
* @see HttpPipelinePolicy
@@ -49,7 +49,7 @@ public class UserAgentPolicy implements HttpPipelinePolicy {
4949
* Creates a {@link UserAgentPolicy} with a default user agent string.
5050
*/
5151
public UserAgentPolicy() {
52-
this(null);
52+
this((String) null);
5353
}
5454

5555
/**
@@ -69,12 +69,10 @@ public UserAgentPolicy(String userAgent) {
6969
/**
7070
* Creates a UserAgentPolicy with the {@code sdkName} and {@code sdkVersion} in the User-Agent header value.
7171
*
72-
* @param applicationId User specified application Id.
73-
* @param sdkName Name of the client library.
74-
* @param sdkVersion Version of the client library.
72+
* @param userAgentOptions Options allowing to customize the user agent string.
7573
*/
76-
public UserAgentPolicy(String applicationId, String sdkName, String sdkVersion) {
77-
this.userAgent = toUserAgentString(applicationId, sdkName, sdkVersion);
74+
public UserAgentPolicy(UserAgentOptions userAgentOptions) {
75+
this.userAgent = toUserAgentString(userAgentOptions);
7876
}
7977

8078
@Override
@@ -88,11 +86,6 @@ public final HttpPipelinePosition getPipelinePosition() {
8886
return HttpPipelinePosition.BEFORE_REDIRECT;
8987
}
9088

91-
private static final int MAX_APPLICATION_ID_LENGTH = 24;
92-
private static final String INVALID_APPLICATION_ID_LENGTH
93-
= "'applicationId' length cannot be greater than " + MAX_APPLICATION_ID_LENGTH;
94-
private static final String INVALID_APPLICATION_ID_SPACE = "'applicationId' cannot contain spaces.";
95-
9689
/**
9790
* Default {@code UserAgent} header.
9891
*/
@@ -101,29 +94,29 @@ public final HttpPipelinePosition getPipelinePosition() {
10194
/**
10295
* Return user agent string for the given sdk name and version.
10396
*
104-
* @param applicationId Name of the application.
105-
* @param sdkName Name of the SDK.
106-
* @param sdkVersion Version of the SDK.
107-
* @return User agent string as specified in design guidelines.
97+
* @param userAgentOptions Options allowing to customize the user agent string.
10898
*
109-
* @throws IllegalArgumentException If {@code applicationId} contains spaces or is larger than 24 characters in
110-
* length.
99+
* @throws IllegalArgumentException If {@code applicationId} contains spaces.
111100
*/
112-
private static String toUserAgentString(String applicationId, String sdkName, String sdkVersion) {
101+
private static String toUserAgentString(UserAgentOptions userAgentOptions) {
113102
StringBuilder userAgentBuilder = new StringBuilder();
114103

104+
String applicationId = userAgentOptions.getApplicationId();
105+
115106
if (!CoreUtils.isNullOrEmpty(applicationId)) {
116-
if (applicationId.length() > MAX_APPLICATION_ID_LENGTH) {
117-
throw new IllegalArgumentException(INVALID_APPLICATION_ID_LENGTH);
118-
} else if (applicationId.contains(" ")) {
119-
throw new IllegalArgumentException(INVALID_APPLICATION_ID_SPACE);
107+
if (applicationId.contains(" ")) {
108+
throw new IllegalArgumentException("'applicationid' cannot contain spaces.");
120109
} else {
121110
userAgentBuilder.append(applicationId).append(" ");
122111
}
123112
}
124113

125114
// Add the required default User-Agent string.
126-
userAgentBuilder.append(DEFAULT_USER_AGENT_HEADER).append("-").append(sdkName).append("/").append(sdkVersion);
115+
userAgentBuilder.append(DEFAULT_USER_AGENT_HEADER)
116+
.append("-")
117+
.append(userAgentOptions.getSdkName())
118+
.append("/")
119+
.append(userAgentOptions.getSdkVersion());
127120
appendPlatformInfo(userAgentBuilder);
128121

129122
return userAgentBuilder.toString();

sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/InstrumentationUtils.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package io.clientcore.core.implementation.instrumentation;
55

6-
import io.clientcore.core.instrumentation.LibraryInstrumentationOptions;
6+
import io.clientcore.core.instrumentation.SdkInstrumentationOptions;
77
import io.clientcore.core.instrumentation.metrics.DoubleHistogram;
88
import io.clientcore.core.instrumentation.metrics.Meter;
99

@@ -23,19 +23,18 @@ public final class InstrumentationUtils {
2323
private static final List<Double> DURATION_BOUNDARIES_ADVICE = Collections.unmodifiableList(
2424
Arrays.asList(0.005d, 0.01d, 0.025d, 0.05d, 0.075d, 0.1d, 0.25d, 0.5d, 0.75d, 1d, 2.5d, 5d, 7.5d, 10d));
2525

26-
public static final LibraryInstrumentationOptions UNKNOWN_LIBRARY_OPTIONS
27-
= new LibraryInstrumentationOptions("unknown");
26+
public static final SdkInstrumentationOptions UNKNOWN_LIBRARY_OPTIONS = new SdkInstrumentationOptions("unknown");
2827

2928
/**
3029
* Creates a new {@link DoubleHistogram} for measuring the duration of client operations.
31-
* @param libraryName the name of the library - corresponds to artifact id and does not include group id
30+
* @param sdkName the name of the library - corresponds to artifact id and does not include group id
3231
* @param meter the meter to use for creating the histogram
3332
* @return a new {@link DoubleHistogram} for measuring the duration of client operations
3433
*/
35-
public static DoubleHistogram createOperationDurationHistogram(String libraryName, Meter meter) {
36-
if (meter.isEnabled() && libraryName != null) {
34+
public static DoubleHistogram createOperationDurationHistogram(String sdkName, Meter meter) {
35+
if (meter.isEnabled() && sdkName != null) {
3736
String metricDescription = "Duration of client operation";
38-
String metricName = libraryName.replace("-", ".") + ".client.operation.duration";
37+
String metricName = sdkName.replace("-", ".") + ".client.operation.duration";
3938
return meter.createDoubleHistogram(metricName, metricDescription, "s", DURATION_BOUNDARIES_ADVICE);
4039
}
4140

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,41 @@
33

44
package io.clientcore.core.implementation.instrumentation;
55

6-
import io.clientcore.core.instrumentation.LibraryInstrumentationOptions;
6+
import io.clientcore.core.instrumentation.SdkInstrumentationOptions;
77

88
/**
9-
* Helper class to access package-private members of {@link LibraryInstrumentationOptions}.
9+
* Helper class to access package-private members of {@link SdkInstrumentationOptions}.
1010
*/
11-
public final class LibraryInstrumentationOptionsAccessHelper {
11+
public final class SdkInstrumentationOptionsAccessHelper {
1212

13-
private static LibraryInstrumentationOptionsAccessor accessor;
13+
private static SdkInstrumentationOptionsAccessor accessor;
1414

1515
/**
16-
* Defines the methods that can be called on an instance of {@link LibraryInstrumentationOptions}.
16+
* Defines the methods that can be called on an instance of {@link SdkInstrumentationOptions}.
1717
*/
18-
public interface LibraryInstrumentationOptionsAccessor {
18+
public interface SdkInstrumentationOptionsAccessor {
1919

2020
/**
2121
* Disables span suppression for the given options.
2222
* @param options the options to disable span suppression for
2323
* @return the options with span suppression disabled
2424
*/
25-
LibraryInstrumentationOptions disableSpanSuppression(LibraryInstrumentationOptions options);
25+
SdkInstrumentationOptions disableSpanSuppression(SdkInstrumentationOptions options);
2626

2727
/**
2828
* Checks if span suppression is disabled for the given options.
2929
* @param options the options to check
3030
* @return true if span suppression is disabled, false otherwise
3131
*/
32-
boolean isSpanSuppressionDisabled(LibraryInstrumentationOptions options);
32+
boolean isSpanSuppressionDisabled(SdkInstrumentationOptions options);
3333
}
3434

3535
/**
3636
* Disables span suppression for the given options.
3737
* @param options the options to disable span suppression for
3838
* @return the options with span suppression disabled
3939
*/
40-
public static LibraryInstrumentationOptions disableSpanSuppression(LibraryInstrumentationOptions options) {
40+
public static SdkInstrumentationOptions disableSpanSuppression(SdkInstrumentationOptions options) {
4141
return accessor.disableSpanSuppression(options);
4242
}
4343

@@ -46,18 +46,18 @@ public static LibraryInstrumentationOptions disableSpanSuppression(LibraryInstru
4646
* @param options the options to check
4747
* @return true if span suppression is disabled, false otherwise
4848
*/
49-
public static boolean isSpanSuppressionDisabled(LibraryInstrumentationOptions options) {
49+
public static boolean isSpanSuppressionDisabled(SdkInstrumentationOptions options) {
5050
return accessor.isSpanSuppressionDisabled(options);
5151
}
5252

5353
/**
5454
* Sets the accessor.
5555
* @param accessor the accessor
5656
*/
57-
public static void setAccessor(LibraryInstrumentationOptionsAccessor accessor) {
58-
LibraryInstrumentationOptionsAccessHelper.accessor = accessor;
57+
public static void setAccessor(SdkInstrumentationOptionsAccessor accessor) {
58+
SdkInstrumentationOptionsAccessHelper.accessor = accessor;
5959
}
6060

61-
private LibraryInstrumentationOptionsAccessHelper() {
61+
private SdkInstrumentationOptionsAccessHelper() {
6262
}
6363
}

sdk/clientcore/core/src/main/java/io/clientcore/core/implementation/instrumentation/fallback/FallbackInstrumentation.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
package io.clientcore.core.implementation.instrumentation.fallback;
55

66
import io.clientcore.core.http.models.RequestContext;
7-
import io.clientcore.core.implementation.instrumentation.LibraryInstrumentationOptionsAccessHelper;
87
import io.clientcore.core.implementation.instrumentation.NoopMeter;
8+
import io.clientcore.core.implementation.instrumentation.SdkInstrumentationOptionsAccessHelper;
99
import io.clientcore.core.instrumentation.Instrumentation;
1010
import io.clientcore.core.instrumentation.InstrumentationAttributes;
1111
import io.clientcore.core.instrumentation.InstrumentationContext;
1212
import io.clientcore.core.instrumentation.InstrumentationOptions;
13-
import io.clientcore.core.instrumentation.LibraryInstrumentationOptions;
13+
import io.clientcore.core.instrumentation.SdkInstrumentationOptions;
1414
import io.clientcore.core.instrumentation.metrics.Meter;
1515
import io.clientcore.core.instrumentation.tracing.Span;
1616
import io.clientcore.core.instrumentation.tracing.SpanBuilder;
@@ -44,16 +44,16 @@ public class FallbackInstrumentation implements Instrumentation {
4444
/**
4545
* Creates a new instance of {@link FallbackInstrumentation}.
4646
* @param instrumentationOptions the application instrumentation options
47-
* @param libraryOptions the library instrumentation options
47+
* @param sdkOptions the library instrumentation options
4848
* @param host the service host
4949
* @param port the service port
5050
*/
51-
public FallbackInstrumentation(InstrumentationOptions instrumentationOptions,
52-
LibraryInstrumentationOptions libraryOptions, String host, int port) {
53-
this.allowNestedSpans = libraryOptions != null
54-
&& LibraryInstrumentationOptionsAccessHelper.isSpanSuppressionDisabled(libraryOptions);
51+
public FallbackInstrumentation(InstrumentationOptions instrumentationOptions, SdkInstrumentationOptions sdkOptions,
52+
String host, int port) {
53+
this.allowNestedSpans
54+
= sdkOptions != null && SdkInstrumentationOptionsAccessHelper.isSpanSuppressionDisabled(sdkOptions);
5555
this.isTracingEnabled = instrumentationOptions == null || instrumentationOptions.isTracingEnabled();
56-
this.tracer = new FallbackTracer(instrumentationOptions, libraryOptions);
56+
this.tracer = new FallbackTracer(instrumentationOptions, sdkOptions);
5757
this.serviceHost = host;
5858
this.servicePort = port;
5959
}

0 commit comments

Comments
 (0)