-
Notifications
You must be signed in to change notification settings - Fork 463
fix: OpenCensus spans should close on IOExceptions #797
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
8 commits
Select commit
Hold shift + click to select a range
f9ae0d8
chore: add basic test for OpenCensus spans
chingor13 f758535
chore: add failing test for closing spans on IOExceptions
chingor13 0763216
fix: close SENT span if the IOException was not handled before rethro…
chingor13 7d5ee76
relax curl logger tests for added opencensus header
chingor13 b06a0db
fix: address PR comments
chingor13 ae89bc0
chore: remove unused helper
chingor13 855db7c
fix: dependency warning for opencensus-impl
chingor13 7e2f2c7
chore: cleanup PR comments
chingor13 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
153 changes: 153 additions & 0 deletions
153
google-http-client/src/test/java/com/google/api/client/http/HttpRequestTracingTest.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,153 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.api.client.http; | ||
|
||
import com.google.api.client.testing.http.MockHttpTransport; | ||
import com.google.api.client.testing.http.MockLowLevelHttpRequest; | ||
import com.google.api.client.testing.http.MockLowLevelHttpResponse; | ||
import io.opencensus.common.Functions; | ||
import io.opencensus.testing.export.TestHandler; | ||
import io.opencensus.trace.AttributeValue; | ||
import io.opencensus.trace.MessageEvent; | ||
import io.opencensus.trace.Status; | ||
import io.opencensus.trace.Tracing; | ||
import io.opencensus.trace.config.TraceParams; | ||
import io.opencensus.trace.export.SpanData; | ||
import io.opencensus.trace.samplers.Samplers; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static com.google.api.client.http.OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
public class HttpRequestTracingTest { | ||
private static final TestHandler testHandler = new TestHandler(); | ||
|
||
@Before | ||
public void setupTestTracer() { | ||
Tracing.getExportComponent().getSpanExporter().registerHandler("test", testHandler); | ||
TraceParams params = | ||
Tracing.getTraceConfig() | ||
.getActiveTraceParams() | ||
.toBuilder() | ||
.setSampler(Samplers.alwaysSample()) | ||
.build(); | ||
Tracing.getTraceConfig().updateActiveTraceParams(params); | ||
} | ||
|
||
@After | ||
public void teardownTestTracer() { | ||
Tracing.getExportComponent().getSpanExporter().unregisterHandler("test"); | ||
} | ||
|
||
@Test(timeout = 20_000L) | ||
public void executeCreatesSpan() throws IOException { | ||
MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse() | ||
.setStatusCode(200); | ||
HttpTransport transport = new MockHttpTransport.Builder() | ||
.setLowLevelHttpResponse(mockResponse) | ||
.build(); | ||
HttpRequest request = new HttpRequestFactory(transport, null) | ||
.buildGetRequest(new GenericUrl("https://google.com/")); | ||
request.execute(); | ||
|
||
// This call blocks - we set a timeout on this test to ensure we don't wait forever | ||
List<SpanData> spans = testHandler.waitForExport(1); | ||
assertEquals(1, spans.size()); | ||
SpanData span = spans.get(0); | ||
|
||
// Ensure the span name is set | ||
assertEquals(SPAN_NAME_HTTP_REQUEST_EXECUTE, span.getName()); | ||
|
||
// Ensure we have basic span attributes | ||
assertAttributeEquals(span, "http.path", "/"); | ||
assertAttributeEquals(span, "http.host", "google.com"); | ||
assertAttributeEquals(span, "http.url", "https://google.com/"); | ||
assertAttributeEquals(span, "http.method", "GET"); | ||
|
||
// Ensure we have a single annotation for starting the first attempt | ||
assertEquals(1, span.getAnnotations().getEvents().size()); | ||
|
||
// Ensure we have 2 message events, SENT and RECEIVED | ||
assertEquals(2, span.getMessageEvents().getEvents().size()); | ||
assertEquals(MessageEvent.Type.SENT, span.getMessageEvents().getEvents().get(0).getEvent().getType()); | ||
assertEquals(MessageEvent.Type.RECEIVED, span.getMessageEvents().getEvents().get(1).getEvent().getType()); | ||
|
||
// Ensure we record the span status as OK | ||
assertEquals(Status.OK, span.getStatus()); | ||
} | ||
|
||
@Test(timeout = 20_000L) | ||
public void executeExceptionCreatesSpan() throws IOException { | ||
HttpTransport transport = new MockHttpTransport.Builder() | ||
.setLowLevelHttpRequest(new MockLowLevelHttpRequest() { | ||
@Override | ||
public LowLevelHttpResponse execute() throws IOException { | ||
throw new IOException("some IOException"); | ||
} | ||
}) | ||
.build(); | ||
HttpRequest request = new HttpRequestFactory(transport, null) | ||
.buildGetRequest(new GenericUrl("https://google.com/")); | ||
|
||
try { | ||
request.execute(); | ||
fail("expected to throw an IOException"); | ||
} catch (IOException expected) { | ||
} | ||
|
||
// This call blocks - we set a timeout on this test to ensure we don't wait forever | ||
List<SpanData> spans = testHandler.waitForExport(1); | ||
assertEquals(1, spans.size()); | ||
SpanData span = spans.get(0); | ||
|
||
// Ensure the span name is set | ||
assertEquals(SPAN_NAME_HTTP_REQUEST_EXECUTE, span.getName()); | ||
|
||
// Ensure we have basic span attributes | ||
assertAttributeEquals(span, "http.path", "/"); | ||
assertAttributeEquals(span, "http.host", "google.com"); | ||
assertAttributeEquals(span, "http.url", "https://google.com/"); | ||
assertAttributeEquals(span, "http.method", "GET"); | ||
|
||
// Ensure we have a single annotation for starting the first attempt | ||
assertEquals(1, span.getAnnotations().getEvents().size()); | ||
|
||
// Ensure we have 2 message events, SENT and RECEIVED | ||
assertEquals(1, span.getMessageEvents().getEvents().size()); | ||
assertEquals(MessageEvent.Type.SENT, span.getMessageEvents().getEvents().get(0).getEvent().getType()); | ||
|
||
// Ensure we record the span status as UNKNOWN | ||
assertEquals(Status.UNKNOWN, span.getStatus()); } | ||
|
||
void assertAttributeEquals(SpanData span, String attributeName, String expectedValue) { | ||
Object attributeValue = span.getAttributes().getAttributeMap().get(attributeName); | ||
assertNotNull("expected span to contain attribute: " + attributeName, attributeValue); | ||
assertTrue(attributeValue instanceof AttributeValue); | ||
String value = ((AttributeValue) attributeValue).match( | ||
Functions.returnToString(), | ||
Functions.returnToString(), | ||
Functions.returnToString(), | ||
Functions.returnToString(), | ||
Functions.</*@Nullable*/ String>returnNull()); | ||
assertEquals(expectedValue, value); | ||
} | ||
} |
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
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.