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

adding r check #8

Open
wants to merge 3 commits into
base: main
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
2 changes: 1 addition & 1 deletion .run/petcilinic-service.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<option name="ALTERNATIVE_JRE_PATH" value="17" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<envs>
<env name="OTEL_RESOURCE_ATTRIBUTES" value="digma.environment=Dev" />
<env name="OTEL_RESOURCE_ATTRIBUTES" value="digma.environment=LOAD_TEST" />
</envs>
<option name="MAIN_CLASS_NAME" value="org.springframework.samples.petclinic.PetClinicApplication" />
<module name="spring-petclinic.main" />
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ group = 'org.springframework.samples'
version = '3.0.0'
sourceCompatibility = '17'

def OPENTELEMETRY_VERSION = '1.22.1'
def OPENTELEMETRY_VERSION = '1.26.0'

repositories {
mavenLocal()
Expand Down Expand Up @@ -43,7 +43,8 @@ dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

//Enables instrumentation using @WithSpan

//Enables instrumentation using @WithSpan
implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:${OPENTELEMETRY_VERSION}")
implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:${OPENTELEMETRY_VERSION}")

Expand All @@ -65,7 +66,7 @@ task runClientTester(type: JavaExec) {
jvmArgs("-javaagent:build/otel/opentelemetry-javaagent.jar")
systemProperty("otel.service.name", "ClientTesterOfPetClinic")
systemProperty("otel.traces.exporter", "otlp")
environment("OTEL_RESOURCE_ATTRIBUTES", "digma.environment=INTEGRATION")
environment("OTEL_RESOURCE_ATTRIBUTES", "digma.environment=TEST")
systemProperty("otel.exporter.otlp.traces.endpoint", "http://localhost:5050")
environment("PETSHOP_URL", "http://localhost:9753")

Expand Down
2 changes: 1 addition & 1 deletion petshop-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0
version: 0.1.2

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.opentelemetry.instrumentation.annotations.WithSpan;
import org.springframework.samples.petclinic.owner.Owner;

import java.util.concurrent.ThreadLocalRandom;

public class OwnerValidation {

private int counter = 0;
Expand Down Expand Up @@ -34,11 +36,24 @@ public void ValidateOwnerWithExternalService(Owner owner) {
this.AuthServiceValidateUser(owner);
}

private void NewFunction(){

}
@WithSpan
// This function and classes were generated by ChatGPT to demonstrate some mock
// business logic
public boolean UserNameMustStartWithR(String usr){

if (!usr.toLowerCase().startsWith("r")){
return false;
}

return true;

}
@WithSpan
// This function and classes were generated by ChatGPT
public boolean ValidateUserAccess(String usr, String pswd, String sysCode) {

UserNameMustStartWithR(usr);
boolean vldUsr = usrValSvc.vldtUsr(usr);
if (!vldUsr) {
return false;
Expand Down Expand Up @@ -76,8 +91,9 @@ public boolean ValidateUserAccess(String usr, String pswd, String sysCode) {

@WithSpan
private synchronized void AuthServiceValidateUser(Owner owner) {
//This is the actual Root Cause!!
try {
Thread.sleep(2000 + (this.counter * 100));
Thread.sleep(2000 + (this.counter * 100));
}
catch (InterruptedException e) {
throw new RuntimeException(e);
Expand All @@ -87,15 +103,35 @@ private synchronized void AuthServiceValidateUser(Owner owner) {
@WithSpan
public boolean checkOwnerValidity(Owner owner) {

this.ValidateOwnerUserBad(owner);
return ValidateOwnerUser(owner);

}


@WithSpan
private boolean ValidateOwnerUserBad(Owner owner) {{

for (int i = 0; i < 100; i++) {
ValidateOwner();
}
return true;


}}





@WithSpan
private boolean ValidateOwnerUser(Owner owner) {

Span span = otelTracer.spanBuilder("db_access_01").startSpan();

var max = ThreadLocalRandom.current().nextInt(90, 110 + 1);
try {
for (int i = 0; i < 100; i++) {
for (int i = 0; i < max; i++) {
ValidateOwner();
}
}
Expand All @@ -106,6 +142,7 @@ private boolean ValidateOwnerUser(Owner owner) {

}

@WithSpan
private void ValidateOwner() {
// simulate SpanKind of DB query
// see
Expand All @@ -117,17 +154,19 @@ private void ValidateOwner() {
.startSpan();

try {
// delay(1);
Thread.sleep(14);
}
catch (Exception e){

}
finally {
span.end();
}
}

@WithSpan
public void PerformValidationFlow(Owner owner) {
if (owner.getPet("Jerry").isNew()) {
ValidateOwner();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class PasswordUtils {
@WithSpan
public boolean vldtPswd(String usr, String pswd) {
try {
Thread.sleep(30);
Thread.sleep(800);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springframework.samples.petclinic.order;

public class Coupon {
// Some fields...

// Constructor, getters and setters should be here...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.springframework.samples.petclinic.order;

import java.util.ArrayList;
import java.util.List;

public class Customer {
private int customerId;
private int age;
private boolean eligible;
private String address;
private double creditScore;
private double accountBalance;
private double totalSpent;
private boolean vipStatus;
private List<Coupon> coupons;
private int bonusPoints;

// Constructor, getters and setters should be here...

public boolean isEligible() {
// Logic to determine eligibility
return true;
}

public boolean hasValidAddress() {
// Logic to validate address
return true;
}

public boolean hasGoodCreditScore() {
// Logic to check credit score
return true;
}

public void addCoupon(Coupon coupon) {
if (coupons == null) {
coupons = new ArrayList<>();
}
coupons.add(coupon);
}

public void addBonusPoints(int points) {
this.bonusPoints += points;
}

public int getCustomerId() {
return 0;
}

public int getAge() {
return 0;
}

public int getAccountBalance() {
return 0;
}

public void setAccountBalance(double v) {

}

public double getTotalSpent() {
return 0;
}

public void setVipStatus(boolean b) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.order;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

public class Order {
private int orderId;
private double totalPrice;
private LocalTime time;

// Constructor, getters and setters should be here...

public double getTotalPrice() {
// Logic to calculate total price
return 0;
}

public int getOrderId() {
return 0;
}

public LocalDateTime getTime() {
return null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.springframework.samples.petclinic.order;

import org.springframework.samples.petclinic.order.Customer;
import org.springframework.samples.petclinic.order.Order;
import org.springframework.samples.petclinic.order.PaymentInfo;
import org.springframework.samples.petclinic.order.Product;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class OrderProcessor {
private final PolicyService policyService ;

public enum OrderStatus {
SUCCESS,
FAILURE,
PENDING,
CANCELLED
}

public OrderProcessor(){
this.policyService=new PolicyService();
}


public CompletableFuture<OrderStatus> processOrder(Order order, Customer customer, List<Product> productList, PaymentInfo paymentInfo) {
return CompletableFuture.supplyAsync(() -> {
if (order == null || customer == null || productList == null || productList.isEmpty() || paymentInfo == null) {
return OrderStatus.FAILURE;
}

// Initial validations
if (order.getOrderId() <= 0 || customer.getCustomerId() <= 0) {
return OrderStatus.FAILURE;
}

// Checking if the customer is eligible for ordering
if (!customer.isEligible()) {
if (customer.getAge() < policyService.GetUnderAgeValue()) {
System.out.println("Customer is under age");
return OrderStatus.FAILURE;
} else if (!customer.hasValidAddress()) {
System.out.println("Customer address is not valid");
return OrderStatus.FAILURE;
} else if (!customer.hasGoodCreditScore()) {
System.out.println("Customer credit score is not sufficient");
return OrderStatus.FAILURE;
}
}

// Checking if the products are available in stock
for (Product product : productList) {
if (product.getStock() <= 0) {
System.out.println("Product out of stock: " + product.getProductId());
return OrderStatus.FAILURE;
}
}

// Checking if the payment method is valid
if (!paymentInfo.isValid()) {
System.out.println("Payment method is invalid");
return OrderStatus.FAILURE;
}

// Checking if the customer has sufficient balance for the order
double totalOrderPrice = order.getTotalPrice();
if (customer.getAccountBalance() < totalOrderPrice) {
System.out.println("Customer does not have sufficient balance");
return OrderStatus.FAILURE;
}

// If all conditions are met, then process the order
for (Product product : productList) {
// Decrease the stock of each product asynchronously
CompletableFuture.runAsync(product::decreaseStock);
}

// Deduct the order price from the customer account balance
customer.setAccountBalance(customer.getAccountBalance() - totalOrderPrice);

// If the customer has spent more than a certain amount, give them VIP status
CompletableFuture.runAsync(() -> {
if (customer.getTotalSpent() + totalOrderPrice > this.policyService.GetVIPMinimum()) {
customer.setVipStatus(true);
}
});

// If the customer has bought a certain product, give them a coupon
productList.stream()
.filter(product -> product.getProductId() == 123)
.findAny()
.ifPresent(product -> CompletableFuture.runAsync(() -> customer.addCoupon(new Coupon())));

// If the order is placed during a certain time, add bonus points to the customer
if (order.getTime().getHour() >= 12 && order.getTime().getHour() <= 17) {
CompletableFuture.runAsync(() -> customer.addBonusPoints(100));
}

System.out.println("Order processed successfully");
return OrderStatus.SUCCESS;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.springframework.samples.petclinic.order;

public class PaymentInfo {
// Some fields...

// Constructor, getters and setters should be here...

public boolean isValid() {
// Logic to validate payment info
return true;
}
}
Loading