From 52e1a9a30872cd6e41f1bf67baa13091af0237d2 Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Tue, 5 Apr 2016 13:45:28 +0300 Subject: [PATCH] Add module that provides support for discovering the zipkin query service using any discovery client. --- pom.xml | 23 +++++++ spring-cloud-kubernetes-zipkin/pom.xml | 67 +++++++++++++++++++ .../KubernetesZipkinDiscoveryProperties.java | 40 +++++++++++ .../ZipkinKubernetesAutoConfiguration.java | 53 +++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + 5 files changed, 185 insertions(+) create mode 100644 spring-cloud-kubernetes-zipkin/pom.xml create mode 100644 spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/KubernetesZipkinDiscoveryProperties.java create mode 100644 spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/ZipkinKubernetesAutoConfiguration.java create mode 100644 spring-cloud-kubernetes-zipkin/src/main/resources/META-INF/spring.factories diff --git a/pom.xml b/pom.xml index 90888bfc..678c7f66 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,8 @@ 1.3.3.RELEASE 1.0.4.RELEASE + 1.0.4.RELEASE + 1.0.0.RC1 1.0.6.RELEASE @@ -94,6 +96,7 @@ spring-cloud-kubernetes-core spring-cloud-starter-kubernetes spring-cloud-kubernetes-ribbon + spring-cloud-kubernetes-zipkin @@ -131,6 +134,15 @@ pom + + + org.springframework.cloud + spring-cloud-sleuth-dependencies + ${spring-cloud-sleuth.version} + import + pom + + org.springframework.boot spring-boot-actuator @@ -307,4 +319,15 @@ + + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone + + false + + + diff --git a/spring-cloud-kubernetes-zipkin/pom.xml b/spring-cloud-kubernetes-zipkin/pom.xml new file mode 100644 index 00000000..be35d662 --- /dev/null +++ b/spring-cloud-kubernetes-zipkin/pom.xml @@ -0,0 +1,67 @@ + + + + + + spring-cloud-kubernetes-project + io.fabric8 + 0.0.3-SNAPSHOT + + 4.0.0 + + io.fabric8 + spring-cloud-kubernetes-zipkin + Fabric8 :: Spring Cloud Kubernetes :: Zipkin + + + + + io.fabric8 + spring-cloud-kubernetes-discovery + ${project.version} + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + org.springframework.cloud + spring-cloud-commons + true + + + + org.springframework.cloud + spring-cloud-context + true + + + + org.springframework.cloud + spring-cloud-sleuth-zipkin + true + + + + + \ No newline at end of file diff --git a/spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/KubernetesZipkinDiscoveryProperties.java b/spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/KubernetesZipkinDiscoveryProperties.java new file mode 100644 index 00000000..a960c89d --- /dev/null +++ b/spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/KubernetesZipkinDiscoveryProperties.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2016 to the original 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.fabric8.spring.cloud.kubernetes.zipkin; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("spring.cloud.kubernetes.zipkin.discovery") +public class KubernetesZipkinDiscoveryProperties { + + private boolean enabled = true; + private String serviceName = "zipkin-query-api"; + + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getServiceName() { + return serviceName; + } +} diff --git a/spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/ZipkinKubernetesAutoConfiguration.java b/spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/ZipkinKubernetesAutoConfiguration.java new file mode 100644 index 00000000..8416e9cf --- /dev/null +++ b/spring-cloud-kubernetes-zipkin/src/main/java/io/fabric8/spring/cloud/kubernetes/zipkin/ZipkinKubernetesAutoConfiguration.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2016 to the original 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.fabric8.spring.cloud.kubernetes.zipkin; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinAutoConfiguration; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +@Configuration +@EnableConfigurationProperties(KubernetesZipkinDiscoveryProperties.class) +@ConditionalOnProperty(value = "spring.cloud.kubernetes.zipkin.discovery.enabled", matchIfMissing = true) +@AutoConfigureBefore(ZipkinAutoConfiguration.class) +public class ZipkinKubernetesAutoConfiguration { + + @Bean + public ZipkinSpanReporter reporter(DiscoveryClient discoveryClient, KubernetesZipkinDiscoveryProperties discoveryProperties, SpanMetricReporter spanMetricReporter, ZipkinProperties zipkin) { + String serviceName = discoveryProperties.getServiceName(); + List services = discoveryClient.getInstances(serviceName); + String serviceUrl = services.stream() + .findFirst() + .map(s -> s.getUri().toString()) + .orElseThrow(() -> new IllegalStateException("No ZipKin Query Api found with service id: [" + serviceName + "].")); + + return new HttpZipkinSpanReporter(serviceUrl, zipkin.getFlushInterval(), zipkin.getCompression().isEnabled(), spanMetricReporter); + + } +} diff --git a/spring-cloud-kubernetes-zipkin/src/main/resources/META-INF/spring.factories b/spring-cloud-kubernetes-zipkin/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..b7eb7dae --- /dev/null +++ b/spring-cloud-kubernetes-zipkin/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.fabric8.spring.cloud.kubernetes.zipkin.ZipkinKubernetesAutoConfiguration \ No newline at end of file