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

Adding HttpLogOptions for Logging improvements #5688

Merged
merged 7 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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,6 +5,7 @@

import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.http.policy.AddDatePolicy;
import com.azure.core.util.logging.ClientLogger;
import com.azure.data.appconfiguration.credentials.ConfigurationClientCredentials;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
Expand All @@ -13,7 +14,6 @@
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.policy.AddDatePolicy;
import com.azure.core.http.policy.AddHeadersPolicy;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLoggingPolicy;
Expand All @@ -22,6 +22,7 @@
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.http.policy.HttpPolicyProviders;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.implementation.util.ImplUtils;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -80,7 +81,7 @@ public final class ConfigurationClientBuilder {
private ConfigurationClientCredentials credential;
private URL endpoint;
private HttpClient httpClient;
private HttpLogDetailLevel httpLogDetailLevel;
private HttpLogOptions httpLogOptions;
private HttpPipeline pipeline;
private RetryPolicy retryPolicy;
private Configuration configuration;
Expand All @@ -89,8 +90,8 @@ public final class ConfigurationClientBuilder {
* The constructor with defaults.
*/
public ConfigurationClientBuilder() {
httpLogDetailLevel = HttpLogDetailLevel.NONE;
policies = new ArrayList<>();
httpLogOptions = new HttpLogOptions();

headers = new HttpHeaders()
.put(ECHO_REQUEST_ID_HEADER, "true")
Expand Down Expand Up @@ -164,7 +165,7 @@ public ConfigurationAsyncClient buildAsyncClient() {

policies.addAll(this.policies);
HttpPolicyProviders.addAfterRetryPolicies(policies);
policies.add(new HttpLoggingPolicy(httpLogDetailLevel));
policies.add(new HttpLoggingPolicy(httpLogOptions));

HttpPipeline pipeline = new HttpPipelineBuilder()
.policies(policies.toArray(new HttpPipelinePolicy[0]))
Expand Down Expand Up @@ -207,14 +208,15 @@ public ConfigurationClientBuilder credential(ConfigurationClientCredentials cred
}

/**
* Sets the logging level for HTTP requests and responses.
* Sets the logging configuration for HTTP requests and responses.
*
* <p> If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set.</p>
*
* @param logLevel The amount of logging output when sending and receiving HTTP requests/responses.
* @param logOptions The logging configuration to use when sending and receiving HTTP requests/responses.
* @return The updated ConfigurationClientBuilder object.
* @throws NullPointerException If {@code logLevel} is {@code null}.
*/
public ConfigurationClientBuilder httpLogDetailLevel(HttpLogDetailLevel logLevel) {
httpLogDetailLevel = Objects.requireNonNull(logLevel);
public ConfigurationClientBuilder httpLogOptions(HttpLogOptions logOptions) {
httpLogOptions = logOptions;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.data.appconfiguration.credentials.ConfigurationClientCredentials;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static void main(String[] args) throws NoSuchAlgorithmException, Invalid
final ConfigurationAsyncClient client = new ConfigurationClientBuilder()
.credential(new ConfigurationClientCredentials(connectionString))
.addPolicy(new HttpMethodRequestTrackingPolicy(tracker))
.httpLogDetailLevel(HttpLogDetailLevel.HEADERS)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.HEADERS))
.buildAsyncClient();

// Adding a couple of settings and then fetching all the settings in our repository.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.exception.ResourceNotFoundException;
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.Response;
Expand Down Expand Up @@ -39,13 +40,13 @@ protected void beforeTest() {
client = clientSetup(credentials -> new ConfigurationClientBuilder()
.credential(credentials)
.httpClient(interceptorManager.getPlaybackClient())
.httpLogDetailLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildAsyncClient());
} else {
client = clientSetup(credentials -> new ConfigurationClientBuilder()
.credential(credentials)
.httpClient(new NettyAsyncHttpClientBuilder().wiretap(true).build())
.httpLogDetailLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.addPolicy(interceptorManager.getRecordPolicy())
.addPolicy(new RetryPolicy())
.buildAsyncClient());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.azure.data.appconfiguration;

import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.data.appconfiguration.models.Range;
import com.azure.data.appconfiguration.models.SettingFields;
Expand Down Expand Up @@ -37,13 +38,13 @@ protected void beforeTest() {
client = clientSetup(credentials -> new ConfigurationClientBuilder()
.credential(credentials)
.httpClient(interceptorManager.getPlaybackClient())
.httpLogDetailLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient());
} else {
client = clientSetup(credentials -> new ConfigurationClientBuilder()
.credential(credentials)
.httpClient(new NettyAsyncHttpClientBuilder().wiretap(true).build())
.httpLogDetailLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.addPolicy(interceptorManager.getRecordPolicy())
.addPolicy(new RetryPolicy())
.buildClient());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.azure.core.annotation.QueryParam;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.implementation.entities.HttpBinFormDataJSON;
import com.azure.core.implementation.entities.HttpBinFormDataJSON.PizzaSize;
import com.azure.core.implementation.entities.HttpBinHeaders;
Expand Down Expand Up @@ -1639,7 +1640,7 @@ public void fluxUploadTest() throws Exception {
//
final HttpPipeline httpPipeline = new HttpPipelineBuilder()
.httpClient(httpClient)
.policies(new HttpLoggingPolicy(HttpLogDetailLevel.BODY_AND_HEADERS, true))
.policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)))
.build();
//
Response<HttpBinJSON> response = RestProxy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.http.policy;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
* The log configurations for HTTP messages.
*/
public class HttpLogOptions {
private HttpLogDetailLevel logLevel;
private Set<String> allowedHeaderNames;
private Set<String> allowedQueryParamNames;

public HttpLogOptions() {
logLevel = HttpLogDetailLevel.NONE;
allowedHeaderNames = new HashSet<>();
allowedQueryParamNames = new HashSet<>();
}

/**
* Gets the level of detail to log on HTTP messages.
*
* @return The {@link HttpLogDetailLevel}.
*/
public HttpLogDetailLevel getLogLevel() {
return logLevel;
}

/**
* Sets the level of detail to log on Http messages.
*
* <p> If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set.</p>
*
* @param logLevel The {@link HttpLogDetailLevel}.
* @return The updated HttpLogOptions object.
*/
public HttpLogOptions setLogLevel(final HttpLogDetailLevel logLevel) {
this.logLevel = logLevel == null ? HttpLogDetailLevel.NONE : logLevel;
return this;
}

/**
* Gets the whitelisted headers that should be logged.
*
* @return The list of whitelisted headers.
*/
public Set<String> getAllowedHeaderNames() {
return allowedHeaderNames;
}

/**
* Sets the given whitelisted headers that should be logged.
*
* @param allowedHeaderNames The list of whitelisted header names from the user.
* @return The updated HttpLogOptions object.
* @throws NullPointerException If {@code allowedHeaderNames} is {@code null}.
*/
public HttpLogOptions setAllowedHeaderNames(final Set<String> allowedHeaderNames) {
this.allowedHeaderNames = allowedHeaderNames;
return this;
}

/**
* Sets the given whitelisted header that should be logged.
*
* @param allowedHeaderName The whitelisted header name from the user.
* @return The updated HttpLogOptions object.
* @throws NullPointerException If {@code allowedHeaderName} is {@code null}.
*/
public HttpLogOptions addAllowedHeaderName(final String allowedHeaderName) {
Objects.requireNonNull(allowedHeaderName);
this.allowedHeaderNames.add(allowedHeaderName);
return this;
}

/**
* Gets the whitelisted query parameters.
*
* @return The list of whitelisted query parameters.
*/
public Set<String> getAllowedQueryParamNames() {
return allowedQueryParamNames;
}

/**
* Sets the given whitelisted query params to be displayed in the logging info.
*
* @param allowedQueryParamNames The list of whitelisted query params from the user.
* @return The updated HttpLogOptions object.
*/
public HttpLogOptions setAllowedQueryParamNames(final Set<String> allowedQueryParamNames) {
this.allowedQueryParamNames = allowedQueryParamNames;
return this;
}

/**
* Sets the given whitelisted query param that should be logged.
*
* @param allowedQueryParamName The whitelisted query param name from the user.
* @return The updated HttpLogOptions object.
* @throws NullPointerException If {@code allowedQueryParamName} is {@code null}.
*/
public HttpLogOptions addAllowedQueryParamName(final String allowedQueryParamName) {
this.allowedQueryParamNames.add(allowedQueryParamName);
return this;
}
}
Loading