# agent 引入

agent 版本需要对应 project 中的依赖 version log4shell 相关漏洞在 8.9.0 以上版本修复的 所以推荐选用 8.9.0 或 9.0.0 以上版本

# java 应用开发

使用 java 进行应用开发时 可以直接使用以下 pom 引入相关依赖

<dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-log4j-2.x</artifactId>
            <version>${skywalking-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-trace</artifactId>
            <version>${skywalking-version}</version>
        </dependency>

# 日志使用

TraceID 可以绑定日志并且在全文中输出 TraceID

log4j2 配置 xml 以在日志输出中添加 TraceID

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
 <!-- 在 pattern 中增加 [% traceId]  来打印 TraceID -->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] [%C] [%traceId] %-5level -  %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO" includeLocation="false">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

全文打印

String traceId = TraceContext.*traceId*();

# 配置文件及插件

# 主配置文件

文件目录 skywalking/config/agent.config 只写几个比较有意义的

#修改 collector endpoint
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:xxx:9002}
#修改认证
agent.authentication=${SW_AGENT_AUTHENTICATION:your_authentication}
#采样 -1 为全量
agent.sample_n_per_3_secs=${SW_AGENT_SAMPLE:-1}
#收集的 grpc 超时时间
collector.grpc_upstream_timeout=${SW_AGENT_COLLECTOR_GRPC_UPSTREAM_TIMEOUT:30
#sw 日志的级别 会占盘空间 所以调成 error
logging.level=${SW_LOGGING_LEVEL:ERROR}
#输出方式 有 CONSOLE 和 FILE 两种 我这里选择的落盘
logging.output=${SW_LOGGING_OUTPUT:FILE}
#单日志文件最大大小
logging.max_file_size=${SW_LOGGING_MAX_FILE_SIZE:314572800}
#历史文件保存 0 或负数代表一直保存历史文件
logging.max_history_files=${SW_LOGGING_MAX_HISTORY_FILES:1}

# 忽略无用指标

文件路径 skywalking/config/apm-trace-ignore-plugin.config

// 使用了 nacos apollo 健康检查及 springcloudgateway 等可以照抄 减少不必要的指标上报
trace.ignore_path=${SW_AGENT_TRACE_IGNORE_PATH:/health,GET:/health,/notifications/v2,/eureka/apps/,GET:/actuator/info,GET:/oauth/actuator/info,/nacos/**,/logstores/**,com.xxx.api.gateway.filter.**,/actuator/**,/eureka/**,httpasyncclient/local,HttpAsyncClient/local,/topics/**,org.springframework.cloud.context.properties.**}

# 可选的 agent

注意根据实际的 springcloudgateway 版本号 选择 apm-spring-cloud-gateway 插件 否则会丢失相关指标上报

apm-customize-enhance-plugin-8.8.0.jar  apm-guava-cache-plugin-8.8.0.jar       apm-quartz-scheduler-2.x-plugin-8.8.0.jar        apm-spring-cloud-gateway-2.1.x-plugin-8.8.0.jar  apm-trace-ignore-plugin-8.8.0.jar
apm-ehcache-2.x-plugin-8.8.0.jar        apm-jackson-2.x-plugin-8.8.0.jar       apm-sentinel-1.x-plugin-8.8.0.jar                apm-spring-cloud-gateway-3.x-plugin-8.8.0.jar    apm-zookeeper-3.4.x-plugin-8.8.0.jar
apm-fastjson-1.x-plugin-8.8.0.jar       apm-kotlin-coroutine-plugin-8.8.0.jar  apm-spring-annotation-plugin-8.8.0.jar           apm-spring-tx-plugin-8.8.0.jar
apm-gson-2.x-plugin-8.8.0.jar           apm-mybatis-3.x-plugin-8.8.0.jar       apm-spring-cloud-gateway-2.0.x-plugin-8.8.0.jar  apm-spring-webflux-5.x-plugin-8.8.0.jar

需要关注对应的配置文件 上文中的 agent.conf 最下方

# If true, trace all the parameters in MongoDB access, default is false. Only trace the operation, not include parameters.
plugin.mongodb.trace_param=${SW_PLUGIN_MONGODB_TRACE_PARAM:false}
# If set to positive number, the `WriteRequest.params` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.
plugin.mongodb.filter_length_limit=${SW_PLUGIN_MONGODB_FILTER_LENGTH_LIMIT:256}
# If true, trace all the DSL(Domain Specific Language) in ElasticSearch access, default is false.
plugin.elasticsearch.trace_dsl=${SW_PLUGIN_ELASTICSEARCH_TRACE_DSL:false}
# If true, the fully qualified method name will be used as the endpoint name instead of the request URL, default is false.
plugin.springmvc.use_qualified_name_as_endpoint_name=${SW_PLUGIN_SPRINGMVC_USE_QUALIFIED_NAME_AS_ENDPOINT_NAME:false}
# If true, the fully qualified method name will be used as the operation name instead of the given operation name, default is false.
plugin.toolit.use_qualified_name_as_operation_name=${SW_PLUGIN_TOOLIT_USE_QUALIFIED_NAME_AS_OPERATION_NAME:false}
# If set to true, the parameters of the sql (typically `java.sql.PreparedStatement`) would be collected.
plugin.jdbc.trace_sql_parameters=${SW_JDBC_TRACE_SQL_PARAMETERS:true}
# If set to positive number, the `db.sql.parameters` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.
plugin.jdbc.sql_parameters_max_length=${SW_PLUGIN_JDBC_SQL_PARAMETERS_MAX_LENGTH:512}
# If set to positive number, the `db.statement` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.
plugin.jdbc.sql_body_max_length=${SW_PLUGIN_JDBC_SQL_BODY_MAX_LENGTH:2048}
# If true, trace all the query parameters(include deleteByIds and deleteByQuery) in Solr query request, default is false.
plugin.solrj.trace_statement=${SW_PLUGIN_SOLRJ_TRACE_STATEMENT:false}
# If true, trace all the operation parameters in Solr request, default is false.
plugin.solrj.trace_ops_params=${SW_PLUGIN_SOLRJ_TRACE_OPS_PARAMS:false}
# If true, trace all middleware/business handlers that are part of the Light4J handler chain for a request.
plugin.light4j.trace_handler_chain=${SW_PLUGIN_LIGHT4J_TRACE_HANDLER_CHAIN:false}
# If true, the transaction definition name will be simplified.
plugin.springtransaction.simplify_transaction_definition_name=${SW_PLUGIN_SPRINGTRANSACTION_SIMPLIFY_TRANSACTION_DEFINITION_NAME:false}
#  Threading classes (`java.lang.Runnable` and `java.util.concurrent.Callable`) and their subclasses, including anonymous inner classes whose name match any one of the `THREADING_CLASS_PREFIXES` (splitted by `,`) will be instrumented, make sure to only specify as narrow prefixes as what you're expecting to instrument, (`java.` and `javax.` will be ignored due to safety issues)
plugin.jdkthreading.threading_class_prefixes=${SW_PLUGIN_JDKTHREADING_THREADING_CLASS_PREFIXES:com.imilab.summer}
#  This config item controls that whether the Tomcat plugin should collect the parameters of the request. Also, activate implicitly in the profiled trace. 
plugin.tomcat.collect_http_params=${SW_PLUGIN_TOMCAT_COLLECT_HTTP_PARAMS:false}
#  This config item controls that whether the SpringMVC plugin should collect the parameters of the request, when your Spring application is based on Tomcat, consider only setting either `plugin.tomcat.collect_http_params` or `plugin.springmvc.collect_http_params`. Also, activate implicitly in the profiled trace. 
plugin.springmvc.collect_http_params=${SW_PLUGIN_SPRINGMVC_COLLECT_HTTP_PARAMS:true}
#  This config item controls that whether the HttpClient plugin should collect the parameters of the request 
plugin.httpclient.collect_http_params=${SW_PLUGIN_HTTPCLIENT_COLLECT_HTTP_PARAMS:true}
#  When `COLLECT_HTTP_PARAMS` is enabled, how many characters to keep and send to the OAP backend, use negative values to keep and send the complete parameters, NB. this config item is added for the sake of performance.  
plugin.http.http_params_length_threshold=${SW_PLUGIN_HTTP_HTTP_PARAMS_LENGTH_THRESHOLD:1024}
#  When `include_http_headers` declares header names, this threshold controls the length limitation of all header values. use negative values to keep and send the complete headers. Note. this config item is added for the sake of performance. 
plugin.http.http_headers_length_threshold=${SW_PLUGIN_HTTP_HTTP_HEADERS_LENGTH_THRESHOLD:2048}
#  Set the header names, which should be collected by the plugin. Header name must follow `javax.servlet.http` definition. Multiple names should be split by comma.
plugin.http.include_http_headers=${SW_PLUGIN_HTTP_INCLUDE_HTTP_HEADERS:}
#  This config item controls that whether the Feign plugin should collect the http body of the request. 
plugin.feign.collect_request_body=${SW_PLUGIN_FEIGN_COLLECT_REQUEST_BODY:true}
#  When `COLLECT_REQUEST_BODY` is enabled, how many characters to keep and send to the OAP backend, use negative values to keep and send the complete body.  
plugin.feign.filter_length_limit=${SW_PLUGIN_FEIGN_FILTER_LENGTH_LIMIT:1024}
#  When `COLLECT_REQUEST_BODY` is enabled and content-type start with SUPPORTED_CONTENT_TYPES_PREFIX, collect the body of the request , multiple paths should be separated by `,`  
plugin.feign.supported_content_types_prefix=${SW_PLUGIN_FEIGN_SUPPORTED_CONTENT_TYPES_PREFIX:application/json,text/}
# If true, trace all the influxql(query and write) in InfluxDB access, default is true.

# agent 挂载方式及启动命令

k8s 使用 volume 挂载

volumes:
        - hostPath:
            path: /etc/localtime
            type: ''
          name: volume-localtime
        - name: volume-skywalking-agent-dir
          persistentVolumeClaim:
            claimName: skywalking-agent-dir

java 启动命令
java -jar -javaagent:/path/to/skywalking-agent.jar -Dskywalking.agent.service_name=xxxx xxx.jar

阅读次数

请我喝[茶]~( ̄▽ ̄)~*

JoyseeKing 微信支付

微信支付

JoyseeKing 支付宝

支付宝

JoyseeKing 贝宝

贝宝