Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.
Merged
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {
}
dependencies {
classpath 'ru.vyarus:gradle-animalsniffer-plugin:1.4.6'
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.16'
classpath "net.ltgt.gradle:gradle-apt-plugin:0.18"
classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0'
Expand Down Expand Up @@ -168,6 +169,7 @@ subprojects {
log4j2Version = '2.11.1'
signalfxVersion = '0.0.48'
springBootVersion = '1.5.15.RELEASE'
springBootTestVersion = '2.1.1.RELEASE'
springCloudVersion = '1.3.4.RELEASE'
springVersion = '4.3.12.RELEASE'
prometheusVersion = '0.6.0'
Expand Down Expand Up @@ -223,6 +225,7 @@ subprojects {
mockito: 'org.mockito:mockito-core:1.9.5',
spring_test: "org.springframework:spring-test:${springVersion}",
truth: 'com.google.truth:truth:0.44',
spring_boot_test: "org.springframework.boot:spring-boot-starter-test:${springBootTestVersion}",
dropwizard: "io.dropwizard.metrics:metrics-core:${dropwizardVersion}",
dropwizard5: "io.dropwizard.metrics5:metrics-core:${dropwizard5Version}",
]
Expand Down
9 changes: 9 additions & 0 deletions buildscripts/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,20 @@ General guidelines on imports:
<allow pkg="io.opencensus.trace"/>
</subpackage>
<subpackage name="spring">
<allow pkg="edu.umd.cs.findbugs.annotations"/>
<allow pkg="io.opencensus.trace"/>
<allow pkg="io.opencensus.contrib.http.servlet"/>
<allow pkg="io.opencensus.contrib.spring"/>
<allow pkg="org.aspectj.lang"/>
<allow pkg="org.aspectj.lang.annotation"/>
<allow pkg="org.aspectj.lang.reflect"/>
<allow pkg="org.springframework.beans.factory.annotation"/>
<allow pkg="org.springframework.beans.factory.config"/>
<allow pkg="org.springframework.boot.autoconfigure"/>
<allow pkg="org.springframework.boot.context"/>
<allow pkg="org.springframework.context.annotation"/>
<allow pkg="org.springframework.core"/>
<allow pkg="org.springframework.stereotype"/>
<subpackage name="sleuth">
<allow pkg="io.opencensus.trace"/>
<allow pkg="org.apache.commons.logging"/>
Expand Down
10 changes: 8 additions & 2 deletions contrib/spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ apply plugin: 'java'

dependencies {
compile project(':opencensus-api'),
project(':opencensus-contrib-http-util'),
project(':opencensus-contrib-http-servlet'),
libraries.spring_aspects,
libraries.spring_context
libraries.spring_context,
libraries.spring_boot_starter_web,
libraries.spring_cloud_build,
libraries.findbugs_annotations

testCompile project(':opencensus-impl'),
project(':opencensus-testing'),
libraries.aspectj,
libraries.spring_test
libraries.spring_test,
libraries.spring_boot_test

signature "org.codehaus.mojo.signature:java17:1.0@signature"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019, OpenCensus 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.opencensus.contrib.spring.autoconfig;

import io.opencensus.common.ExperimentalApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} to
* enable tracing using OpenCensus.
*
* @since 0.23.0
*/
@Configuration
@ComponentScan(basePackages = "io.opencensus")
@ConditionalOnProperty(value = "opencensus.spring.enabled", matchIfMissing = true)
@EnableConfigurationProperties(OpenCensusProperties.class)
@ExperimentalApi
public class OpenCensusAutoConfiguration {

/**
* TRACE_FILTER_ORDER determines the order in which {@link
* io.opencensus.contrib.spring.instrument.web.HttpServletFilter} is invoked. In order to capture
* accurate request processing latency it is desirable that the filter is invoked as early as
* possible. However, there may be some security related filters that my need to execute before,
* hence +5 is added.
*/
public static final int TRACE_FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019, OpenCensus 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.opencensus.contrib.spring.autoconfig;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Opencensus settings.
*
* @since 0.23.0
*/
@ConfigurationProperties("opencensus.spring")
public class OpenCensusProperties {

private boolean enabled = true;

public boolean isEnabled() {
return this.enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019, OpenCensus 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.opencensus.contrib.spring.instrument.web;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.opencensus.contrib.http.servlet.OcHttpServletFilter;
import io.opencensus.contrib.spring.autoconfig.OpenCensusAutoConfiguration;
import javax.servlet.Filter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(OpenCensusAutoConfiguration.TRACE_FILTER_ORDER)
@SuppressFBWarnings("RI_REDUNDANT_INTERFACES")
public class HttpServletFilter extends OcHttpServletFilter implements Filter {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"properties": [
{
"name": "opencensus.spring.enabled",
"type": "java.lang.Boolean",
"description": "Enable Spring Integration Opencensus Instrumentation.",
"defaultValue": true
}
]}
3 changes: 3 additions & 0 deletions contrib/spring/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auto Configuration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.opencensus.contrib.spring.autoconfig.OpenCensusAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2019, OpenCensus 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.opencensus.contrib.spring.instrument.web;

import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@WebAppConfiguration
public abstract class AbstractMvcIntegrationTest {

@Autowired protected WebApplicationContext webApplicationContext;

protected MockMvc mockMvc;

@Before
public void setup() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/view/");
viewResolver.setSuffix(".jsp");
DefaultMockMvcBuilder mockMvcBuilder =
MockMvcBuilders.webAppContextSetup(this.webApplicationContext);
configureMockMvcBuilder(mockMvcBuilder);
this.mockMvc = mockMvcBuilder.build();
}

/**
* Override in a subclass to modify mockMvcBuilder configuration (e.g. add filter).
*
* <p>The method from super class should be called.
*/
protected void configureMockMvcBuilder(DefaultMockMvcBuilder mockMvcBuilder) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2019, OpenCensus 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.opencensus.contrib.spring.instrument.web;

import static com.google.common.truth.Truth.assertThat;

import io.opencensus.testing.export.TestHandler;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.config.TraceParams;
import io.opencensus.trace.export.SpanData;
import io.opencensus.trace.export.SpanExporter;
import io.opencensus.trace.samplers.Samplers;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RunWith(SpringRunner.class)
@SpringBootTest(
classes = HttpServletFilterIntegrationTests.Config.class,
properties = "opencensus.spring.enabled=true")
@ContextConfiguration(
locations = {"file:src/test/resources/beans/HttpServletFilterIntegrationTest-context.xml"})
public class HttpServletFilterIntegrationTests extends AbstractMvcIntegrationTest {

private TestHandler handler;

@Autowired HttpServletFilter httpServletFilter;

@Before
@Override
public void setup() {
super.setup();
handler = new TestHandler();

SpanExporter exporter = Tracing.getExportComponent().getSpanExporter();
exporter.registerHandler("testing", handler);

TraceParams params =
Tracing.getTraceConfig()
.getActiveTraceParams()
.toBuilder()
.setSampler(Samplers.alwaysSample())
.build();
Tracing.getTraceConfig().updateActiveTraceParams(params);
}

@After
public void teardown() {
SpanExporter exporter = Tracing.getExportComponent().getSpanExporter();
exporter.unregisterHandler("testing");
}

@Override
protected void configureMockMvcBuilder(DefaultMockMvcBuilder mockMvcBuilder) {
mockMvcBuilder.addFilters(this.httpServletFilter);
}

@Test(timeout = 10000)
public void shouldCreateServerTrace() throws Exception {
sendRequest();

List<SpanData> data = handler.waitForExport(1);
assertThat(data).isNotNull();
assertThat(data.size()).isEqualTo(1);
assertThat(data.get(0).getName()).isEqualTo("/foo");
}

private MvcResult sendRequest() throws Exception {
MvcResult result =
this.mockMvc
.perform(MockMvcRequestBuilders.get("/foo").accept(MediaType.TEXT_PLAIN))
.andReturn();
return result;
}

@Configuration
protected static class Config {

@RestController
public static class TestController {

@RequestMapping("/foo")
public String ping() {
return "fooResult";
}
}
}
}
3 changes: 3 additions & 0 deletions contrib/spring/src/test/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auto Configuration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.opencensus.contrib.spring.autoconfig.OpenCensusAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<bean class="io.opencensus.contrib.spring.instrument.web.HttpServletFilter"/>

</beans>