GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add initial opencensus instrumentation code in Cloud Storage. by HailongWen · Pull Request #2846 · googleapis/google-cloud-java

Add initial opencensus instrumentation code in Cloud Storage. #2846

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.

Sign up for GitHub

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
pongad merged 5 commits into googleapis:master from HailongWen:opencensus-instrumentation
Feb 9, 2018

Uh oh!

There was an error while loading. Please reload this page.

Merged

Add initial opencensus instrumentation code in Cloud Storage. #2846

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
136c81b
Add initial Open Census instrumentation.
HailongWen Feb 3, 2018
7a9b576
Address codacy-bot comments to remove unused imports and make fields …
HailongWen Feb 3, 2018
3ce35bd
Fix build.
HailongWen Feb 3, 2018
aec2843
Fix typo, remove unused import and enhance span status description.
HailongWen Feb 5, 2018
f14c3bb
Remove opencensus-impl dependency.
HailongWen Feb 8, 2018
File filter
Filter by extension


Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

7 changes: 6 additions & 1 deletion 7 google-cloud-bom/pom.xml
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<guava.version>20.0</guava.version>
<http-client.version>1.23.0</http-client.version>
<protobuf.version>3.5.1</protobuf.version>
<opencensus.version>0.10.0</opencensus.version>
<opencensus.version>0.11.1</opencensus.version>

<cloud.version>0.34.1-alpha-SNAPSHOT</cloud.version>
<bigtable.version>0.34.1-beta-SNAPSHOT</bigtable.version>
Expand Down Expand Up @@ -1052,6 +1052,11 @@
<artifactId>opencensus-contrib-grpc-util</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-http-util</artifactId>
<version>${opencensus.version}</version>
</dependency>

