-
Couldn't load subscription status.
- Fork 30
Telemetry handler #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/microsoft/graph/httpcore/TelemetryHandler.java
This file contains hidden or 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,38 @@ | ||
| package com.microsoft.graph.httpcore; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions; | ||
|
|
||
| import okhttp3.Interceptor; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
|
|
||
| public class TelemetryHandler implements Interceptor{ | ||
|
|
||
| public static final String SDK_VERSION = "SdkVersion"; | ||
| public static final String VERSION = "v0.1.0-SNAPSHOT"; | ||
| public static final String GRAPH_VERSION_PREFIX = "graph-java-core"; | ||
| public static final String CLIENT_REQUEST_ID = "client-request-id"; | ||
|
|
||
| @Override | ||
| public Response intercept(Chain chain) throws IOException { | ||
| Request request = chain.request(); | ||
| Request.Builder telemetryAddedBuilder = request.newBuilder(); | ||
|
|
||
| TelemetryOptions telemetryOptions = request.tag(TelemetryOptions.class); | ||
|
|
||
| if(telemetryOptions != null) { | ||
| String featureUsage = "(featureUsage=" + telemetryOptions.getFeatureUsage() + ")"; | ||
| String sdkversion_value = GRAPH_VERSION_PREFIX + "/" + VERSION + " " + featureUsage; | ||
| telemetryAddedBuilder.addHeader(SDK_VERSION, sdkversion_value); | ||
|
|
||
| if(request.header(CLIENT_REQUEST_ID) == null) { | ||
| telemetryAddedBuilder.addHeader(CLIENT_REQUEST_ID, telemetryOptions.getClientRequestId()); | ||
| } | ||
| } | ||
|
|
||
| return chain.proceed(telemetryAddedBuilder.build()); | ||
| } | ||
|
|
||
| } | ||
36 changes: 36 additions & 0 deletions
36
src/main/java/com/microsoft/graph/httpcore/middlewareoption/TelemetryOptions.java
This file contains hidden or 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,36 @@ | ||
| package com.microsoft.graph.httpcore.middlewareoption; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class TelemetryOptions { | ||
|
|
||
| public static final int NONE_FLAG = 0; | ||
| public static final int REDIRECT_HANDLER_ENABLED_FLAG = 1; | ||
| public static final int RETRY_HANDLER_ENABLED_FLAG = 2; | ||
| public static final int AUTH_HANDLER_ENABLED_FLAG = 4; | ||
| public static final int DEFAULT_HTTPROVIDER_ENABLED_FLAG = 8; | ||
| public static final int LOGGING_HANDLER_ENABLED_FLAG = 16; | ||
|
|
||
| private int featureUsage = NONE_FLAG; | ||
| private String clientRequestId; | ||
|
|
||
| public void setFeatureUsage(int flag) { | ||
| featureUsage = featureUsage | flag; | ||
| } | ||
|
|
||
| public String getFeatureUsage() { | ||
| return Integer.toHexString(featureUsage); | ||
| } | ||
|
|
||
| public void setClientRequestId(String clientRequestId) { | ||
| this.clientRequestId = clientRequestId; | ||
| } | ||
|
|
||
| public String getClientRequestId() { | ||
| if(clientRequestId == null) { | ||
| clientRequestId = UUID.randomUUID().toString(); | ||
| } | ||
| return clientRequestId; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
src/test/java/com/microsoft/graph/httpcore/TelemetryHandlerTest.java
This file contains hidden or 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,59 @@ | ||
| package com.microsoft.graph.httpcore; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import org.junit.Ignore; | ||
| import org.junit.Test; | ||
|
|
||
| import okhttp3.Interceptor; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
|
|
||
| @Ignore | ||
| public class TelemetryHandlerTest { | ||
| @Test | ||
| public void telemetryInitTest() { | ||
| TelemetryHandler telemetryHandler = new TelemetryHandler(); | ||
| assertNotNull(telemetryHandler); | ||
| } | ||
|
|
||
| @Test | ||
| public void interceptTest() throws IOException { | ||
| String expectedHeader = TelemetryHandler.SDK_VERSION + TelemetryHandler.GRAPH_VERSION_PREFIX +"/" | ||
| +TelemetryHandler.VERSION; | ||
| OkHttpClient client = HttpClients.createDefault(new ICoreAuthenticationProvider() { | ||
| @Override | ||
| public Request authenticateRequest(Request request) { | ||
| return request; | ||
| } | ||
| }); | ||
| Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); | ||
| Response response = client.newCall(request).execute(); | ||
| assertNotNull(response); | ||
| assertTrue(response.request().header("SdkVersion").contains(expectedHeader)); | ||
| } | ||
|
|
||
| @Test | ||
| public void arrayInterceptorsTest() throws IOException { | ||
|
|
||
| AuthenticationHandler authenticationHandler = new AuthenticationHandler(new ICoreAuthenticationProvider() { | ||
|
|
||
| @Override | ||
| public Request authenticateRequest(Request request) { | ||
| return request; | ||
| } | ||
| }); | ||
| Interceptor[] interceptors = {new RetryHandler(), new RedirectHandler(), authenticationHandler}; | ||
| OkHttpClient client = HttpClients.createFromInterceptors(interceptors); | ||
| String expectedHeader = TelemetryHandler.SDK_VERSION + TelemetryHandler.GRAPH_VERSION_PREFIX +"/" | ||
| +TelemetryHandler.VERSION; | ||
| Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); | ||
| Response response = client.newCall(request).execute(); | ||
| assertNotNull(response); | ||
| assertTrue(response.request().header("SdkVersion").contains(expectedHeader)); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/test/java/com/microsoft/graph/httpcore/TelemetryOptionsTest.java
This file contains hidden or 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,53 @@ | ||
| package com.microsoft.graph.httpcore; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions; | ||
|
|
||
| public class TelemetryOptionsTest { | ||
|
|
||
| @Test | ||
| public void createTelemetryOptionsTest() { | ||
| TelemetryOptions telemetryOptions = new TelemetryOptions(); | ||
| assertNotNull(telemetryOptions); | ||
| assertNotNull(telemetryOptions.getClientRequestId()); | ||
| } | ||
|
|
||
| @Test | ||
| public void setFeatureUsageTest() { | ||
| TelemetryOptions telemetryOptions = new TelemetryOptions(); | ||
| telemetryOptions.setFeatureUsage(TelemetryOptions.AUTH_HANDLER_ENABLED_FLAG); | ||
| telemetryOptions.setFeatureUsage(TelemetryOptions.REDIRECT_HANDLER_ENABLED_FLAG); | ||
| assertTrue(telemetryOptions.getFeatureUsage().compareTo("5")==0); | ||
| } | ||
|
|
||
| @Test | ||
| public void getFeatureUsageTest() { | ||
| TelemetryOptions telemetryOptions = new TelemetryOptions(); | ||
| telemetryOptions.setFeatureUsage(TelemetryOptions.AUTH_HANDLER_ENABLED_FLAG); | ||
| telemetryOptions.setFeatureUsage(TelemetryOptions.REDIRECT_HANDLER_ENABLED_FLAG); | ||
| telemetryOptions.setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG); | ||
| assertTrue(telemetryOptions.getFeatureUsage().compareTo("7")==0); | ||
| } | ||
|
|
||
| @Test | ||
| public void setClientRequestIdTest() { | ||
| TelemetryOptions telemetryOptions = new TelemetryOptions(); | ||
| telemetryOptions.setClientRequestId("test id"); | ||
| assertTrue(telemetryOptions.getClientRequestId().compareTo("test id")==0); | ||
| } | ||
|
|
||
| @Test | ||
| public void getClientRequestIdTest() { | ||
| TelemetryOptions telemetryOptions = new TelemetryOptions(); | ||
| assertNotNull(telemetryOptions.getClientRequestId()); | ||
| telemetryOptions.setClientRequestId("test id 1"); | ||
| assertTrue(telemetryOptions.getClientRequestId().compareTo("test id 1")==0); | ||
| telemetryOptions.setClientRequestId("test id 2"); | ||
| assertTrue(telemetryOptions.getClientRequestId().compareTo("test id 2")==0); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.