From 881f849ca69368ee1c0848530b070122f7faacfc Mon Sep 17 00:00:00 2001 From: oibe Date: Fri, 17 Jun 2016 10:44:48 -0400 Subject: [PATCH] Add constants for standard span tags (#30) According to http://opentracing.io/data-semantics/ --- opentracing-api/pom.xml | 11 ++- .../java/io/opentracing/tag/AbstractTag.java | 28 ++++++ .../java/io/opentracing/tag/BooleanTag.java | 25 +++++ .../main/java/io/opentracing/tag/IntTag.java | 25 +++++ .../java/io/opentracing/tag/ShortTag.java | 25 +++++ .../java/io/opentracing/tag/StringTag.java | 29 ++++++ .../main/java/io/opentracing/tag/Tags.java | 91 +++++++++++++++++++ .../io/opentracing/tag/BooleanTagTest.java | 34 +++++++ .../java/io/opentracing/tag/IntTagTest.java | 34 +++++++ .../java/io/opentracing/tag/ShortTagTest.java | 33 +++++++ .../io/opentracing/tag/StringTagTest.java | 35 +++++++ opentracing-impl-java8/pom.xml | 2 +- opentracing-impl/pom.xml | 2 +- pom.xml | 2 +- 14 files changed, 372 insertions(+), 4 deletions(-) create mode 100644 opentracing-api/src/main/java/io/opentracing/tag/AbstractTag.java create mode 100644 opentracing-api/src/main/java/io/opentracing/tag/BooleanTag.java create mode 100644 opentracing-api/src/main/java/io/opentracing/tag/IntTag.java create mode 100644 opentracing-api/src/main/java/io/opentracing/tag/ShortTag.java create mode 100644 opentracing-api/src/main/java/io/opentracing/tag/StringTag.java create mode 100644 opentracing-api/src/main/java/io/opentracing/tag/Tags.java create mode 100644 opentracing-api/src/test/java/io/opentracing/tag/BooleanTagTest.java create mode 100644 opentracing-api/src/test/java/io/opentracing/tag/IntTagTest.java create mode 100644 opentracing-api/src/test/java/io/opentracing/tag/ShortTagTest.java create mode 100644 opentracing-api/src/test/java/io/opentracing/tag/StringTagTest.java diff --git a/opentracing-api/pom.xml b/opentracing-api/pom.xml index 9cfa9e86..4fa9f8cc 100644 --- a/opentracing-api/pom.xml +++ b/opentracing-api/pom.xml @@ -22,7 +22,7 @@ io.opentracing parent - 0.1.0-SNAPSHOT + 0.2.0-SNAPSHOT opentracing-api @@ -32,4 +32,13 @@ ${project.basedir}/.. + + + + org.mockito + mockito-all + 1.10.19 + test + + diff --git a/opentracing-api/src/main/java/io/opentracing/tag/AbstractTag.java b/opentracing-api/src/main/java/io/opentracing/tag/AbstractTag.java new file mode 100644 index 00000000..8feec400 --- /dev/null +++ b/opentracing-api/src/main/java/io/opentracing/tag/AbstractTag.java @@ -0,0 +1,28 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +public abstract class AbstractTag { + protected final String key; + + protected AbstractTag(String tagKey) { + this.key = tagKey; + } + + public String getKey() { + return key; + } + + abstract void set(io.opentracing.Span span, T tagValue); +} diff --git a/opentracing-api/src/main/java/io/opentracing/tag/BooleanTag.java b/opentracing-api/src/main/java/io/opentracing/tag/BooleanTag.java new file mode 100644 index 00000000..745270ad --- /dev/null +++ b/opentracing-api/src/main/java/io/opentracing/tag/BooleanTag.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +public class BooleanTag extends AbstractTag { + BooleanTag(String key) { + super(key); + } + + @Override + public void set(io.opentracing.Span span, Boolean tagValue) { + span.setTag(super.key, tagValue); + } +} diff --git a/opentracing-api/src/main/java/io/opentracing/tag/IntTag.java b/opentracing-api/src/main/java/io/opentracing/tag/IntTag.java new file mode 100644 index 00000000..d819302d --- /dev/null +++ b/opentracing-api/src/main/java/io/opentracing/tag/IntTag.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +public class IntTag extends AbstractTag { + IntTag(String key) { + super(key); + } + + @Override + public void set(io.opentracing.Span span, Integer tagValue) { + span.setTag(super.key, tagValue); + } +} diff --git a/opentracing-api/src/main/java/io/opentracing/tag/ShortTag.java b/opentracing-api/src/main/java/io/opentracing/tag/ShortTag.java new file mode 100644 index 00000000..25355c5b --- /dev/null +++ b/opentracing-api/src/main/java/io/opentracing/tag/ShortTag.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +public class ShortTag extends AbstractTag { + ShortTag(String key) { + super(key); + } + + @Override + public void set(io.opentracing.Span span, Short tagValue) { + span.setTag(super.key, tagValue); + } +} diff --git a/opentracing-api/src/main/java/io/opentracing/tag/StringTag.java b/opentracing-api/src/main/java/io/opentracing/tag/StringTag.java new file mode 100644 index 00000000..1a58cecb --- /dev/null +++ b/opentracing-api/src/main/java/io/opentracing/tag/StringTag.java @@ -0,0 +1,29 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +public class StringTag extends AbstractTag { + StringTag(String key) { + super(key); + } + + @Override + public void set(io.opentracing.Span span, String tagValue) { + span.setTag(super.key, tagValue); + } + + public void set(io.opentracing.Span span, StringTag tag) { + span.setTag(super.key, tag.key); + } +} diff --git a/opentracing-api/src/main/java/io/opentracing/tag/Tags.java b/opentracing-api/src/main/java/io/opentracing/tag/Tags.java new file mode 100644 index 00000000..6aa9f92f --- /dev/null +++ b/opentracing-api/src/main/java/io/opentracing/tag/Tags.java @@ -0,0 +1,91 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +/** + * The following span tags are recommended for instrumentors who are trying to capture more + * semantic information about the spans. Tracers may expose additional features based on these + * standardized data points. Tag names follow a general structure of namespacing. + * + * @see http://opentracing.io/data-semantics/ + */ + +public final class Tags { + private Tags(){} + + /** + * A constant for setting the span kind to indicate that it represents a server span. + */ + public static final String SPAN_KIND_SERVER = "server"; + + /** + * A constant for setting the span kind to indicate that it represents a client span. + */ + public static final String SPAN_KIND_CLIENT = "client"; + + /** + * HTTP_URL records the url of the incoming request. + */ + public static final StringTag HTTP_URL = new StringTag("http.url"); + + /** + * HTTP_STATUS records the http status code of the response. + */ + public static final IntTag HTTP_STATUS = new IntTag("http.status_code"); + + /** + * PEER_HOST_IPV4 records IPv4 host address of the peer. + */ + public static final IntTag PEER_HOST_IPV4 = new IntTag("peer.ipv4"); + + /** + * PEER_HOST_IPV6 records the IPv6 host address of the peer. + */ + public static final StringTag PEER_HOST_IPV6 = new StringTag("peer.ipv6"); + + /** + * PEER_SERVICE records the service name of the peer. + */ + public static final StringTag PEER_SERVICE = new StringTag("peer.service"); + + /** + * PEER_HOSTNAME records the host name of the peer. + */ + public static final StringTag PEER_HOSTNAME = new StringTag("peer.hostname"); + + /** + * PEER_PORT records the port number of the peer. + */ + public static final ShortTag PEER_PORT = new ShortTag("peer.port"); + + /** + * SAMPLING_PRIORITY determines the priority of sampling this Span. + */ + public static final ShortTag SAMPLING_PRIORITY = new ShortTag("sampling.priority"); + + /** + * SPAN_KIND hints at the relationship between spans, e.g. client/server. + */ + public static final StringTag SPAN_KIND = new StringTag("span.kind"); + + /** + * COMPONENT is a low-cardinality identifier of the module, library, or package that is instrumented. + */ + public static final StringTag COMPONENT = new StringTag("component"); + + /** + * ERROR indicates whether a Span ended in an error state. + */ + public static final BooleanTag ERROR = new BooleanTag("error"); +} diff --git a/opentracing-api/src/test/java/io/opentracing/tag/BooleanTagTest.java b/opentracing-api/src/test/java/io/opentracing/tag/BooleanTagTest.java new file mode 100644 index 00000000..89807d6e --- /dev/null +++ b/opentracing-api/src/test/java/io/opentracing/tag/BooleanTagTest.java @@ -0,0 +1,34 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +import io.opentracing.Span; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class BooleanTagTest { + @Test + public void testSetBoolean() { + Boolean value = true; + String key = "expected.key"; + Span span = mock(Span.class); + + BooleanTag tag = new BooleanTag(key); + tag.set(span, value); + + verify(span).setTag(key, value); + } +} diff --git a/opentracing-api/src/test/java/io/opentracing/tag/IntTagTest.java b/opentracing-api/src/test/java/io/opentracing/tag/IntTagTest.java new file mode 100644 index 00000000..6a5d48fe --- /dev/null +++ b/opentracing-api/src/test/java/io/opentracing/tag/IntTagTest.java @@ -0,0 +1,34 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +import io.opentracing.Span; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class IntTagTest { + @Test + public void testSetInt() { + Integer value = 7; + String key = "expected.key"; + Span span = mock(Span.class); + + IntTag tag = new IntTag(key); + tag.set(span, value); + + verify(span).setTag(key, value); + } +} \ No newline at end of file diff --git a/opentracing-api/src/test/java/io/opentracing/tag/ShortTagTest.java b/opentracing-api/src/test/java/io/opentracing/tag/ShortTagTest.java new file mode 100644 index 00000000..d6e77fee --- /dev/null +++ b/opentracing-api/src/test/java/io/opentracing/tag/ShortTagTest.java @@ -0,0 +1,33 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +import io.opentracing.Span; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ShortTagTest { + @Test + public void testSetShort() { + Short value = 4; + String key = "expected.key"; + + Span span = mock(Span.class); + ShortTag tag = new ShortTag(key); + tag.set(span, value); + verify(span).setTag(key, value); + } +} diff --git a/opentracing-api/src/test/java/io/opentracing/tag/StringTagTest.java b/opentracing-api/src/test/java/io/opentracing/tag/StringTagTest.java new file mode 100644 index 00000000..fd9828d4 --- /dev/null +++ b/opentracing-api/src/test/java/io/opentracing/tag/StringTagTest.java @@ -0,0 +1,35 @@ +/** + * Copyright 2016 The OpenTracing Authors + * + * 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 io.opentracing.tag; + +import io.opentracing.Span; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class StringTagTest { + + @Test + public void testSetString() { + String value = "expected.value"; + String key = "expected.key"; + + Span span = mock(Span.class); + StringTag tag = new StringTag(key); + tag.set(span, value); + + verify(span).setTag(key, value); + } +} diff --git a/opentracing-impl-java8/pom.xml b/opentracing-impl-java8/pom.xml index 233f1f44..f51fef76 100644 --- a/opentracing-impl-java8/pom.xml +++ b/opentracing-impl-java8/pom.xml @@ -22,7 +22,7 @@ io.opentracing parent - 0.1.0-SNAPSHOT + 0.2.0-SNAPSHOT opentracing-impl-java8 diff --git a/opentracing-impl/pom.xml b/opentracing-impl/pom.xml index d2b4587d..9b04ae81 100644 --- a/opentracing-impl/pom.xml +++ b/opentracing-impl/pom.xml @@ -22,7 +22,7 @@ io.opentracing parent - 0.1.0-SNAPSHOT + 0.2.0-SNAPSHOT opentracing-impl diff --git a/pom.xml b/pom.xml index 071f7cc7..45e8c0b4 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ io.opentracing parent - 0.1.0-SNAPSHOT + 0.2.0-SNAPSHOT pom