Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Add xdock client service #122

Merged
merged 6 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -27,6 +27,7 @@
import com.uber.jaeger.context.TraceContext;
import com.uber.jaeger.crossdock.resources.behavior.ExceptionMapper;
import com.uber.jaeger.crossdock.resources.behavior.TraceBehavior;
import com.uber.jaeger.crossdock.resources.behavior.http.EndToEndBehaviorResource;
import com.uber.jaeger.crossdock.resources.behavior.http.TraceBehaviorResource;
import com.uber.jaeger.crossdock.resources.behavior.tchannel.TChannelServer;
import com.uber.jaeger.crossdock.resources.health.HealthResource;
Expand Down Expand Up @@ -120,7 +121,7 @@ public Tracer getTracer() {

public static void main(String[] args) throws Exception {
BasicConfigurator.configure();
JerseyServer server = new JerseyServer("0.0.0.0:8081", TraceBehaviorResource.class);
JerseyServer server = new JerseyServer("0.0.0.0:8081", TraceBehaviorResource.class, EndToEndBehaviorResource.class);
TraceBehavior behavior = new TraceBehavior();
new TChannelServer(8082, behavior, server.getTracer(), false).start();
new JerseyServer("0.0.0.0:8080", HealthResource.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2017, Uber Technologies, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.crossdock.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.ToString;

import java.util.Map;

@ToString
@Getter
public class CreateTracesRequest {
private String operation;
private int count;
private Map<String, String> tags;

@JsonCreator
public CreateTracesRequest(
@JsonProperty("operation") String operation,
@JsonProperty("count") int count,
@JsonProperty("tags") Map<String, String> tags) {
this.operation = operation;
this.count = count;
this.tags = tags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2017, Uber Technologies, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.crossdock.resources.behavior;

import com.uber.jaeger.Configuration;
import com.uber.jaeger.crossdock.api.CreateTracesRequest;
import io.opentracing.Span;
import io.opentracing.Tracer;

import java.util.Map;

public class EndToEndBehavior {

private io.opentracing.Tracer tracer;

public EndToEndBehavior() {
Configuration cfg = new Configuration(
"crossdock-java",
// TODO make polling interval available for sampler
new Configuration.SamplerConfiguration("remote", 1, "test_driver:5778"),
new Configuration.ReporterConfiguration(null, "test_driver", 5775, 1000, null));
this.tracer = cfg.getTracer();
}

public EndToEndBehavior(io.opentracing.Tracer tracer) {
this.tracer = tracer;
}

public void GenerateTraces(CreateTracesRequest request) {
for (int i = 0; i < request.getCount(); i++) {
Tracer.SpanBuilder builder = tracer.buildSpan(request.getOperation());
for (Map.Entry<String, String> kv: request.getTags().entrySet()) {
builder.withTag(kv.getKey(), kv.getValue());
}
Span span = builder.start();
span.finish();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2017, Uber Technologies, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.crossdock.resources.behavior.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.uber.jaeger.crossdock.api.CreateTracesRequest;
import com.uber.jaeger.crossdock.resources.behavior.EndToEndBehavior;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;

/**
* Handles creating traces from a http request.
*
* json: {
* "operation": "operationName",
* "count": 2,
* "tags": {
* "key": "value"
* }
* }
*
* Given the above json payload, the resource will create 2 traces for the "operationName"
* operation with the tags: {"key":"value"}. These traces are reported to the agent with
* the hostname "test_driver".
*/
@Path("")
@Provider
@Slf4j
public class EndToEndBehaviorResource {
private static final Logger logger = LoggerFactory.getLogger(TraceBehaviorResource.class);

private final ObjectMapper mapper = new ObjectMapper();
private final EndToEndBehavior behavior;

public EndToEndBehaviorResource() {
this.behavior = new EndToEndBehavior();
}

public EndToEndBehaviorResource(io.opentracing.Tracer tracer) {
this.behavior = new EndToEndBehavior(tracer);
}

@POST
@Path("create_traces")
@Consumes(MediaType.APPLICATION_JSON)
public void createTraces(CreateTracesRequest request) throws Exception {
logger.info("http:create_traces request: {}", mapper.writeValueAsString(request));
behavior.GenerateTraces(request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2017, Uber Technologies, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.crossdock.resources.behavior.http;

import com.uber.jaeger.Span;
import com.uber.jaeger.crossdock.api.*;
import com.uber.jaeger.reporters.InMemoryReporter;
import com.uber.jaeger.samplers.ConstSampler;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.*;

public class EndToEndBehaviorResourceTest {
private EndToEndBehaviorResource resource;
private InMemoryReporter reporter;

@Before
public void setUp() throws Exception {
reporter = new InMemoryReporter();
io.opentracing.Tracer tracer =
new com.uber.jaeger.Tracer.Builder("crossdock-java", reporter, new ConstSampler(true))
.build();
resource = new EndToEndBehaviorResource(tracer);
}

@Test
public void testCreateTraces() throws Exception {
Map<String, String> tags = new HashMap<String, String>();
tags.put("key", "value");
CreateTracesRequest request =
new CreateTracesRequest("operation", 2, tags);

resource.createTraces(request);
validateSpans(reporter.getSpans(), request);
}

private void validateSpans(List<Span> spans, CreateTracesRequest request) {
assertEquals(request.getCount(), spans.size());
for (Span s : spans) {
assertEquals(request.getOperation(), s.getOperationName());
Map<String, Object> tags = s.getTags();
for (Map.Entry<String, String> entry : request.getTags().entrySet()) {
assertTrue(tags.containsKey(entry.getKey()));
Object value = tags.get(entry.getKey());
assertEquals(entry.getValue(), value);
}
}
}
}