Opentelemetry Java Agent 浅度解构
· ☕ 2 分钟

Agent initialization sequence

Agent classloader state

Conf

Creating spans around methods with otel.instrumentation.methods.include

Ref: https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/manual-instrumentation.md

Format is "java -Dotel.instrumentation.methods.include=my.package.MyClass1[method1,method2];my.package.MyClass2[method3]"

Classloader

[arthas@16908]$ classloader -t
+-BootstrapClassLoader                                                                                                 
+-io.opentelemetry.javaagent.bootstrap.AgentClassLoader@379619aa                                                       
+-sun.misc.Launcher$ExtClassLoader@41fa769c                                                                            
  +-com.taobao.arthas.agent.ArthasClassloader@3697b340                                                                 
  +-sun.misc.Launcher$AppClassLoader@18b4aac2                                                                          
    +-java.net.URLClassLoader@71b2d611                                                                                 
    +-java.net.URLClassLoader@69cd1085                                                                                 
    | +-WebAppClassLoader=266661735@fe4ef67                                                                            
    | | +-com.mycom.sig.foundation.servicediscovery.ExtendedClassLoader@573f7aae                                    
    | +-WebAppClassLoader=Server Initiated@1ccb04b3                                                                    
    | | +-com.mycom.sig.foundation.servicediscovery.ExtendedClassLoader@4b3b9a06                                    
    | | +-jnr.ffi.provider.jffi.AsmClassLoader@73cb9ccb                                                                
    | | +-jnr.ffi.provider.jffi.AsmClassLoader@7c380e94                                                                
    | | +-jnr.ffi.provider.jffi.AsmClassLoader@69ec5d1f                                                                
    | +-WebAppClassLoader=OAuth Server@10d98940                                                                        
    | | +-com.mycom.ece.common.svcfinder.ExtendedClassLoader@3528968e                                               
    | | +-com.mycom.sig.foundation.servicediscovery.ExtendedClassLoader@2919aff3                                    
    | | +-jnr.ffi.provider.jffi.AsmClassLoader@1b0e6bac                                                                
    | | +-jnr.ffi.provider.jffi.AsmClassLoader@2f12d8d1                                                                
    | | +-jnr.ffi.provider.jffi.AsmClassLoader@73123f21                                                                
    | +-WebAppClassLoader=1133988396@43974a2c                                                                          
    |   +-com.mycom.sig.foundation.servicediscovery.ExtendedClassLoader@39d87c5f                                    
    +-java.net.URLClassLoader@1b4c457c                                                                                 
    +-java.net.URLClassLoader@3a477cf5                                                                                 
Affect(row-cnt:24) cost in 31 ms.

[arthas@16908]$ classloader
 name                                                              numberOfInstances  loadedCountTotal                 
 org.eclipse.jetty.webapp.WebAppClassLoader                        4                  24687                            
 BootstrapClassLoader                                              1                  4341                             
 io.opentelemetry.javaagent.bootstrap.AgentClassLoader             1                  3502                             
 com.taobao.arthas.agent.ArthasClassloader                         1                  1430                             
 java.net.URLClassLoader                                           4                  1252                             
 sun.misc.Launcher$AppClassLoader                                  1                  570                              
 sun.reflect.DelegatingClassLoader                                 378                378                              
 sun.misc.Launcher$ExtClassLoader                                  1                  56                               
 jnr.ffi.provider.jffi.AsmClassLoader                              6                  8                                
 com.mycom.sig.foundation.servicediscovery.ExtendedClassLoader  4                  6                                
 com.mycom.ece.common.svcfinder.ExtendedClassLoader             1                  1  
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[arthas@16908]$ jad -c 1ccb04b3 com.mycom.jee.abc.client.service.payload.ClientResponse setProjectName

ClassLoader:                                                                                                           
+-WebAppClassLoader=Server Initiated@1ccb04b3                                                                          
  +-java.net.URLClassLoader@69cd1085                                                                                   
    +-sun.misc.Launcher$AppClassLoader@18b4aac2                                                                        
      +-sun.misc.Launcher$ExtClassLoader@41fa769c                                                                      

