Skip to content

Commit e19f80a

Browse files
authored
Add Traits and Timeouts Documentation (Azure#36710)
Add Traits and Timeouts Documentation
1 parent 5495bc1 commit e19f80a

File tree

1 file changed

+138
-36
lines changed

1 file changed

+138
-36
lines changed

sdk/core/azure-core/README.md

Lines changed: 138 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
[![Build Documentation](https://img.shields.io/badge/documentation-published-blue.svg)](https://azure.github.io/azure-sdk-for-java)
44

55
Azure Core provides shared primitives, abstractions, and helpers for modern Java Azure SDK client libraries.
6-
These libraries follow the [Azure SDK Design Guidelines for Java](https://azure.github.io/azure-sdk/java_introduction.html)
6+
These libraries follow
7+
the [Azure SDK Design Guidelines for Java](https://azure.github.io/azure-sdk/java_introduction.html)
78
and can be easily identified by package names starting with `com.azure` and module names starting with `azure-`,
89
e.g. `com.azure.storage.blobs` would be found within the `/sdk/storage/azure-storage-blob` directory. A more complete
9-
list of client libraries using Azure Core can be found [here](https://azure.github.io/azure-sdk/releases/latest/#java-packages).
10+
list of client libraries using Azure Core can be
11+
found [here](https://azure.github.io/azure-sdk/releases/latest/#java-packages).
1012

1113
Azure Core allows client libraries to expose common functionality consistently, so that once you learn how to use these
1214
APIs in one client library, you will know how to use them in other client libraries.
@@ -21,8 +23,10 @@ APIs in one client library, you will know how to use them in other client librar
2123

2224
#### Include the BOM file
2325

24-
Please include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number.
25-
To learn more about the BOM, see the [AZURE SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/boms/azure-sdk-bom/README.md).
26+
Please include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the
27+
library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number.
28+
To learn more about the BOM, see
29+
the [AZURE SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/boms/azure-sdk-bom/README.md).
2630

2731
```xml
2832
<dependencyManagement>
@@ -37,28 +41,31 @@ To learn more about the BOM, see the [AZURE SDK BOM README](https://github.com/A
3741
</dependencies>
3842
</dependencyManagement>
3943
```
40-
and then include the direct dependency in the dependencies section without the version tag. Typically, you won't need to install or depend on Azure Core, instead it will be transitively downloaded by your build
44+
45+
and then include the direct dependency in the dependencies section without the version tag. Typically, you won't need to
46+
install or depend on Azure Core, instead it will be transitively downloaded by your build
4147
tool when you depend on client libraries using it.
4248

4349
```xml
4450
<dependencies>
45-
<dependency>
46-
<groupId>com.azure</groupId>
47-
<artifactId>azure-core</artifactId>
48-
</dependency>
51+
<dependency>
52+
<groupId>com.azure</groupId>
53+
<artifactId>azure-core</artifactId>
54+
</dependency>
4955
</dependencies>
5056
```
5157

5258
#### Include direct dependency
59+
5360
If you want to take dependency on a particular version of the library that is not present in the BOM,
5461
add the direct dependency to your project as follows.
5562

5663
[//]: # ({x-version-update-start;com.azure:azure-core;current})
5764
```xml
5865
<dependency>
59-
<groupId>com.azure</groupId>
60-
<artifactId>azure-core</artifactId>
61-
<version>1.43.0</version>
66+
<groupId>com.azure</groupId>
67+
<artifactId>azure-core</artifactId>
68+
<version>1.43.0</version>
6269
</dependency>
6370
```
6471
[//]: # ({x-version-update-end})
@@ -67,12 +74,13 @@ add the direct dependency to your project as follows.
6774

6875
The key concepts of Azure Core (and therefore all Azure client libraries using Azure Core) include:
6976

70-
- Configuring service clients, e.g. configuring retries, logging, etc.
77+
- Configuring service clients, e.g. configuring retries, logging, etc. (`HttpTrait<T>`, `ConfigurationTrait<T>`, etc.)
7178
- Accessing HTTP response details (`Response<T>`).
7279
- Calling long-running operations (`PollerFlux<T>`).
7380
- Paging and asynchronous streams (`ContinuablePagedFlux<T>`).
7481
- Exceptions for reporting errors from service requests consistently.
7582
- Abstractions for representing Azure SDK credentials.
83+
- Operation timeouts
7684

7785
These will be introduced by way of the examples presented below.
7886

@@ -102,15 +110,106 @@ consumer requests more data until the consumer finishes processing or all pages
102110

103111
### Long Running Operations with `PollerFlux<T>`
104112

105-
`PollerFlux` manages sending an initial service request and requesting processing updates on a fix interval until polling is cancelled or reaches a terminal state.
113+
`PollerFlux` manages sending an initial service request and requesting processing updates on a fix interval until
114+
polling is cancelled or reaches a terminal state.
115+
116+
### Configuring Builders
117+
118+
Builders are used to create service clients and some `TokenCredential` implementations. They can be configured with a
119+
variety of options, including `HttpPipeline` and `HttpClient` for HTTP-based clients and more general options such as
120+
`Configuration` and`endpoint`. To allow for simpler integration into frameworks such as Spring and to allow generic
121+
methods to be used for all builders `azure-core` provides a set of interfaces that can be implemented to provide
122+
the necessary functionality.
123+
124+
#### HttpTrait<T>
125+
126+
`HttpTrait<T>` contains methods for setting key configurations for HTTP-based clients. This interface will allow you to
127+
configure the `HttpClient`, `HttpPipeline`, `HttpPipelinePolicy`s, `RetryOptions`, `HttpLogOptions`, and `ClientOptions`
128+
(preferably `HttpClientOptions` as it is more specific for HTTP-based service clients).
129+
130+
For builders that expose `HttpTrait<T>`, if an `HttpPipeline` or `HttpClient` isn't set a default instance will be
131+
created based on classpath configurations and the `ClientOptions` based to the builder. This can cause confusion if
132+
you're expecting specific behavior for your client, such as using a proxy that wasn't loaded from the environment. To
133+
avoid this, it is recommended to always set the `HttpPipeline` or `HttpClient` in all clients if you're building if your
134+
configurations aren't based on the environment running the application.
135+
136+
#### Credential Traits
137+
138+
Azure Core provides different credentials for authenticating with Azure services. Each credential type has a
139+
corresponding trait that can be implemented to provide the credential to the client builder. The following table
140+
lists the credential traits and the corresponding credential type.
141+
142+
| Credential Trait | Credential Type |
143+
|-----------------------------------|-----------------------------------------------------------|
144+
| `AzureKeyCredentialTrait` | `AzureKeyCredential` |
145+
| `AzureNamedKeyCredentialTrait` | `AzureNamedKeyCredential` |
146+
| `AzureSasCredentialTrait` | `AzureSasCredential` |
147+
| `ConnectionStringCredentialTrait` | `String` (there is no formal type for connection strings) |
148+
| `KeyCredentialTrait` | `KeyCredential` |
149+
| `TokenCredentialTrait` | `TokenCredential` |
150+
151+
#### ConfigurationTrait<T>
152+
153+
`ConfigurationTrait<T>` allows for setting `Configuration` on service clients. `Configuration` can be used to pass a set
154+
of runtime behaviors to the client builder such as how `ProxyOptions` are loaded from the environment, implicitly
155+
passing credentials to some client builders that support it, and more.
156+
157+
#### EndpointTrait<T>
158+
159+
`EndpointTrait<T>` allows for setting the service endpoint on service clients.
160+
161+
### Operation Timeouts
162+
163+
Azure SDKs provide a few, consistent ways to configure timeouts on API calls. Each timeout effects a different scope
164+
of the Azure SDKs and calling application.
165+
166+
#### HTTP Timeouts
167+
168+
HTTP timeouts are the lowest level of timeout handling the Azure SDKs provide. These timeouts can be configured when
169+
building `HttpClient`s or using `HttpClientOptions` when building service clients without configuring an `HttpClient`
170+
yourself. The following table lists the HTTP timeout, the corresponding `HttpClientOptions` method that can be used to
171+
set it, environment variable to control the default value, the default value if the environment value isn't set, and a
172+
brief description of what the timeout effects.
173+
174+
| HTTP Timeout | `HttpClientOptions` Method | Environment Variable | Default Value | Description |
175+
|------------------|--------------------------------|--------------------------------|---------------|-------------------------------------------------------------------------------------------------------------------|
176+
| Connect Timeout | `setConnectTimeout(Duration)` | AZURE_REQUEST_CONNECT_TIMEOUT | 10 seconds | The amount of time for a connection to be established before timing out. |
177+
| Write Timeout | `setWriteTimeout(Duration)` | AZURE_REQUEST_WRITE_TIMEOUT | 60 seconds | The amount of time between each request data write to the network before timing out. |
178+
| Response Timeout | `setResponseTimeout(Duration)` | AZURE_REQUEST_RESPONSE_TIMEOUT | 60 seconds | The amount of time between finishing sending the request to receiving the first response bytes before timing out. |
179+
| Read Timeout | `setReadTimeout(Duration)` | AZURE_REQUEST_READ_TIMEOUT | 60 seconds | The amount of time between each response data read from the network before timing out. |
180+
181+
Since these timeouts are closest to the network, if they trigger they will be propagated back through the `HttpPipeline`
182+
and generally should be retried by the `RetryPolicy`.
183+
184+
#### HttpPipeline Timeouts
185+
186+
HttpPipeline timeouts are the next level of timeout handling the Azure SDKs provide. These timeouts are configured using
187+
an `HttpPipelinePolicy` and configuring a timeout using either `Mono.timeout` for asynchronous requests or an
188+
`ExecutorService` with a timed `get(long, TimeUnit)` for synchronous requests.
189+
190+
Depending on the location within the `HttpPipeline`, these timeouts may be captured by the `RetryPolicy` and retried.
191+
If the timeout policy is `PER_RETRY` (`HttpPipelinePolicy.getPipelinePosition()`) the timeout will be captured by the
192+
`RetryPolicy` as it will be positioned after the `RetryPolicy`, therefore in its capture scope, if it is `PER_CALL`
193+
retrying the request will need to be handled by application logic.
194+
195+
#### Service Client Timeouts
196+
197+
Service client timeouts are the highest level of timeout handling the Azure SDKs provide. These timeouts are configured
198+
by passing `Duration timeout` into synchronous service methods that support timeouts or by using `Mono.timeout` or
199+
`Flux.timeout` on asynchronous service methods.
200+
201+
Since these timeouts are on the API call itself they cannot be captured by any retry mechanisms within the Azure SDKs
202+
and must be handled by application logic.
106203

107204
## Next steps
108205

109-
Get started with Azure libraries that are [built using Azure Core](https://azure.github.io/azure-sdk/releases/latest/#java).
206+
Get started with Azure libraries that
207+
are [built using Azure Core](https://azure.github.io/azure-sdk/releases/latest/#java).
110208

111209
## Troubleshooting
112210

113-
If you encounter any bugs, please file issues via [GitHub Issues](https://github.com/Azure/azure-sdk-for-java/issues/new/choose)
211+
If you encounter any bugs, please file issues
212+
via [GitHub Issues](https://github.com/Azure/azure-sdk-for-java/issues/new/choose)
114213
or checkout [StackOverflow for Azure Java SDK](https://stackoverflow.com/questions/tagged/azure-java-sdk).
115214

116215
### Enabling Logging
@@ -121,35 +220,36 @@ locate the root issue. View the [logging][logging] documentation for guidance ab
121220

122221
#### HTTP Request and Response Logging
123222

124-
HTTP request and response logging can be enabled by setting `HttpLogDetailLevel` in the `HttpLogOptions` used to create
223+
HTTP request and response logging can be enabled by setting `HttpLogDetailLevel` in the `HttpLogOptions` used to create
125224
an HTTP-based service client or by setting the environment variable or system property `AZURE_HTTP_LOG_DETAIL_LEVEL`.
126225
The following table displays the valid options for `AZURE_HTTP_LOG_DETAIL_LEVEL` and the `HttpLogDetailLevel` it
127226
correlates to (valid options are case-insensitive):
128227

129-
| `AZURE_HTTP_LOG_DETAIL_LEVEL` value | `HttpLogDetailLevel` enum |
130-
| ----------------------------------- | ------------------------- |
131-
| `basic` | `HttpLogDetailLevel.BASIC` |
132-
| `headers` | `HttpLogDetailLevel.HEADERS` |
133-
| `body` | `HttpLogDetailLevel.BODY` |
134-
| `body_and_headers` | `HttpLogDetailLevel.BODY_AND_HEADERS` |
135-
| `bodyandheaders` | `HttpLogDetailLevel.BODY_AND_HEADERS` |
136-
137-
All other values, or unsupported values, result in `HttpLogDetailLevel.NONE`, or disabled HTTP request and response
138-
logging. Logging [must be enabled](#enabling-logging) to log HTTP requests and responses. Logging of HTTP headers requires `verbose`
228+
| `AZURE_HTTP_LOG_DETAIL_LEVEL` value | `HttpLogDetailLevel` enum |
229+
|-------------------------------------|---------------------------------------|
230+
| `basic` | `HttpLogDetailLevel.BASIC` |
231+
| `headers` | `HttpLogDetailLevel.HEADERS` |
232+
| `body` | `HttpLogDetailLevel.BODY` |
233+
| `body_and_headers` | `HttpLogDetailLevel.BODY_AND_HEADERS` |
234+
| `bodyandheaders` | `HttpLogDetailLevel.BODY_AND_HEADERS` |
235+
236+
All other values, or unsupported values, result in `HttpLogDetailLevel.NONE`, or disabled HTTP request and response
237+
logging. Logging [must be enabled](#enabling-logging) to log HTTP requests and responses. Logging of HTTP headers
238+
requires `verbose`
139239
logging to be enabled. The following table explains what logging is enabled for each `HttpLogDetailLevel`:
140240

141-
| `HttpLogDetailLevel` value | Logging enabled |
142-
| -------------------------- |-------------------------------------------------------------------------|
143-
| `HttpLogDetailLevel.NONE` | No HTTP request or response logging |
144-
| `HttpLogDetailLevel.BASIC` | HTTP request method, response status code, and request and response URL |
145-
| `HttpLogDetailLevel.HEADERS` | All of `HttpLogDetailLevel.BASIC` and request and response headers if the log level is `verbose` |
146-
| `HttpLogDetailLevel.BODY` | All of `HttpLogDetailLevel.BASIC` and request and response body if it's under 10KB in size |
147-
| `HttpLogDetailLevel.BODY_AND_HEADERS` | All of `HttpLogDetailLevel.HEADERS` and `HttpLogDetailLevel.BODY` |
148-
241+
| `HttpLogDetailLevel` value | Logging enabled |
242+
|---------------------------------------|--------------------------------------------------------------------------------------------------|
243+
| `HttpLogDetailLevel.NONE` | No HTTP request or response logging |
244+
| `HttpLogDetailLevel.BASIC` | HTTP request method, response status code, and request and response URL |
245+
| `HttpLogDetailLevel.HEADERS` | All of `HttpLogDetailLevel.BASIC` and request and response headers if the log level is `verbose` |
246+
| `HttpLogDetailLevel.BODY` | All of `HttpLogDetailLevel.BASIC` and request and response body if it's under 10KB in size |
247+
| `HttpLogDetailLevel.BODY_AND_HEADERS` | All of `HttpLogDetailLevel.HEADERS` and `HttpLogDetailLevel.BODY` |
149248

150249
## Contributing
151250

152-
For details on contributing to this repository, see the [contributing guide](https://github.com/Azure/azure-sdk-for-java/blob/main/CONTRIBUTING.md).
251+
For details on contributing to this repository, see
252+
the [contributing guide](https://github.com/Azure/azure-sdk-for-java/blob/main/CONTRIBUTING.md).
153253

154254
1. Fork it
155255
2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -158,7 +258,9 @@ For details on contributing to this repository, see the [contributing guide](htt
158258
5. Create new Pull Request
159259

160260
<!-- links -->
261+
161262
[logging]: https://learn.microsoft.com/azure/developer/java/sdk/logging-overview
263+
162264
[jdk_link]: https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable
163265

164266
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Fcore%2Fazure-core%2FREADME.png)

0 commit comments

Comments
 (0)