Skip to content

老板以为的Helloworld程序

About 176 wordsLess than 1 minute

可观测性

2025-02-06

老板以为你的写 Hello World 程序

public static void main(String[] args){
   
  System.out.println("hello "+args[0]);
  
}

程序员写的 Hello World 程序

  • 满足可观测性
  • 满足可用性
public static void main(String[] args){
    if(ags.length==0){
      log.error("ags is null");//记录日志
      Metric.submitEvent("hello-world-ags-error"); 
      return  ;
    }
    
    if(ags.length>1){
      log.error("ags is "+Arrays.asList(ags));
      Metric.submitEvent("hello-world-ags-error-2");
      //继续执行
    }
    Metric helloMetric = Metric.of("hello-world"); 
    try{
        
        helloMetric.start();
        System.out.println("hello "+args[0]);
        log.info("hello world success "+args[0]);
    }catch(Exception ex){
        if(MyRatelimiter.tryAcquire()){
            //阻止大量error日志打爆磁盘
            log.error("helloworld error ",ex);
        }else{
             Metric.submitEvent("too-many-error-log");
        }     
        //TODO 如何降级处理?
        helloMetric.error(ex.getMessage()); 
    }finally{
        helloMetric.end();
    }

}

知道合格程序员为啥这么累了吧😂

知行合一