Location:                                                                                                              
/var/lib/modules/DP-OAuth-Traffic/tmp/server-initiated-aa-war.war/webapp/WEB-INF/lib/clients-api-3.19.5.jar            

            public void setProjectName(String string) {                                                                
                StackTraceElement[] stackTraceElementArray;
/*729*/         Context context = null;                                                                                
                Scope scope = null;               
                try {                                                                                                  
                    stackTraceElementArray = Thread.currentThread().getStackTrace();                                                                                                                                                          
                    context = MethodTracer.tracer().startSpan(ClientResponse.class.getMethod("setProjectName", String.class), stackTraceElementArray, new Object[]{string});
                    if (context != null) {
                        scope = context.makeCurrent();
                    }
                }
                catch (Throwable throwable) {
                    try {
                        LoggerFactory.getLogger(ExceptionLogger.class).debug("Failed to handle exception in instrumentation for com.mycom.jee.abc.client.service.payload.ClientResponse on sun.misc.Launcher$AppClassLoader@18b4aac2", thr$
wable);
                    }
                    catch (Throwable throwable2) {
                    }
                }
                stackTraceElementArray = this;
                String projectName = string;
                try {
                    this.projectName = projectName;
                    stackTraceElementArray = null;
                }
                catch (Throwable throwable) {
                    // empty catch block
                }
                try {
                    if (context != null) {
                        scope.close();
                        if (stackTraceElementArray != null) {
                            MethodTracer.tracer().endExceptionally(context, (Throwable)stackTraceElementArray);
                        } else {
                            Span span = Span.fromContext(context);
                            span.setAttribute(MethodTracer.JAVA_METHOD_RETURN.getKey(), null == null ? "null" : ((Object)null).toString());
                            MethodTracer.tracer().end(context);
                        }
                    }
                }
                catch (Throwable throwable) {
                    try {
                        LoggerFactory.getLogger(ExceptionLogger.class).debug("Failed to handle exception in instrumentation for com.mycom.jee.abc.client.service.payload.ClientResponse on sun.misc.Launcher$AppClassLoader@18b4aac2", thro
wable);
                    }
                    catch (Throwable throwable3) {
                    }
                }
                if (stackTraceElementArray != null) {
                    throw stackTraceElementArray;
                }
            }
[arthas@16908]$ sc io.opentelemetry.javaagent.instrumentation.methods.MethodInstrumentationModule
io.opentelemetry.javaagent.instrumentation.methods.MethodInstrumentationModule
Affect(row-cnt:1) cost in 54 ms.
[arthas@16908]$ sc -d io.opentelemetry.javaagent.instrumentation.methods.MethodInstrumentationModule
 class-info        io.opentelemetry.javaagent.instrumentation.methods.MethodInstrumentationModule                      
 code-source       /                                                                                                   
 name              io.opentelemetry.javaagent.instrumentation.methods.MethodInstrumentationModule                      
 isInterface       false                                                                                               
 isAnnotation      false                                                                                               
 isEnum            false                                                                                               
 isAnonymousClass  false                                                                                               
 isArray           false                                                                                               
 isLocalClass      false                                                                                               
 isMemberClass     false                                                                                               
 isPrimitive       false                                                                                               
 isSynthetic       false                                                                                               
 simple-name       MethodInstrumentationModule                                                                         
 modifier          public                                                                                              
 annotation                                                                                                            
 interfaces                                                                                                            
 super-class       +-io.opentelemetry.javaagent.tooling.InstrumentationModule                                          
                     +-java.lang.Object                                                                                
 class-loader      +-io.opentelemetry.javaagent.bootstrap.AgentClassLoader@379619aa                                    
 classLoaderHash   379619aa 
