This commit is contained in:
Roni Dover 2023-10-04 11:22:03 +02:00
parent 0e5c72d5ec
commit bfec3b0cd5
10 changed files with 122 additions and 62 deletions

View file

@ -11,6 +11,10 @@ extensions:
password: glc_eyJvIjoiOTQzMDk3IiwibiI6Im90bHAtb3RscCIsImsiOiJEWnhpNTFsZVZWMHJYcTg5MTVqODJOOEgiLCJtIjp7InIiOiJ1cyJ9fQ==
exporters:
otlp/jaeger:
endpoint: "jaeger:4320"
tls:
insecure: true
otlphttp/grafana:
endpoint: https://otlp-gateway-prod-us-east-0.grafana.net/otlp
auth:
@ -31,5 +35,5 @@ service:
pipelines:
traces:
receivers: [otlp]
exporters: [otlphttp/grafana,otlp/digma]
exporters: [otlphttp/grafana,otlp/digma,otlp/jaeger]
processors: [batch]

View file

@ -1,17 +1,34 @@
version: "3.6"
services:
jaeger:
image: jaegertracing/all-in-one:latest
container_name: jaeger
volumes:
- badger_data:/badger
ports:
- "16686:16686"
- "14250"
- "0.0.0.0:14268:14268"
environment:
- SPAN_STORAGE_TYPE=badger
- BADGER_EPHEMERAL=false
- BADGER_SPAN_STORE_TTL=2000h
- BADGER_DIRECTORY_VALUE=/badger/data
- BADGER_DIRECTORY_KEY=/badger/key
collector:
image: otel/opentelemetry-collector-contrib
command: ["--config=/otel-local-config.yaml"]
volumes:
- ./collector-config.yaml:/otel-local-config.yaml
ports:
- "0.0.0.0:4317:4317" # OTLP receiver
- "0.0.0.0:4318:4318" # HTTP OTLP receiver
- "0.0.0.0:8889:8889" # METRICS
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
- jaeger
environment:
- OTLP_EXPORTER_DIGMA_COLLECTOR_API=host.docker.internal:5050
@ -20,4 +37,4 @@ networks:
name: tracing-network
volumes:
badger_data:
badger_data:

33
pom.xml
View file

@ -40,14 +40,10 @@
<dependencies>
<dependency>
<groupId>org.jobrunr</groupId>
<artifactId>jobrunr-spring-boot-3-starter</artifactId>
<version>6.3.0</version>
<artifactId>jobrunr-pro-spring-boot-3-starter</artifactId>
<version>6.3.1</version>
</dependency>
<!-- Spring and Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
@ -111,11 +107,6 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<!-- caching -->
<dependency>
<groupId>javax.cache</groupId>
@ -168,6 +159,22 @@
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<version>1.26.0</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>io.github.digma-ai</groupId>
<artifactId>digma-spring-boot-micrometer-tracing-autoconf</artifactId>
<version>0.7.4</version>
</dependency>
</dependencies>
<dependencyManagement>
@ -313,6 +320,10 @@
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>JobRunrPro</id>
<url>https://repo.jobrunr.io/private-trials-202310/</url>
</repository>
</repositories>
<pluginRepositories>

View file

