• istio 日志打印 request body 和respon body


    envoyfilter.yaml

    apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
      name: reviews-lua
      namespace: default #应用的名称空间
    spec:
      workloadSelector:
        labels:
          app: crm-bms #app label
      configPatches:
      - applyTo: HTTP_FILTER
        match:
          context: SIDECAR_INBOUND
          listener:
            portNumber: 80
            filterChain:
              filter:
                name: "envoy.filters.network.http_connection_manager"
                subFilter:
                  name: "envoy.filters.http.router"
        patch:
          operation: INSERT_BEFORE
          value: 
           name: envoy.lua
           typed_config:
              "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
              inlineCode: |
                function envoy_on_request(request_handle)
                  local request_body_buffer = request_handle:body()
                  if(request_body_buffer == nil)
                  then
                    request_handle:streamInfo():dynamicMetadata():set("envoy.lua", "requestBody", "-")
                  else
                    local request_body_data = request_body_buffer:getBytes(0, request_body_buffer:length())
                    request_handle:streamInfo():dynamicMetadata():set("envoy.lua", "requestBody", request_body_data)
                  end
                end
    
                function envoy_on_response(response_handle)
                  local response_body_buffer = response_handle:body()
                  if(response_body_buffer == nil)
                  then
                    response_handle:streamInfo():dynamicMetadata():set("envoy.lua", "responseBody", "-")
                  else
                    local response_body_data = response_body_buffer:getBytes(0, response_body_buffer:length())
                    response_handle:streamInfo():dynamicMetadata():set("envoy.lua", "responseBody", response_body_data)
                  end
                end

    将以上规则应用到集群,然后修改istio配置文件,定制日志格式:    accessLogFormat: "%RESPONSE_CODE% %DYNAMIC_METADATA(envoy.lua)%\n"

    用以下命令测试:curl -d '"test":"ok"' crm-bms

    查看效果:

  • 相关阅读:
    算术异常
    MySQL和Oracle的区别
    string常用方法
    io异常
    关于null和空指针异常
    string的一些特殊点
    Mybatis中的动态SQL
    ORM框架的概述
    朴素贝叶斯分类器
    正则表达式
  • 原文地址:https://www.cnblogs.com/dufeixiang/p/15745990.html
Copyright © 2020-2023  润新知