Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link spring cloud gateway #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2017-2019 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.

-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>opentracing-spring-cloud-parent</artifactId>
<groupId>io.opentracing.contrib</groupId>
<version>0.3.3-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>opentracing-spring-cloud-gateway-starter</artifactId>

<properties>
<main.basedir>${project.basedir}/../..</main.basedir>
</properties>

<dependencies>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-tracer-configuration-starter</artifactId>
</dependency>

<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-web-starter</artifactId>
<version>${version.io.opentracing.contrib-opentracing-spring-web}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2017-2018 The OpenTracing Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.contrib.spring.cloud.gateway;

import io.opentracing.Tracer;
import io.opentracing.contrib.spring.cloud.gateway.propagation.TextMapAdapter;
import io.opentracing.contrib.spring.tracer.configuration.TracerAutoConfiguration;
import io.opentracing.propagation.Format;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpRequest;

@Configuration
@ConditionalOnWebApplication
@ConditionalOnBean(Tracer.class)
@AutoConfigureAfter(TracerAutoConfiguration.class)
@ConditionalOnClass(GatewayFilter.class)
@ConditionalOnProperty(name = "opentracing.spring.cloud.gateway.enabled", havingValue = "true", matchIfMissing = true)
public class GatewayAutoConfiguration {
@Bean
public GlobalFilter traceIdTransfer(Tracer tracer) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
ServerHttpRequest.Builder requestBuilder = request.mutate();

tracer.inject(tracer.activeSpan().context(), Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(request, requestBuilder));

return chain.filter(exchange.mutate().request(requestBuilder.build()).build());
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.opentracing.contrib.spring.cloud.gateway.propagation;

import io.opentracing.propagation.TextMap;
import io.opentracing.propagation.TextMapExtractAdapter;
import org.springframework.http.server.reactive.ServerHttpRequest;

public class TextMapAdapter extends TextMapExtractAdapter implements TextMap {
private ServerHttpRequest.Builder requestBuilder;

public TextMapAdapter(ServerHttpRequest serverHttpRequest, ServerHttpRequest.Builder requestBuilder) {
super(serverHttpRequest.getHeaders().toSingleValueMap());
this.requestBuilder = requestBuilder;
}

@Override
public void put(String key, String value) {
this.requestBuilder.header(key, value);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"properties": [
{
"name": "opentracing.spring.cloud.gateway.enabled",
"type": "java.lang.Boolean",
"description": "Enable Gateway tracing.",
"defaultValue": true
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.opentracing.contrib.spring.cloud.gateway.GatewayAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2017-2018 The OpenTracing Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.contrib.spring.cloud.gateway;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author <a href="mailto:[email protected]">Ales Justin</a>
*/
@RestController
public class TestController {

@RequestMapping("/hello")
public String hello() {
return "Hello";
}

@RequestMapping("/notTraced")
public String notTraced() {
return "Not traced";
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>instrument-starters/opentracing-spring-cloud-jdbc-starter</module>
<module>instrument-starters/opentracing-spring-cloud-jms-starter</module>
<module>instrument-starters/opentracing-spring-cloud-feign-starter</module>
<module>instrument-starters/opentracing-spring-cloud-gateway-starter</module>
<module>instrument-starters/opentracing-spring-cloud-mongo-starter</module>
<module>instrument-starters/opentracing-spring-cloud-websocket-starter</module>
<module>instrument-starters/opentracing-spring-cloud-zuul-starter</module>
Expand Down