使用apm-trace-ignore-plugin插件自定义忽略Java agent指定端点

时间:2024-12-12 19:25:48

背景信息

默认情况下,Skywalking会采集被追踪系统的所有端点(接口)。为了方便浏览UI界面,需要将一些非业务端点(接口)忽略,例如:用于K8s存活和就绪的HTTP探针接口,在我的环境中它是:/private/health。下图展示了skywalking UI界面中关于Java服务middle-gateway的/private/health端点追踪信息:

使用apm-trace-ignore-plugin插件自定义忽略Java agent指定端点_Skywaling java agent

环境说明


Skywalking server

Skywalking agent(Java)

版本

9.2.0

9.1.0

运行环境

k8s

k8s

运行方式

业务容器

sidecar容器

自定义忽略指定端点

基于背景信息,需要忽略采集Java服务middle-gateway的/private/health端点。要实现该需求,需要执行以下步骤:

安装apm-trace-ignore-plugin插件

该插件的作用是过滤预期被追踪系统忽略的端点

复制插件到指定目录

默认情况下, docker hub官方提供的Java agent镜像中在/skywalking/agent/optional-plugins/目录下已存在该插件,如图所示:

使用apm-trace-ignore-plugin插件自定义忽略Java agent指定端点_apm-trace-ignore_02

由于是可选插件,因此,默认情况下并没有激活,我们需要将其从/skywalking/agent/optional-plugins/目录复制到/skywalking/agent/plugins/目录下并添加相关配置来激活该插件。

配置需要忽略的端点

通过配置文件实现

/skywalking/agent/config/目录下,创建apm-trace-ignore-plugin.config文件并添加如下配置:

trace.ignore_path=GET:/private/health

通过jvm选项实现

在java启动命令中添加以下jvm选项来忽略指定端点

-Dskywalking.trace.ignore_path=GET:/private/health

注:该方式官方并未提供,通过查询第三方资料得知。

基于Sidecar容器的最佳实践

上面说明了如何忽略指定插件的详细步骤,接下来将展示完整的配置,重点在于initContainer中执行的Shell指令。Java服务middle-gateway的Deployment配置清单如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "10"
  labels:
    app: java
    app.kubernetes.io/instance: test01-java-middle-gateway
    chart: java-0.1.0
    heritage: Helm
    release: test01-java-middle-gateway
  name: test01-java-middle-gateway
  namespace: test
spec:
  replicas: 1
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: java
      release: test01-java-middle-gateway
  template:
    metadata:
      labels:
        app: java
        release: test01-java-middle-gateway
    spec:
      containers:
      - env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        - name: TZ
          value: Asia/Shanghai
        - name: JAVA_OPTS
          value: -Dserver.port=8080 -javaagent:/skywalking/agent/skywalking-agent.jar
        - name: BOOTOPTS
          value: --spring.profiles.active=test
        - name: JVMOPTS
          value: -XX:MinRAMPercentage=50.0 -XX:MaxRAMPercentage=50.0 -XX:+HeapDumpOnOutOfMemoryError  -XX:-OmitStackTraceInFastThrow
            -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -XX:+UseConcMarkSweepGC
            -XX:+UseParNewGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70
            -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSClassUnloadingEnabled -javaagent:/jmx_prometheus_javaagent-0.15.0.jar=8088:/prometheus-jmx-config.yaml
            -Dfile.encoding=UTF-8  -Dreactor.netty.pool.leasingStrategy=lifo -Dlog4j2.formatMsgNoLookups=true
        - name: SW_AGENT_NAME
          value: test::middle-gateway
        - name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
          value: skywalking-oap.devops.svc.cluster.local:11800
        image: xxx/middle-gateway-test01:5-b30c8e1
        imagePullPolicy: IfNotPresent
        lifecycle:
          preStop:
            httpGet:
              path: /private/shutdown
              port: 8080
              scheme: HTTP
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /private/health?secret=831B719A9DA604A889F0D236DA4E8DED
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        name: java
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /private/health?secret=831B719A9DA604A889F0D236DA4E8DED
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            memory: 1Gi
          requests:
            memory: 700Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /dumps
          name: heap-dumps
        - mountPath: /skywalking
          name: skywalking-agent
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: registry-secret-diyiyin
      initContainers:
      - args:
        - -c
        - cp /skywalking/agent/optional-plugins/apm-trace-ignore-plugin-9.1.0.jar
          /skywalking/agent/plugins/ && echo 'trace.ignore_path=${TRACE_IGNORE_PATH:GET:/private/health}'
          >> /skywalking/agent/config/apm-trace-ignore-plugin.config && cp -R /skywalking/agent
          /agent/
        command:
        - /bin/sh
        image: xxx/skywalking-java-agent:9.1.0-alpine
        imagePullPolicy: IfNotPresent
        name: agent-container
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /agent
          name: skywalking-agent
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 45
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - emptyDir: {}
        name: heap-dumps
      - emptyDir: {}
        name: skywalking-agent

重启agent

要通过插件实现忽略指定端点,除了完成上面的配置外,还需要重启Java agent,可以通过apply的方式实现。

验证

完成上述所有的操作,再次观察UI界面,查看Java服务middle-gateway中是否还显示/private/health端点信息,如果不显示则表示通过上述配置已成功自定义忽略指定端点。

使用apm-trace-ignore-plugin插件自定义忽略Java agent指定端点_apm-trace-ignore_03

注意事项

在配置忽略端点的时候,需要加上方法类型,以/private/health为例,在我的环境中需要配置成GET:/private/health,否则无法生效。

参考资料

  • 官方文档:支持自定义跟踪忽略
  • 自定义忽略指定端点(Trace Path/Endpoint)