[arthas@16908]$ sc -d io.opentelemetry.javaagent.instrumentation.methods.MethodTracer
 class-info        io.opentelemetry.javaagent.instrumentation.methods.MethodTracer                                     
 code-source                                                                                                           
 name              io.opentelemetry.javaagent.instrumentation.methods.MethodTracer                                     
 isInterface       false                                                                                               
 isAnnotation      false                                                                                               
 isEnum            false                                                                                               
 isAnonymousClass  false                                                                                               
 isArray           false                                                                                               
 isLocalClass      false                                                                                               
 isMemberClass     false                                                                                               
 isPrimitive       false                                                                                               
 isSynthetic       false                                                                                               
 simple-name       MethodTracer                                                                                        
 modifier          public                                                                                              
 annotation                                                                                                            
 interfaces                                                                                                            
 super-class       +-io.opentelemetry.javaagent.shaded.instrumentation.api.tracer.BaseTracer                           
                     +-java.lang.Object                                                                                
 class-loader      +-WebAppClassLoader=Server Initiated@1ccb04b3                                                       
                     +-java.net.URLClassLoader@69cd1085                                                                
                       +-sun.misc.Launcher$AppClassLoader@18b4aac2                                                     
                         +-sun.misc.Launcher$ExtClassLoader@41fa769c                                                   
 classLoaderHash   1ccb04b3                                                                                            

 class-info        io.opentelemetry.javaagent.instrumentation.methods.MethodTracer                                     
 code-source                                                                                                           
 name              io.opentelemetry.javaagent.instrumentation.methods.MethodTracer                                     
 isInterface       false                                                                                               
 isAnnotation      false                                                                                               
 isEnum            false                                                                                               
 isAnonymousClass  false                                                                                               
 isArray           false                                                                                               
 isLocalClass      false                                                                                               
 isMemberClass     false                                                                                               
 isPrimitive       false                                                                                               
 isSynthetic       false                                                                                               
 simple-name       MethodTracer                                                                                        
 modifier          public                                                                                              
 annotation                                                                                                            
 interfaces                                                                                                            
 super-class       +-io.opentelemetry.javaagent.shaded.instrumentation.api.tracer.BaseTracer                           
                     +-java.lang.Object                                                                                
 class-loader      +-WebAppClassLoader=OAuth Server@10d98940                                                           
                     +-java.net.URLClassLoader@69cd1085                                                                
                       +-sun.misc.Launcher$AppClassLoader@18b4aac2                                                     
                         +-sun.misc.Launcher$ExtClassLoader@41fa769c                                                   
 classLoaderHash   10d98940                                                                                            

shaded

io.opentelemetry.instrumentation.api.config.Config
->
sc -d io.opentelemetry.javaagent.shaded.instrumentation.api.config.Config

[arthas@16908]$ sc -d io.opentelemetry.javaagent.shaded.instrumentation.api.config.Config                                                                                            

 class-info        io.opentelemetry.javaagent.shaded.instrumentation.api.config.Config                                 
 code-source                                                                                                           
 name              io.opentelemetry.javaagent.shaded.instrumentation.api.config.Config                                 
 isInterface       false                                                                                               
 isAnnotation      false                                                                                               
 isEnum            false                                                                                               
 isAnonymousClass  false                                                                                               
 isArray           false                                                                                               
 isLocalClass      false                                                                                               
 isMemberClass     false                                                                                               
 isPrimitive       false                                                                                               
 isSynthetic       false                                                                                               
 simple-name       Config                                                                                              
 modifier          abstract,public                                                                                     
 annotation                                                                                                            
 interfaces                                                                                                            
 super-class       +-java.lang.Object                                                                                  
 class-loader                                                                                                          
 classLoaderHash   null 

K8s Custom Resources(CR)
· ☕ 2 分钟

Custom Resource 的入口

请求是这样分发到 api 扩展点的:

image-20210327224709520

例如我们有 (Custom Resource)CR

1
2
3
4
5
6
7
8
apiVersion: cnat.programming-kubernetes.info/v1alpha1
kind: At
metadata:
  name: example-at
spec:
  schedule: "2019-07-03T02:00:00Z"
status:
  phase: "pending"

相应的 CustomResourceDefinition (CRD) 会是这样:


K8s API 核心对象 —— client-go
· ☕ 3 分钟

API 入口

Client Sets

接收变更通知和缓存(Informers and Caching)

Client Sets可以 watch 变更,但一般我们用更高级的 Informers,因为它有缓存、索引等功能。

image-20210326154940269

  • Lister :被应用调用,返回缓存中的数据列表
  • Informer:监听器

Informer 有两个功能


Helm base
· ☕ 1 分钟

Concept

umbrella chart

you can also create a chart with dependencies to other charts (a.k.a. umbrella chart) which are completely external using the requirements.yaml file.

image-20210325143339901

versioning

Simple 1-1 versioning

Synced versions in Helm

Chart versus application versioning

Independent Helm versioning

参考

https://codefresh.io/docs/docs/new-helm/helm-best-practices/


Kubernetes extends concept
· ☕ 3 分钟

前言

最近由于工作需要,重新系统回顾 Kubernetes 的编程。发现《Programming Kubernetes》这书写得比较系统。于是边学,边记录一些重点。