@ -53,6 +53,7 @@ public class OwnerValidation {
}
// This function and classes were generated by ChatGPT
@WithSpan
public boolean ValidateUserAccess(String usr, String pswd, String sysCode) {
UserNameMustStartWithR(usr);
@ -162,8 +163,41 @@ public class OwnerValidation {
}
}
@WithSpan
public void PerformValidationFlow(Owner owner) {
public boolean PerformValidationFlow(String usr, String pswd, String sysCode) {
UserNameMustStartWithR(usr);
boolean vldUsr = usrValSvc.vldtUsr(usr);
if (!vldUsr) {
return false;
}
boolean vldPswd = pwdUtils.vldtPswd(usr, pswd);
if (!vldPswd) {
return false;
}
boolean vldUsrRole = roleSvc.vldtUsrRole(usr, sysCode);
if (!vldUsrRole) {
return false;
}
boolean is2FASuccess = twoFASvc.init2FA(usr);
if (!is2FASuccess) {
return false;
}
boolean is2FATokenValid = false;
int retry = 0;
while (retry < 3 && !is2FATokenValid) {
String token = twoFASvc.getTokenInput();
is2FATokenValid = twoFASvc.vldtToken(usr, token);
retry++;
}
if (!is2FATokenValid) {
return false;
}
return true;
}

View file

@ -15,15 +15,10 @@ public class PasswordUtils {
return true;
}
@WithSpan
public String encPswd(String pswd) {
try {
Thread.sleep(300);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "";
}
}

View file

@ -1,6 +1,7 @@
package org.springframework.samples.petclinic.domain;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,19 +20,24 @@ import java.util.List;
@Component
public class PetVaccinationStatusService {
public record UpdateVaccineStatusRequest(int ownerId, int petId) {
}
@Autowired
private OwnerRepository ownerRepositorys;
@Autowired
private PetVaccinationService adapter;
@WithSpan
public void updateVaccinationStatus(List<Integer> petIds) {
public void updateVaccinationStatus(List<UpdateVaccineStatusRequest> updateVaccineStatusRequests) {
for (Integer petId : petIds) {
var pet = ownerRepositorys.findById(petId).getPet(petId);
for (UpdateVaccineStatusRequest request : updateVaccineStatusRequests) {
var owner = ownerRepositorys.findById(request.ownerId);
var pet = owner.getPet(request.petId);
try {
var vaccinationRecords = this.adapter.allVaccines();

View file

@ -63,21 +63,20 @@ class OwnerController {
dataBinder.setDisallowedFields("id");
}
@ModelAttribute("owner")
public Owner findOwner(@PathVariable(name = "ownerId", required = false) Integer ownerId) {
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
}
// @ExceptionHandler(NullPointerException.class)
// @ResponseStatus(HttpStatus.BAD_REQUEST)
// public ErrorResponse onIllegaArgumentException(RuntimeException npe){
// return ErrorResponse.builder(npe,HttpStatus.NOT_FOUND, npe.getMessage())
// .title("Not found")
// .type(URI.create("https://api.bookmarks.com/errors/not-found"))
// .build();
//
// }
// @ExceptionHandler(NullPointerException.class)
// @ResponseStatus(HttpStatus.BAD_REQUEST)
// public ErrorResponse onIllegaArgumentException(RuntimeException npe){
// return ErrorResponse.builder(npe,HttpStatus.NOT_FOUND, npe.getMessage())
// .title("Not found")
// .type(URI.create("https://api.bookmarks.com/errors/not-found"))
// .build();
//
// }
@GetMapping("/owners/new")
public String initCreationForm(Map<String, Object> model) {
@ -89,15 +88,13 @@ class OwnerController {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/owners/new")
public String processCreationForm(@Valid Owner owner, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
validator.ValidateOwnerWithExternalService(owner);
validator.PerformValidationFlow(owner);
// validator.PerformValidationFlow(owner);
validator.checkOwnerValidity(owner);
this.owners.save(owner);
@ -110,8 +107,6 @@ class OwnerController {
return "owners/findOwners";
}
@GetMapping("/owners")
public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
Model model) {

View file

@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.owner;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -50,7 +51,6 @@ class PetController implements InitializingBean {
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
private final OwnerRepository owners;
@Autowired
@ -97,7 +97,8 @@ class PetController implements InitializingBean {
}
@PostMapping("/pets/new")
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model)
throws ExecutionException, InterruptedException {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
result.rejectValue("name", "duplicate", "already exists");
}
@ -109,12 +110,18 @@ class PetController implements InitializingBean {
}
this.owners.save(owner);
var pets = owner.getPets().toArray(Pet[]::new);
// executorService.submit(
// () -> petVaccinationStatus.updateVaccinationStatus(pets));
// var pets = owner.getPets().toArray(Pet[]::new);
var petIds = owner.getPets().stream().map(Pet::getId).toList();
BackgroundJob.enqueue(() -> petVaccinationStatus.updateVaccinationStatus(petIds) );
var petRequests = owner.getPets()
.stream()
.map(x -> new PetVaccinationStatusService.UpdateVaccineStatusRequest(owner.getId(), x.getId()))
.toList();
// executorService.submit(() ->
// petVaccinationStatus.updateVaccinationStatus(petRequests)).get();
executorService.submit(() -> petVaccinationStatus.updateVaccinationStatus(petRequests));
//
// BackgroundJob.enqueue(() ->
// petVaccinationStatus.updateVaccinationStatus(petRequests));
return "redirect:/owners/{ownerId}";
}

View file

@ -5,6 +5,9 @@ spring.sql.init.data-locations=classpath*:db/${database}/data.sql
org.jobrunr.job-scheduler.enabled=true
org.jobrunr.background-job-server.enabled=true
org.jobrunr.dashboard.enabled=true
org.jobrunr.metrics.otel-observability.enabled=true
org.jobrunr.metrics.enabled=true
# Web
spring.thymeleaf.mode=HTML

View file

@ -38,24 +38,12 @@ public class OwnerControllerTests {
@LocalServerPort
private Integer port;
@BeforeAll
static void beforeAll() {
postgres.start();
}
@AfterAll
static void afterAll() {
postgres.stop();
}
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine");
@BeforeEach
void setUp() {
// ownerRepository.deleteAll();
RestAssured.baseURI = "http://localhost:" + port;
}