diff --git a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/JerseyServer.java b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/JerseyServer.java index 49fcf1ee2..553909a34 100644 --- a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/JerseyServer.java +++ b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/JerseyServer.java @@ -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; @@ -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); diff --git a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/api/CreateTracesRequest.java b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/api/CreateTracesRequest.java new file mode 100644 index 000000000..6cb502a58 --- /dev/null +++ b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/api/CreateTracesRequest.java @@ -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 tags; + + @JsonCreator + public CreateTracesRequest( + @JsonProperty("operation") String operation, + @JsonProperty("count") int count, + @JsonProperty("tags") Map tags) { + this.operation = operation; + this.count = count; + this.tags = tags; + } +} diff --git a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java new file mode 100644 index 000000000..0957aeaa2 --- /dev/null +++ b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/EndToEndBehavior.java @@ -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 kv: request.getTags().entrySet()) { + builder.withTag(kv.getKey(), kv.getValue()); + } + Span span = builder.start(); + span.finish(); + } + } +} diff --git a/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/http/EndToEndBehaviorResource.java b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/http/EndToEndBehaviorResource.java new file mode 100644 index 000000000..f5c2e38de --- /dev/null +++ b/jaeger-crossdock/src/main/java/com/uber/jaeger/crossdock/resources/behavior/http/EndToEndBehaviorResource.java @@ -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); + } +} diff --git a/jaeger-crossdock/src/test/java/com/uber/jaeger/crossdock/resources/behavior/http/EndToEndBehaviorResourceTest.java b/jaeger-crossdock/src/test/java/com/uber/jaeger/crossdock/resources/behavior/http/EndToEndBehaviorResourceTest.java new file mode 100644 index 000000000..bba77162f --- /dev/null +++ b/jaeger-crossdock/src/test/java/com/uber/jaeger/crossdock/resources/behavior/http/EndToEndBehaviorResourceTest.java @@ -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 tags = new HashMap(); + tags.put("key", "value"); + CreateTracesRequest request = + new CreateTracesRequest("operation", 2, tags); + + resource.createTraces(request); + validateSpans(reporter.getSpans(), request); + } + + private void validateSpans(List spans, CreateTracesRequest request) { + assertEquals(request.getCount(), spans.size()); + for (Span s : spans) { + assertEquals(request.getOperation(), s.getOperationName()); + Map tags = s.getTags(); + for (Map.Entry entry : request.getTags().entrySet()) { + assertTrue(tags.containsKey(entry.getKey())); + Object value = tags.get(entry.getKey()); + assertEquals(entry.getValue(), value); + } + } + } +}