Skip to content

Commit

Permalink
README_API_KEY is red from env.var, fetching headers info and readin …
Browse files Browse the repository at this point in the history
…user data from it
  • Loading branch information
Serhii Vydiuk committed Dec 11, 2024
1 parent 925639f commit febcd8b
Show file tree
Hide file tree
Showing 26 changed files with 473 additions and 238 deletions.
5 changes: 5 additions & 0 deletions packages/java/examples/OwlTestApp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.readme</groupId>
<artifactId>metrics-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.readme.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -13,6 +14,9 @@
@RestController
public class OwlController {

@Value("${readme.readmeApiKey}")
private String readmeApiKey;

private final Map<String, String> owlStorage = new HashMap<>();

public OwlController() {
Expand All @@ -21,7 +25,7 @@ public OwlController() {

@GetMapping("/owl/{id}")
public String getOwlById(@PathVariable String id) {
return "Owl with id " + id;
return "Owl with id " + id + " and key is " + readmeApiKey;
}

@GetMapping("/owls")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
readme:
readmeApiKey: ${README_API_KEY}
userdata:
apiKey:
source: header
fieldName: X-User-Name
email:
source: header
fieldName: X-User-Email
label:
source: header
fieldName: X-User-Id

This file was deleted.

32 changes: 9 additions & 23 deletions packages/java/readme-metrics-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,27 @@

<dependencies>
<dependency>
<groupId>com.readme</groupId>
<artifactId>readme-metrics</artifactId>
<version>${readme-metrics.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
<optional>true</optional>
<groupId>com.readme</groupId>
<artifactId>readme-metrics</artifactId>
<version>${readme-metrics.version}</version>
</dependency>


<!-- TESTS-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.readme.starter.config;

import com.readme.dataextraction.RequestDataCollector;
import com.readme.dataextraction.UserDataCollector;
import com.readme.starter.datacollection.DataCollectionFilter;
import com.readme.starter.datacollection.ServletDataPayloadAdapter;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
* Configuration class for registering and initializing the JakartaDataCollectionFilter
* along with its dependencies in a Spring Boot application.
* <p>
* This configuration provides the following:
* <ul>
* <li>Instantiates the {@link DataCollectionFilter} with required collectors.</li>
* <li>Registers the filter using {@link FilterRegistrationBean} for servlet-based applications.</li>
* <li>Sets up default implementations for collecting request and user data.</li>
* </ul>
*/
@Configuration
@ConditionalOnClass({UserDataProperties.class})
@ComponentScan(basePackages = {"com.readme.starter"})
@AllArgsConstructor
public class DataCollectionAutoConfiguration {

@Bean
public FilterRegistrationBean<DataCollectionFilter> metricsFilter(
RequestDataCollector<ServletDataPayloadAdapter> requestDataCollector,
UserDataCollector<ServletDataPayloadAdapter> userDataCollector) {
FilterRegistrationBean<DataCollectionFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new DataCollectionFilter(requestDataCollector, userDataCollector));
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.readme.starter.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "readme")
public class ReadmeConfigurationProperties {

private String readmeApiKey;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.readme.config.FieldMapping;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
* Configuration properties for monitoring library.
Expand All @@ -14,12 +14,12 @@
* (e.g., header, jwtClaim, or jsonBody) and its corresponding value.
* </p>
*/
@Configuration
@ConfigurationProperties(prefix = "readme.userdata")

@Data
public class MonitoringProperties {
@Component
@ConfigurationProperties(prefix = "readme.userdata")
public class UserDataProperties {

private String readmeApiKey;
private FieldMapping apiKey;
private FieldMapping email;
private FieldMapping label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

Expand All @@ -23,21 +24,29 @@
* compatibility with modern Servlet API versions.</p>
*/
@AllArgsConstructor
@Slf4j
public class DataCollectionFilter implements Filter {

private RequestDataCollector<HttpServletDataPayload> requestDataCollector;
private RequestDataCollector<ServletDataPayloadAdapter> requestDataCollector;

private UserDataCollector<HttpServletDataPayload> userDataCollector;
private UserDataCollector<ServletDataPayloadAdapter> userDataCollector;


@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletDataPayload payload =
new HttpServletDataPayload((HttpServletRequest) request, (HttpServletResponse) response);

UserData userData = userDataCollector.collect(payload);
//TODO: Validate user data. Collect request data only if user data is valid ?
requestDataCollector.collect(payload, userData);
try {
ServletDataPayloadAdapter payload =
new ServletDataPayloadAdapter((HttpServletRequest) request,
(HttpServletResponse) response);
UserData userData = userDataCollector.collect(payload);

//TODO: Validate user data. Collect request data only if user data is valid ?
requestDataCollector.collect(payload, userData);
} catch (Exception e){
log.error("Error occurred while processing request by readme metrics-sdk: {}", e.getMessage());
} finally {
chain.doFilter(request, response);
}
}

}

This file was deleted.

Loading

0 comments on commit febcd8b

Please sign in to comment.