Skip to content

Commit

Permalink
first functioning version, still needs lots of polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
fabito committed Dec 21, 2016
1 parent 88c2210 commit 8af5b22
Show file tree
Hide file tree
Showing 43 changed files with 1,497 additions and 172 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# dropwizard-gcp

[![Build Status][travis-image]][travis-url]

Dropwizard libraries for applications running on Google Cloud Platform

* logging
* tracing
* metrics

[travis-image]: https://travis-ci.org/fabito/dropwizard-gcp.svg?branch=master
[travis-url]: https://travis-ci.org/fabito/dropwizard-gcp
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ subprojects {
repositories {
mavenCentral()
jcenter()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

project.ext {
dropwizardVersion = '1.0.5'
dropwizardVersion = '1.1.0-SNAPSHOT'
}
}

Expand Down Expand Up @@ -71,4 +72,4 @@ subprojects {

task wrapper(type: Wrapper) {
gradleVersion = '3.2.1'
}
}
25 changes: 25 additions & 0 deletions dropwizard-stackdriver-trace/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apply plugin: 'java'

sourceCompatibility = 1.8

project.ext {
cloudTrace = "0.2.2"
}

dependencies {

compile "io.dropwizard:dropwizard-client:${dropwizardVersion}"
compile ('org.glassfish.hk2:hk2-extras:2.5.0-b30')

compile group: 'com.google.cloud.trace', name: 'trace-grpc-api-service', version: cloudTrace
compile group: 'com.google.cloud.trace', name: 'core', version: cloudTrace
compile group: 'com.google.cloud.trace', name: 'annotation', version: cloudTrace
compile group: 'com.google.cloud.trace', name:'guice-annotation', version: cloudTrace

compile group: 'com.google.auth', name: 'google-auth-library-oauth2-http', version: '0.4.0'

compile group: 'io.grpc', name: 'grpc-netty', version: '1.0.1'
compile group: 'io.netty', name: 'netty-tcnative-boringssl-static', version: '1.1.33.Fork23'

testCompile group: 'junit', name: 'junit', version: '4.11'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

/**
* Common HTTP header constants.
*/
class HttpHeaders {
static final String USER_AGENT = "User-Agent";
static final String CONTENT_LENGTH = "Content-Length";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

/**
* Common HTTP label constants.
*/
public class HttpLabels {
public static final String REQUEST_SIZE = "/request/size";
public static final String RESPONSE_SIZE = "/response/size";
public static final String HTTP_METHOD = "/http/method";
public static final String HTTP_STATUS_CODE = "/http/status_code";
public static final String HTTP_URL = "/http/url";
public static final String HTTP_HOST = "/http/host";
public static final String HTTP_REDIRECTED_URL = "/http/redirected_url";
public static final String HTTP_USER_AGENT = "/http/user_agent";
public static final String HTTP_CLIENT_PROTOCOL = "/http/client_protocol";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

import java.net.URI;

/**
* Common interface for HTTP requests.
*/
public interface HttpRequest {

/**
* The HTTP method of the HTTP request..
* @return The HTTP method.
*/
String getMethod();

/**
* The URI being requested.
* @return The URI being requested.
*/
URI getURI();

/**
* Get a header value.
* @param name The name of the header.
* @return The value of the header.
*/
String getHeader(String name);

/**
* The HTTP protocol being used by the client (e.g. HTTP, SPDY).
* @return The HTTP protocol being used.
*/
String getProtocol();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

/**
* Common interface for HTTP responses.
*/
public interface HttpResponse {

/**
* Get a header value.
* @param name The name of the header.
* @return The value of the header.
*/
String getHeader(String name);

/**
* Get the status code.
* @return The status code from the response.
*/
int getStatus();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

import com.google.cloud.trace.Trace;
import com.google.cloud.trace.Tracer;
import com.google.cloud.trace.core.Labels;
import com.google.cloud.trace.core.TraceContext;

/**
* An interceptor that records tracing information for HTTP requests. Should be used along with
* {@link TraceHttpResponseInterceptor}.
*/
public class TraceHttpRequestInterceptor {

private final Tracer tracer;

public TraceHttpRequestInterceptor() {
this(Trace.getTracer());
}

public TraceHttpRequestInterceptor(Tracer tracer) {
this.tracer = tracer;
}

/**
* Starts a span for an HTTP request and record relevant labels.
* @param request The HTTP request.
* @return The new TraceContext
*/
public TraceContext process(HttpRequest request) {
Labels.Builder labels = Labels.builder();
TraceInterceptorUtil
.annotateIfNotEmpty(labels, HttpLabels.HTTP_METHOD, request.getURI().toString());
labels.add(HttpLabels.HTTP_METHOD, request.getMethod());
TraceInterceptorUtil
.annotateIfNotEmpty(labels, HttpLabels.HTTP_URL, request.getURI().toString());
TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.HTTP_CLIENT_PROTOCOL,
request.getProtocol());
TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.HTTP_USER_AGENT,
request.getHeader(HttpHeaders.USER_AGENT));
TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.REQUEST_SIZE,
request.getHeader(HttpHeaders.CONTENT_LENGTH));
TraceContext traceContext = tracer.startSpan(request.getURI().getPath());
tracer.annotateSpan(traceContext, labels.build());
return traceContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

import com.google.cloud.trace.Trace;
import com.google.cloud.trace.Tracer;
import com.google.cloud.trace.core.Labels;
import com.google.cloud.trace.core.TraceContext;

/**
* An interceptor that records tracing information for HTTP responses. Should be used along with
* {@link TraceHttpRequestInterceptor}.
*/
public class TraceHttpResponseInterceptor {
private final Tracer tracer;

public TraceHttpResponseInterceptor() {
this(Trace.getTracer());
}

public TraceHttpResponseInterceptor(Tracer tracer) {
this.tracer = tracer;
}

/**
* Ends a span for an HTTP request and records relevant labels.
* @param response The HTTP response.
* @param traceContext The TraceContext for the request.
*/
public void process(HttpResponse response, TraceContext traceContext) {
if (traceContext == null) {
return;
}
Labels.Builder labels = Labels.builder();
TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.RESPONSE_SIZE,
response.getHeader(HttpHeaders.CONTENT_LENGTH));
labels.add(HttpLabels.HTTP_STATUS_CODE, Integer.toString(response.getStatus()));
tracer.annotateSpan(traceContext, labels.build());
tracer.endSpan(traceContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.trace.http;

import com.google.cloud.trace.core.Labels;

class TraceInterceptorUtil {
static void annotateIfNotEmpty(Labels.Builder labels, String key, String value) {
if (value != null && value.length() > 0) {
labels.add(key, value);
}
}
}
Loading

0 comments on commit 8af5b22

Please sign in to comment.