<!-- testlib deps. Only put testlib dependencies here, which are testlib packages
that correspond to another non-test package. Put pure test dependencies (e.g.
Expand Down
8 changes: 8 additions & 0 deletions 8 google-cloud-core-http/pom.xml
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,13 @@
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-http-util</artifactId>
</dependency>
</dependencies>
</project>
167 changes: 167 additions & 0 deletions 167 google-cloud-core-http/src/main/java/com/google/cloud/http/CensusHttpModule.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright 2018 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.cloud.http;

import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.http.HttpExecuteInterceptor;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.common.annotations.VisibleForTesting;
import io.opencensus.contrib.http.util.HttpPropagationUtil;
import io.opencensus.trace.SpanContext;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.propagation.TextFormat;
import java.io.IOException;
import javax.annotation.Nullable;

/**
* Provides utilities for Open Census to record http stats/trace information and propagate tracing
* context.
*/
public final class CensusHttpModule {

/**
* OpenCensus tracing component.
* When no OpenCensus implementation is provided, it will return a no-op tracer.
*/
private final Tracer tracer;

/**
* {@link TextFormat} used in tracing context propagation.
*/
@Nullable
private final TextFormat propagationTextFormat;

/**
* {@link TextFormat.Setter} for {@link #propagationTextFormat}.
*/
@Nullable
private final TextFormat.Setter<HttpHeaders> propagationTextFormatSetter;

/**
* Whether spans are stored locally.
*/
private final boolean isRecordEvents;

/**
* Default HTTP propagation text formatter.
*/
@VisibleForTesting
final static class DefaultPropagationTextFormatSetter extends TextFormat.Setter<HttpHeaders> {
static final TextFormat.Setter<HttpHeaders> INSTANCE = new DefaultPropagationTextFormatSetter();

@Override
public void put(HttpHeaders carrier, String key, String value) {
carrier.set(key, value);
}
}

/**
* An {@link HttpExecuteInterceptor} implementation to inject HTTP request and add getContext
* information before it is executed.
*/
@VisibleForTesting
final class CensusHttpExecuteInterceptor implements HttpExecuteInterceptor {
@Nullable
HttpExecuteInterceptor interceptor;

CensusHttpExecuteInterceptor(HttpExecuteInterceptor interceptor) {
this.interceptor = interceptor;
}

@Override
public void intercept(HttpRequest request) throws IOException {
checkNotNull(request);
if (this.interceptor != null) {
this.interceptor.intercept(request);
}
if (propagationTextFormat != null && propagationTextFormatSetter != null) {
SpanContext spanContext = tracer.getCurrentSpan().getContext();
if (!SpanContext.INVALID.equals(spanContext)) {
propagationTextFormat.inject(
spanContext, request.getHeaders(), propagationTextFormatSetter);
}
}
}
}

/**
* An {@link HttpRequestInitializer} implementation to set {@link CensusHttpExecuteInterceptor} as
* interceptor.
*/
@VisibleForTesting
final class CensusHttpRequestInitializer implements HttpRequestInitializer {
@Nullable
HttpRequestInitializer initializer;

CensusHttpRequestInitializer(HttpRequestInitializer initializer) {
this.initializer = initializer;
}

@Override
public void initialize(HttpRequest request) throws IOException {
checkNotNull(request);
if (this.initializer != null) {
this.initializer.initialize(request);
}
request.setInterceptor(new CensusHttpExecuteInterceptor(request.getInterceptor()));
}
}

/**
* Creates a {@link CensusHttpModule} with given parameters.
*
* @param tracer the OpenCensus {@code Tracer}.
* @param isRecordEvents whether spans are stored locally.
*/
public CensusHttpModule(Tracer tracer, boolean isRecordEvents) {
checkNotNull(tracer, "tracer");
this.tracer = tracer;
this.isRecordEvents = isRecordEvents;
this.propagationTextFormat = HttpPropagationUtil.getCloudTraceFormat();
this.propagationTextFormatSetter = DefaultPropagationTextFormatSetter.INSTANCE;
}

/**
* Returns the tracing component of OpenCensus.
*
* @return the tracing component of OpenCensus.
*/
public Tracer getTracer() {
return tracer;
}

/**
* Returns whether spans are stored locally.
*
* @return whether spans are stored locally.
*/
public boolean isRecordEvents() {
return isRecordEvents;
}

/**
* Returns the {@link HttpExecuteInterceptor} used when initializing the {@link HttpRequest}.
*
* @param initializer the original initializer which will be executed before this initializer.
* @return the {@code HttpExecuteInterceptor}.
*/
public HttpRequestInitializer getHttpRequestInitializer(HttpRequestInitializer initializer) {
return new CensusHttpRequestInitializer(initializer);
}
}
159 changes: 159 additions & 0 deletions 159 google-cloud-core-http/src/test/java/com/google/cloud/http/CensusHttpModuleTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2018 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.cloud.http;

import static com.google.common.truth.Truth.assertThat;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createMockBuilder;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import com.google.api.client.http.HttpExecuteInterceptor;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;

import io.opencensus.common.Scope;
import io.opencensus.trace.Span;
import io.opencensus.trace.SpanContext;
import io.opencensus.trace.SpanId;
import io.opencensus.trace.TraceId;
import io.opencensus.trace.TraceOptions;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.propagation.TextFormat;

import java.io.IOException;
import java.util.EnumSet;
import java.util.Random;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link CensusHttpModule}. */
@RunWith(JUnit4.class)
public class CensusHttpModuleTest {

@Rule public final ExpectedException thrown = ExpectedException.none();

private final Tracer tracer = Tracing.getTracer();
private final CensusHttpModule censusHttpModule = new CensusHttpModule(tracer, false);
private HttpRequest httpRequest;

@Before
public void setUp() throws IOException {
httpRequest = new NetHttpTransport().createRequestFactory().buildRequest(
"GET", new GenericUrl("https://www.google.com"), null);
}

@Test
public void tracerShouldNotBeNull() {
assertThat(censusHttpModule.getTracer()).isNotNull();
}

@Test
public void isRecordEventsShouldBeSet() {
assertThat(censusHttpModule.isRecordEvents()).isEqualTo(false);
}

@Test
public void getHttpRequestInitializerShouldReturnCorrectClass() {
HttpRequestInitializer initializer = censusHttpModule.getHttpRequestInitializer(null);
assertThat(initializer).isInstanceOf(CensusHttpModule.CensusHttpRequestInitializer.class);
}

@Test
public void implementationOfDefaultTextFormatSetter() {
String testKey = "testKey";
String testValue = "testValue";
TextFormat.Setter<HttpHeaders> setter =
CensusHttpModule.DefaultPropagationTextFormatSetter.INSTANCE;
setter.put(httpRequest.getHeaders(), testKey, testValue);
assertThat(httpRequest.getHeaders().get(testKey)).isEqualTo(testValue);
}

@Test
public void censusHttpExecuteInterceptorDisallowNullRequest() throws IOException {
HttpExecuteInterceptor interceptor = censusHttpModule.new CensusHttpExecuteInterceptor(null);
thrown.expect(NullPointerException.class);
interceptor.intercept(null);
}

@Test
public void censusHttpExecuteInterceptorShouldExecuteOriginal() throws IOException {
HttpExecuteInterceptor mockInterceptor = createMock(HttpExecuteInterceptor.class);
HttpExecuteInterceptor censusInterceptor = censusHttpModule.new CensusHttpExecuteInterceptor(
mockInterceptor);
mockInterceptor.intercept(httpRequest);
replay(mockInterceptor);
censusInterceptor.intercept(httpRequest);
verify(mockInterceptor);
}

@Test
public void censusHttpExecuteInterceptorShouldInjectHeader() throws IOException {
Random random = new Random();
SpanContext spanContext = SpanContext.create(
TraceId.generateRandomId(random),
SpanId.generateRandomId(random),
TraceOptions.DEFAULT);
Span mockSpan = createMockBuilder(Span.class)
.withConstructor(SpanContext.class, EnumSet.class)
.withArgs(spanContext, null)
.createMock();
Scope scope = tracer.withSpan(mockSpan);
try {
HttpExecuteInterceptor interceptor = censusHttpModule.new CensusHttpExecuteInterceptor(null);
interceptor.intercept(httpRequest);
assertThat(httpRequest.getHeaders().get("X-Cloud-Trace-Context")).isNotNull();
} finally {
scope.close();
}
}

@Test
public void censusHttpRequestInitializerDisallowNullRequest() throws IOException {
HttpRequestInitializer initializer = censusHttpModule.getHttpRequestInitializer(null);
thrown.expect(NullPointerException.class);
initializer.initialize(null);
}

@Test
public void censusHttpRequestInitializerShouldExecuteOriginal() throws IOException {
HttpRequestInitializer mockOriginalInitializer = createMock(HttpRequestInitializer.class);
HttpRequestInitializer censusInitializer = censusHttpModule.getHttpRequestInitializer(
mockOriginalInitializer);
mockOriginalInitializer.initialize(httpRequest);
replay(mockOriginalInitializer);
censusInitializer.initialize(httpRequest);
verify(mockOriginalInitializer);
}

@Test
public void censusHttpRequestInitializerShouldSetInterceptor() throws IOException {
censusHttpModule.getHttpRequestInitializer(null).initialize(httpRequest);
assertThat(httpRequest.getInterceptor()).isInstanceOf(
CensusHttpModule.CensusHttpExecuteInterceptor.class);
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.