Controller

Controller Loop

image-20210325162149792


Kubernetes 自动扩缩容
· ☕ 1 分钟

本文状态:草稿

image-20210323153341945

配置例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

算法

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

currentMetricValue 为相关 pod 的 metric 平均数。


内核同步原语
· ☕ 5 分钟

image-20210321163632623

什么是同步原语

共享内存,多进程/线程的运行期设计模式已成主流的今天,你有好奇一下,进程/线程间的怎么同步的吗?大部分人知道,我们用的开发语言,或类库函数库,已经提供了看起来很漂亮的封装。然而在漂亮的面子工程后面,大部分归根到底是要内核 或/和 CPU 硬件去完成这些同步的。而反过来,只要我们理解了内部原理,你就可以快速理解那些漂亮的面子工程,和他们可能的性能问题,进而选择一个适合你的“面子工程”。而这些内部原理,就是同步原语。


Istio Canary(金丝雀) 上线
· ☕ 1 分钟

按比例分配分配新旧版本流量

VirtualService:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 75
    - destination:
        host: reviews
        subset: v2
      weight: 25

DestinationRule :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: bookinfo-ratings
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN
  subsets:
  - name: v1
    labels:
      version: v1
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN
  - name: v2
    labels:
      version: v2
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN

根据请求源 pod 的 label 路由

使用 sourceLabels 规则,可以根据源 pod 的 label 进行路由。这里用了 version 这个 label。即根据pod的应用版本进行路由。
这样的路由规则实际是使用于发起调用方的 sidecar。


程序员的平行宇宙 —— eBPF 系统级跟踪技术简单入门
· ☕ 5 分钟

image-20210310223153844

Linus Torvalds in 1991

程序员的平行宇宙

程序员有两个世界:

  1. 一个是编码世界,我们很容易认为,我们考虑了一切,也完成了一切的代码。
  2. 然后是运行世界,我们发现,无论我们多么的严谨和考虑一切,世界总是有异常。异常就好像电磁波,一开始,我们只能通过它引发的结果而发现它,而不能直接观察。因为它总出现在那些黑暗的角落,如果我们手中没有电简和电磁波示波器,一切只能依靠猜测和迷信。最后,我们进入了一个用猜测驱动的世界。有时候,我们运气好,猜中了。有时候,我们只能在 release 前多上香。

It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts.


系统级跟踪 eBPF 工具 —— bpftrace 入门
· ☕ 1 分钟

bpftrace 简介

bpftrace 简单使用

查询可以跟踪的内核函数,以 sleep 为关键字

1
2
3
4
5
6
7
8
$ bpftrace -l '*open*'

tracepoint:syscalls:sys_exit_open_tree
tracepoint:syscalls:sys_enter_open
...
kprobe:vfs_open
kprobe:tcp_try_fastopen
...

跟踪所有 sys_enter_open() 系统调用

1
$ bpftrace -e 'tracepoint:syscalls:sys_enter_open{ printf("%s %s\n", comm,str(args->filename)); }' | grep vi

然后在另外一个终端中

1
$ vi /etc/hosts

可以看到在 bpftrace 终端中输出:


Kernel - Page Frame 回收
· ☕ 4 分钟

From [Understanding The Linux Kernel]

Page Frame 回收

之前我们了解到,Linux 倾向用最多的内存做 Page Cache。这使我们不得不考虑如何在内存不足前回收内存。问题是,回收内存的程序本身也可能有 IO 操作,也可能需要内存。


Kernel - Pagecache
· ☕ 1 分钟

简介

page cache 存放的数据的类型

  • 普通的文件
  • 目录数据
  • 直接读取自 block device file 的数据
  • 已经被swap out的用户进程内存的数据(可以强制内核在page cahce中保留一些已经被swap out的数据)
  • 归属于一些特殊 filesystem 的内存 page,如用于进程间通讯的 shm filesystem

page cache 的标识体系

page cache 中的每个 page 均归属于文件. 这个文件 — 或更精确来说,是文件的 inode 被称为 page 的owner.


Kernel - Pagecache - Core
· ☕ 3 分钟

address_space 数据结构

Page cahce 的核心数据结构是 addrees_space。一般来说,每个 inode (Kernel 用来存放文件元信息的内存中的数据结构,可以视为一个文件的描述信息)中包含一个 addrees_space