问题描述

近日好消息,如果是一个Java Spring Cloud的项目,想使用Azure Applicaiton Insights来收集日志及一些应用程序见解。但是有不愿意集成SDK来修改代码或者配置,有没有一种更好的办法呢? 答案是有。

在2020年,微软推出了“Java 无代码应用程序监视 Azure Monitor Application Insights” 代理工具 Application Insights Agent 3.x。它 无需更改代码,只需更改几个配置即可启用 Java 代理。官方的宣传语为:

Java 代理可在任何环境中正常工作,并允许你监视所有 Java 应用程序。 换句话说,无论你是在 VM 上、本地、AKS 中还是在 Windows、Linux 上运行 Java 应用,不管什么位置,Java 3.0 代理都可以监视你的应用。

不再需要将 Application Insights Java SDK 添加到你的应用程序,因为 3.0 代理会自动收集请求、依赖项并自行记录所有内容。

你仍可以从应用程序发送自定义遥测。 3.0 代理会跟踪它并将它与所有自动收集的遥测数据相关联。

3.0 代理支持 Java 8 及更高版本。

 

使用方法也非常的简单,三个步骤:下载代理到本地项目,配置Application Insights连接字符串,在java启动命令中添加代理参数( -javaagent:agent/applicationinsights-agent-3.1.0.jar ) 

 

快速启用Java Agent:

1.下载代理 applicationinsights-agent-3.1.0.jar

2.将 JVM 指向该代理,将 -javaagent:path/to/applicationinsights-agent-3.1.0.jar 添加到应用程序的 JVM 参数。如

java -javaagent:agent/applicationinsights-agent-3.1.0.jar -jar target/spring-boot-0.0.1-snapshot.jar

3.将代理指向 Application Insights 资源

通过设置环境变量,将代理指向 Application Insights 资源:APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=...

另一种方法是创建一个名为 applicationinsights.json 的配置文件,并将其置于 applicationinsights-agent-3.1.0.jar 所在的目录中,该文件包含以下内容:

{

"connectionString": "InstrumentationKey=..."

}

可以在 Application Insights 资源中找到连接字符串:

4.就这么简单!

现在启动应用程序,并访问 Azure 门户中的 Application Insights 资源以查看监视数据。

 备注:监视数据可能需要几分钟时间才能在门户中显示。

 

效果展示:

 

以上展示成功的采集到Java应用的所有请求及请求所产生的两个外部依赖,一个访问azure blob文件,一个是请求博客园文章地址。

但是,当站点的请求量巨大时候,Agent也将发送大量的请求,日志,依赖等等监控数据。 那是不是有什么方便的办法来解决数据量问题呢?是的。同样可以通过采样率(sampling)设置来实现这个目的。

Sampling有两种方式:一种是应用的所有请求采样率都下降,不要100%采集。 修改方式为在 applicationinsights.json 文件中设置sampling的值。如下修改采样率为50%.

{

"connectionString": "InstrumentationKey=xxxxxxxxxxxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=https://xxxxxxxx.in.applicationinsights.azure.cn/",

"role": {

"name": "my test 3"

},

"sampling": {

"percentage": 50

}

}

第二种方式,就是本文中将介绍的。采用指定特定的URL来修改次采样率。

 

问题解决

使用Agent 3.0.3版本之后才支持的采样代替(sampling overrides)功能。采样替代允许替代默认采样百分比,例如:

将采样百分比设置为 0(或某个小的值)以进行干扰状况检查。

将采样百分比设置为 0(或某个小的值)以进行干扰依赖项调用。

针对重要请求类型(例如,/login),将采样百分比设置为 100,即使将默认采样配置为低于此值也是如此。

 采样代替的作用范围为:

传入的请求。

传出的依赖项(例如,对另一个服务的远程调用)。

进程内依赖项(例如,服务的子组件所做的工作)。

对于采样替代而言,属性是一个非常重要的概念,它表示给定请求或依赖项的标准属性和自定义属性。对于一个URL请求而言,可以用以下的属性值来作为配置条件:

HTTP 范围

属性类型说明

http.method

string

HTTP 请求方法。

http.url

string

