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

Feature/1150/1152/1168: log options, different content types and webhook #1178

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.readme.example;
package com.owl.example;

import com.readme.dataextraction.LogOptions;
import com.readme.dataextraction.payload.user.UserData;
import com.readme.dataextraction.payload.user.UserDataCollector;
import com.readme.starter.datacollection.ServletDataPayloadAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
Expand All @@ -24,15 +29,23 @@ public class CustomUserDataCollectorConfig {
//It automatically overrides the default one


// @Bean
// public UserDataCollector<ServletDataPayloadAdapter> customUserDataCollector() {
// return payloadAdapter -> {
// String apiKey = payloadAdapter.getRequestHeaders().get("x-user-name");
// return UserData.builder()
// .apiKey(apiKey)
// .email("owl@owlfactory.abc")
// .label("owl-label")
// .build();
// };
// }
@Bean
public UserDataCollector<ServletDataPayloadAdapter> customUserDataCollector() {
return payloadAdapter -> {
String apiKey = payloadAdapter.getRequestHeaders().get("x-user-name");
return UserData.builder()
.apiKey(apiKey)
.email("owl@owlfactory.abc")
.label("owl-label")
.build();
};
}

@Bean
public LogOptions logOptions() {
return LogOptions.builder()
.baseLogUrl("http://baseurl.abcd")
.bufferLength(1)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.readme.example;
package com.owl.example;

import com.readme.datatransfer.har.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -11,8 +10,8 @@
@RestController
public class OwlController {

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

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

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

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

@GetMapping("/owls")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.readme.example;
package com.owl.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down

This file was deleted.

24 changes: 24 additions & 0 deletions packages/java/examples/SpringMetricsExample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### VS Code ###
.vscode/

.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
3 changes: 3 additions & 0 deletions packages/java/examples/SpringMetricsExample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SpringMetricsExample

The application is a simple REST web-service. It is an example of using Java metrics SDK.
47 changes: 47 additions & 0 deletions packages/java/examples/SpringMetricsExample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.readme</groupId>
<artifactId>spring-metrics-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-metrics-example</name>
<description>Example project to test metrics-sdk</description>

<properties>
<java.version>21</java.version>
</properties>

<dependencies>
<dependency>
<groupId>com.readme</groupId>
<artifactId>metrics-spring</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.owl.example;

import com.readme.core.dataextraction.LogOptions;
import com.readme.core.dataextraction.payload.user.UserData;
import com.readme.core.dataextraction.payload.user.UserDataCollector;
import com.readme.spring.datacollection.ServletDataPayloadAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
* Configuration class for customizing the strategy to collect user data.
*
* <p>This configuration provides a custom implementation of {@link UserDataCollector},
* which overrides the default behavior provided by the SDK. It allows developers
* to specify their own logic for extracting user-specific information, such as API keys,
* email addresses, or labels, from the incoming HTTP requests.</p>
*
* <p>In this example, the API key is extracted from the HTTP headers using the header
* "X-User-Name", while the email and label fields are hardcoded with custom values.
* Developers can modify this logic to suit their application's requirements.</p>
*
* <p>By defining this bean, Spring Boot's auto-configuration will automatically use
* this custom implementation instead of the default {@link UserDataCollector}.</p>
*/
@Configuration
public class CustomUserDataCollectorConfig {

//Uncomment the code below to have a custom user data collection configuration.
//It automatically overrides the default one


@Bean
public UserDataCollector<ServletDataPayloadAdapter> customUserDataCollector() {
return payloadAdapter -> {
String apiKey = payloadAdapter.getRequestHeaders().get("x-user-name");
return UserData.builder()
.apiKey(apiKey)
.email("owl@owlfactory.abc")
.label("owl-label")
.build();
};
}

@Bean
public LogOptions logOptions() {
return LogOptions.builder()
.baseLogUrl("http://baseurl.abcd")
.bufferLength(1)
.allowlist(List.of("urlencoded_param2"))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.owl.example;

import com.readme.core.datatransfer.har.HttpStatus;
import jakarta.annotation.PostConstruct;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class OwlController {

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

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

public OwlController() {
owlStorage.put("1", "Default Owl");
}

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

@GetMapping("/owls")
public Collection<String> getAllOwl() {
return owlStorage.values();
}

@PutMapping("/owl/{owlName}")
public ResponseEntity<String> createOwl(@PathVariable String owlName, @RequestBody String body) {
UUID birdId = UUID.randomUUID();
owlStorage.put(birdId.toString(), owlName);

String responseBody = "Bird " + owlName + " created a bird with id: " + birdId + "\n" +
"Creation request body: \n" + body;

HttpHeaders headers = new HttpHeaders();
headers.add("bird-id", birdId.toString());
headers.add("bird-token", Base64.getEncoder()
.encodeToString(birdId.toString()
.getBytes()));

return ResponseEntity.status(HttpStatus.CREATED.getCode())
.headers(headers)
.body(responseBody);
}

@PutMapping(value = "/owl/urlencoded/{owlName}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> createOwlUrlencoded(@RequestParam Map<String, String> params) {
UUID birdId = UUID.randomUUID();

String responseBody = "Created a bird with id: " + birdId + "\n" +
"Creation request urlencoded body: \n" + params;

HttpHeaders headers = new HttpHeaders();
headers.add("bird-id", birdId.toString());
headers.add("bird-token", Base64.getEncoder()
.encodeToString(birdId.toString()
.getBytes()));

return ResponseEntity.status(HttpStatus.CREATED.getCode())
.headers(headers)
.body(responseBody);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.owl.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringMetricsExample {

public static void main(String[] args) {
SpringApplication.run(SpringMetricsExample.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.application.name=example
logging.level.com.readme=DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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

#readme:
# readmeApiKey: ${README_API_KEY}
# userdata:
# apiKey:
# source: jsonBody
# fieldName: /owl-creator/name
# email:
# source: jsonBody
# fieldName: /owl-creator/contacts/email
# label:
# source: jsonBody
# fieldName: owl-creator/label
#
#readme:
# readmeApiKey: ${README_API_KEY}
# userdata:
# apiKey:
# source: jwt
# fieldName: name
# email:
# source: jwt
# fieldName: aud
# label:
# source: jwt
# fieldName: user_id



Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.readme.webhook_example;

public class WebhookController {
}
Loading
Loading