This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit f488a03e421be6aad5717773b976768c55ccfa56 Author: wujimin <[email protected]> AuthorDate: Wed Mar 7 16:54:35 2018 +0800 SCB-369 define TagFinder that can search from tags --- foundations/foundation-metrics/pom.xml | 4 ++ .../publish/spectator/DefaultTagFinder.java | 43 +++++++++++++++ .../metrics/publish/spectator/TagFinder.java | 41 +++++++++++++++ .../publish/spectator/TestDefaultTagFinder.java | 51 ++++++++++++++++++ .../metrics/publish/spectator/TestTagFinder.java | 61 ++++++++++++++++++++++ java-chassis-dependencies/pom.xml | 5 ++ 6 files changed, 205 insertions(+) diff --git a/foundations/foundation-metrics/pom.xml b/foundations/foundation-metrics/pom.xml index 96bd6f7..baa407c 100644 --- a/foundations/foundation-metrics/pom.xml +++ b/foundations/foundation-metrics/pom.xml @@ -36,6 +36,10 @@ <artifactId>archaius-core</artifactId> </dependency> <dependency> + <groupId>com.netflix.spectator</groupId> + <artifactId>spectator-reg-servo</artifactId> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> diff --git a/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/publish/spectator/DefaultTagFinder.java b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/publish/spectator/DefaultTagFinder.java new file mode 100644 index 0000000..74601f3 --- /dev/null +++ b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/publish/spectator/DefaultTagFinder.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.servicecomb.foundation.metrics.publish.spectator; + +import com.netflix.spectator.api.Tag; + +public class DefaultTagFinder implements TagFinder { + private String tagKey; + + public DefaultTagFinder(String tagKey) { + this.tagKey = tagKey; + } + + @Override + public String getTagKey() { + return tagKey; + } + + @Override + public Tag find(Iterable<Tag> tags) { + for (Tag tag : tags) { + if (tag.key().equals(tagKey)) { + return tag; + } + } + + return null; + } +} diff --git a/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TagFinder.java b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TagFinder.java new file mode 100644 index 0000000..40c26bb --- /dev/null +++ b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TagFinder.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.servicecomb.foundation.metrics.publish.spectator; + +import com.netflix.spectator.api.Tag; + +public interface TagFinder { + static TagFinder build(Object obj) { + if (String.class.isInstance(obj)) { + return new DefaultTagFinder((String) obj); + } + + if (TagFinder.class.isInstance(obj)) { + return (TagFinder) obj; + } + + throw new IllegalArgumentException( + "only support String or TagFinder, but got " + + (obj == null ? "null" : obj.getClass().getName())); + } + + String getTagKey(); + + // read target tag from tags + // return directly or do some change and then return + Tag find(Iterable<Tag> tags); +} diff --git a/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TestDefaultTagFinder.java b/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TestDefaultTagFinder.java new file mode 100644 index 0000000..84a288a --- /dev/null +++ b/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TestDefaultTagFinder.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.servicecomb.foundation.metrics.publish.spectator; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +import com.netflix.spectator.api.BasicTag; +import com.netflix.spectator.api.Tag; + +public class TestDefaultTagFinder { + TagFinder finder = new DefaultTagFinder("key"); + + @Test + public void getTagKey() { + Assert.assertEquals("key", finder.getTagKey()); + } + + @Test + public void readSucc() { + Tag tag = new BasicTag("key", "value"); + List<Tag> tags = Arrays.asList(new BasicTag("t1", "t1v"), + tag); + + Assert.assertSame(tag, finder.find(tags)); + } + + @Test + public void readFail() { + List<Tag> tags = Arrays.asList(new BasicTag("t1", "t1v")); + + Assert.assertNull(finder.find(tags)); + } +} diff --git a/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TestTagFinder.java b/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TestTagFinder.java new file mode 100644 index 0000000..1f74e19 --- /dev/null +++ b/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/publish/spectator/TestTagFinder.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.servicecomb.foundation.metrics.publish.spectator; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class TestTagFinder { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void buildFromString() { + String name = "key"; + TagFinder finder = TagFinder.build(name); + + Assert.assertEquals(name, finder.getTagKey()); + Assert.assertEquals(DefaultTagFinder.class, finder.getClass()); + } + + @Test + public void buildFromTagFinder() { + TagFinder finder = new DefaultTagFinder("key"); + Assert.assertSame(finder, TagFinder.build(finder)); + } + + @Test + public void buildFromInvalidType() { + expectedException.expect(IllegalArgumentException.class); + expectedException + .expectMessage(Matchers.is("only support String or TagFinder, but got " + Integer.class.getName())); + + TagFinder.build(1); + } + + @Test + public void buildFromNull() { + expectedException.expect(IllegalArgumentException.class); + expectedException + .expectMessage(Matchers.is("only support String or TagFinder, but got null")); + + TagFinder.build(null); + } +} diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml index 3846dd7..8739033 100644 --- a/java-chassis-dependencies/pom.xml +++ b/java-chassis-dependencies/pom.xml @@ -358,6 +358,11 @@ </exclusions> </dependency> <dependency> + <groupId>com.netflix.spectator</groupId> + <artifactId>spectator-reg-servo</artifactId> + <version>0.62.0</version> + </dependency> + <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.0</version> -- To stop receiving notification emails like this one, please contact [email protected].