完整的 HTTP 请求 URL(采用 scheme://host[:port]/path?query[#fragment] 格式)。 例如:http://localhost:8080/out

http.status_code

数值

HTTP 响应状态代码。

http.flavor

string

HTTP 协议类型。

http.user_agent

string

客户端发送的 HTTP User-Agent 标头的值。

 

在本示例中,本Java应用有三个请求。http://localhost:8080, http://localhost:8080/blob 和http://localhost:8080/out。 三个请求的依赖项如下图:

 

 为了把blob的请求以及它的依赖的所有请求都排除在请求之外。使用采样代替功能,可以参考如下设置:

{

"connectionString":"InstrumentationKey=xxxxxxxxxxxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=https://xxxxxxxx.in.applicationinsights.azure.cn/",

"role": {

"name": "my test 3"

},

"preview": {

"sampling": {

"overrides": [

{

"attributes": [

{

"key": "http.url",

"value": "https?://[^/]+/blob",

"matchType": "regexp"

}

],

"percentage": 0

}

]

}

}

}

修改完 applicationinsights.json 文件后,重启应用。验证结果:

 

 

附件一:如果需要对多个请求URL进行采样率修改。在节点overrides中的数组中不停加入attributes内容即可。如:

{

"connectionString":"InstrumentationKey=xxxxxxxxxxxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=https://xxxxxxxx.in.applicationinsights.azure.cn/",

"role": {

"name": "my test 3"

},

"preview": {

"sampling": {

"overrides": [

{

"attributes": [

{

"key": "http.url",

"value": "https?://[^/]+/blob",

"matchType": "regexp"

}

],

"percentage": 0

},

{

"attributes": [

{

"key": "http.url",

"value": "https?://[^/]+/lulight/",

"matchType": "regexp"

}

],

"percentage": 0

}

]

}

}

}

 

附件二:对整体采样率设置为非常小,如1,但是对一些重点的URL则设置为100. 

{

"connectionString": "InstrumentationKey=xxxxxxxxxxxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=https://xxxxxxxx.in.applicationinsights.azure.cn/",

"role": {

"name": "Java App Test 16"

},

"sampling": {

"percentage": 1

},

"preview": {

"sampling": {

"overrides": [

{

"attributes": [

{

"key": "http.url",

"value": "http?://[^/]+/v1/member+(/[\\w-./?%&=]*)?$",

"matchType": "regexp"

}

],

"percentage": 100

},

{

"attributes": [

{

"key": "http.url",

"value": "http?://[^/]+/blob",

"matchType": "regexp"

}

],

"percentage": 0

},

{

"attributes": [

{

"key": "http.url",

"value": "http?://[^/]+/out",

"matchType": "regexp"

}

],

"percentage": 100

},

{

"attributes": [

{

"key": "http.url",

"value": "https?://[^/]+/lulight/p/14978216.html",

"matchType": "regexp"

}

],

"percentage": 0

}

]

}

}

}

 

附件三:如何验证正则表达式的正确性及在JAVA中的正确字符串(需要注意转义)

验证站点:https://c.runoob.com/front-end/854

 

 然后点击生成代码,发现Java部分中所使用的字符串,COPY出来放入applicationinsights.json文件中。

 

 

附件四:如果在启动AI Java Agent 出现 “java.util.regex.PatternSyntaxException: Unclosed character class near index 28”, 则一定要根据#3中的方式来检查 applicationinsights.json 中正则表达式是否正确

2021-07-27 15:47:46.936+08 ERROR c.m.applicationinsights.agent - ApplicationInsights Java Agent 3.1.0 failed to start

java.util.regex.PatternSyntaxException: Unclosed character class near index 28

http?://[^/]+/v1/member?/[^]+

^

at java.util.regex.Pattern.error(Pattern.java:1969)

at java.util.regex.Pattern.clazz(Pattern.java:2562)

at java.util.regex.Pattern.sequence(Pattern.java:2077)

at java.util.regex.Pattern.expr(Pattern.java:2010)

at java.util.regex.Pattern.compile(Pattern.java:1702)

at java.util.regex.Pattern.(Pattern.java:1352)

at java.util.regex.Pattern.compile(Pattern.java:1028)

at com.microsoft.applicationinsights.agent.internal.sampling.SamplingOverrides$RegexpMatcher.(SamplingOverrides.java:179)

at com.microsoft.applicationinsights.agent.internal.sampling.SamplingOverrides$RegexpMatcher.(SamplingOverrides.java:173)

at com.microsoft.applicationinsights.agent.internal.sampling.SamplingOverrides$MatcherGroup.toPredicate(SamplingOverrides.java:150)

at com.microsoft.applicationinsights.agent.internal.sampling.SamplingOverrides$MatcherGroup.(SamplingOverrides.java:123)

at com.microsoft.applicationinsights.agent.internal.sampling.SamplingOverrides$MatcherGroup.(SamplingOverrides.java:115)

at com.microsoft.applicationinsights.agent.internal.sampling.SamplingOverrides.(SamplingOverrides.java:28)

at com.microsoft.applicationinsights.agent.internal.sampling.Samplers.getSampler(Samplers.java:9)

at com.microsoft.applicationinsights.agent.internal.wasbootstrap.OpenTelemetryConfigurer.configure(OpenTelemetryConfigurer.java:39)

at io.opentelemetry.sdk.autoconfigure.TracerProviderConfiguration.configureTracerProvider(TracerProviderConfiguration.java:40)

at io.opentelemetry.sdk.autoconfigure.OpenTelemetrySdkAutoConfiguration.initialize(OpenTelemetrySdkAutoConfiguration.java:45)

at io.opentelemetry.javaagent.tooling.OpenTelemetryInstaller.installAgentTracer(OpenTelemetryInstaller.java:36)

at io.opentelemetry.javaagent.tooling.OpenTelemetryInstaller.beforeByteBuddyAgent(OpenTelemetryInstaller.java:27)

at io.opentelemetry.javaagent.tooling.AgentInstaller.installComponentsBeforeByteBuddy(AgentInstaller.java:180)

at io.opentelemetry.javaagent.tooling.AgentInstaller.installBytebuddyAgent(AgentInstaller.java:114)

at io.opentelemetry.javaagent.tooling.AgentInstaller.installBytebuddyAgent(AgentInstaller.java:98)

at com.microsoft.applicationinsights.agent.internal.wasbootstrap.MainEntryPoint.start(MainEntryPoint.java:89)

at io.opentelemetry.javaagent.tooling.AgentInstallerOverride.installBytebuddyAgent(AgentInstallerOverride.java:11)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at io.opentelemetry.javaagent.bootstrap.AgentInitializer.startAgent(AgentInitializer.java:50)

at io.opentelemetry.javaagent.bootstrap.AgentInitializer.initialize(AgentInitializer.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at io.opentelemetry.javaagent.OpenTelemetryAgent.premain(OpenTelemetryAgent.java:66)

at com.microsoft.applicationinsights.agent.Agent.premain(Agent.java:41)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)

at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)

 

参考资料

Java 无代码应用程序监视 Azure Monitor Application Insights:https://docs.azure.cn/zh-cn/azure-monitor/app/java-in-process-agent

采样替代(预览版)- 适用于 Java 的 Azure Monitor Application Insights:https://docs.azure.cn/zh-cn/azure-monitor/app/java-standalone-sampling-overrides

 

【完】

 

推荐阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。