Observability Done Right: Best Practices and Anti-Patterns for Effective System Monitoring

Image
  WHAT Observability is a concept that refers to the ability to gain insights into the behavior and performance of complex systems. In the context of software engineering, observability involves the collection, analysis, and visualization of data from software applications, infrastructure, and other components of a system. In the animal kingdom, observability plays a critical role in survival, allowing animals to monitor their surroundings, detect threats, and find food. Dolphins use echolocation to observe their surroundings. They emit high-frequency sounds that bounce off objects, allowing them to create a 3D map of their environment. Thanks for reading Knowledge Cafe! Subscribe for free to receive new posts and support my work. Subscribed WHY In today's era, architectures are becoming increasingly large, complex, and fast-paced due to the faster development and deployment of software by distributed teams with the help of DevOps, continuous delivery, and agile development methodo...

Comparable Vs Comparator interface

1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package.

2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.

4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.

5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Array.sort() method and object will be sorted based on there natural order defined by CompareTo method.

6)Objects which implement Comparable in Java  can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.

Comparable Sample
imports
public class Pet implements Comparable {
int petId;
String petType;
public Pet(int argPetId, String argPetType) {
petId = argPetId;
this.petType = argPetType;
}
public int compareTo(Object o) {
Pet petAnother = (Pet)o;
//natural alphabetical ordering by type
//if equal returns 0, if greater returns +ve int,
//if less returns -ve int
return this.petType.compareTo(petAnother.petType);
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Pet(2, "Dog"));
list.add(new Pet(1, "Parrot"));
list.add(new Pet(2, "Cat"));
Collections.sort(list); // sorts using compareTo method
for (Iterator iter = list.iterator(); iter.hasNext();) {
Pet element = (Pet) iter.next();
System.out.println(element);
}
}
public String toString() {
return petType;
}
}

 
Output: Cat, Dog, Parrot

Comparator Sample:
...imports
public class PetComparator implements Comparator, Serializable{
public int compare(Object o1, Object o2) {
int result = 0;
Pet pet = (Pet)o1;
Pet petAnother = (Pet)o2;
//use Integer class's natural ordering
Integer pId = new Integer(pet.getPetId());
Integer pAnotherId = new Integer(petAnother.getPetId());
result = pId.compareTo(pAnotherId);
//if ids are same compare by petType
if(result == 0) {
result= pet.getPetType().compareTo
(petAnother.getPetType());
}
return result;
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Pet(2, "Dog"));
list.add(new Pet(1, "Parrot"));
list.add(new Pet(2, "Cat"));
Collections.sort(list, new PetComparator());
for (Iterator iter = list.iterator(); iter.hasNext();){
Pet element = (Pet) iter.next();
System.out.println(element);
}
}
}
Output: Parrot, Cat, Dog.

 

 

Popular posts from this blog

Chain of responsibility using Spring @Autowired List

Iterate Through a HashMap

Under the Hood: Understanding the Gossip Protocol in Apache Cassandra