Skip to content

Commit

Permalink
feat: add agent-info http api close #285 (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
observeralone authored Sep 29, 2022
1 parent f0a4b3e commit 4110e17
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ A lightweight & opening Java Agent for Cloud-Native and APM system
* Instrumenting the `traceId` and `spanId` into user application logging automatically
* Supplying the `health check` endpoint
* Supplying the `readiness check` endpoint for `SpringBoot2.2.x`
* Supplying the `agent info` endpoint

* Data Reports
* Console Reporter.
Expand Down
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.megaease.easeagent.config.*;
import com.megaease.easeagent.context.ContextManager;
import com.megaease.easeagent.core.config.*;
import com.megaease.easeagent.core.info.AgentInfoFactory;
import com.megaease.easeagent.core.plugin.BaseLoader;
import com.megaease.easeagent.core.plugin.BridgeDispatcher;
import com.megaease.easeagent.core.plugin.PluginLoader;
Expand All @@ -32,6 +33,7 @@
import com.megaease.easeagent.plugin.api.trace.TracingProvider;
import com.megaease.easeagent.plugin.bean.AgentInitializingBean;
import com.megaease.easeagent.plugin.bean.BeanProvider;
import com.megaease.easeagent.plugin.bridge.AgentInfo;
import com.megaease.easeagent.plugin.bridge.EaseAgent;
import com.megaease.easeagent.plugin.report.AgentReport;
import com.megaease.easeagent.report.AgentReportAware;
Expand Down Expand Up @@ -92,7 +94,10 @@ public static void start(String args, Instrumentation inst) {
configPath = args;
}

final GlobalConfigs conf = ConfigFactory.loadConfigs(configPath, Bootstrap.class.getClassLoader());
ClassLoader classLoader = Bootstrap.class.getClassLoader();
final AgentInfo agentInfo = AgentInfoFactory.loadAgentInfo(classLoader);
EaseAgent.agentInfo = agentInfo;
final GlobalConfigs conf = ConfigFactory.loadConfigs(configPath, classLoader);
wrapConfig(conf);

// loader check
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.core.info;

import com.megaease.easeagent.log4j2.Logger;
import com.megaease.easeagent.log4j2.LoggerFactory;
import com.megaease.easeagent.plugin.bridge.AgentInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AgentInfoFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(AgentInfoFactory.class);
public static final String AGENT_TYPE = "EaseAgent";
private static final String VERSION_FILE = "version.txt";


public static AgentInfo loadAgentInfo(ClassLoader classLoader) {
return new AgentInfo(AGENT_TYPE, loadVersion(classLoader, VERSION_FILE));
}


private static String loadVersion(ClassLoader classLoader, String file) {
try (InputStream in = classLoader.getResourceAsStream(file)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String version = reader.readLine();
reader.close();
return version;
} catch (IOException e) {
LOGGER.warn("Load config file:{} by classloader:{} failure: {}", file, classLoader.toString(), e);
}
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.core.info;

import com.megaease.easeagent.core.utils.JsonUtil;
import com.megaease.easeagent.httpserver.nano.AgentHttpHandler;
import com.megaease.easeagent.httpserver.nano.AgentHttpHandlerProvider;
import com.megaease.easeagent.httpserver.nano.AgentHttpServer;
import com.megaease.easeagent.httpserver.nanohttpd.protocols.http.IHTTPSession;
import com.megaease.easeagent.httpserver.nanohttpd.protocols.http.response.Response;
import com.megaease.easeagent.httpserver.nanohttpd.protocols.http.response.Status;
import com.megaease.easeagent.httpserver.nanohttpd.router.RouterNanoHTTPD;
import com.megaease.easeagent.plugin.bean.BeanProvider;
import com.megaease.easeagent.plugin.bridge.EaseAgent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AgentInfoProvider implements AgentHttpHandlerProvider, BeanProvider {

@Override
public List<AgentHttpHandler> getAgentHttpHandlers() {
List<AgentHttpHandler> list = new ArrayList<>();
list.add(new AgentInfoHttpHandler());
return list;
}

public static class AgentInfoHttpHandler extends AgentHttpHandler {

@Override
public String getPath() {
return "/agent-info";
}

@Override
public Response process(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) {
return Response.newFixedLengthResponse(Status.OK, AgentHttpServer.JSON_TYPE, JsonUtil.toJson(EaseAgent.getAgentInfo()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
com.megaease.easeagent.core.health.HealthProvider
com.megaease.easeagent.core.info.AgentInfoProvider
1 change: 1 addition & 0 deletions core/src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${project.version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.core.info;

import com.megaease.easeagent.config.ConfigFactory;
import com.megaease.easeagent.config.Configs;
import com.megaease.easeagent.plugin.bridge.AgentInfo;
import org.junit.Test;

import static org.junit.Assert.*;

public class AgentInfoFactoryTest {

@Test
public void loadAgentInfo() {
AgentInfo config = AgentInfoFactory.loadAgentInfo(this.getClass().getClassLoader());
assertEquals(AgentInfoFactory.AGENT_TYPE, config.getType());
assertTrue(config.getVersion().matches(".*\\d+\\.\\d+\\.\\d+.*"));
}
}
17 changes: 16 additions & 1 deletion doc/user-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Logging](#logging)
- [Prometheus Support](#prometheus-support)
- [Health Check and Readiness Check Endpoint](#health-check-and-readiness-check-endpoint)
- [Agent info Endpoint](#agent-info-endpoint)
- [Tracing](#tracing)
- [Tracing Component](#tracing-component)
- [Custom Span Tag](#custom-span-tag)
Expand Down Expand Up @@ -72,7 +73,7 @@ EaseAgent opens port `9900` by default to receive configuration change notificat

| Key | Default Value | Description |
| -------------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `easeagent.server.enabled` | true | Enable Internal HTTP Server. `false` can disable it. EaseAgent will no longer accept any HTTP requests (`Prometheus``Health Check``Readiness Check`) when the Internal HTTP Server is disabled. User can add VM parameter:`-Deaseagent.server.enabled=[true or false]` to override. |
| `easeagent.server.enabled` | true | Enable Internal HTTP Server. `false` can disable it. EaseAgent will no longer accept any HTTP requests (`Prometheus``Health Check``Readiness Check``Agent Info`) when the Internal HTTP Server is disabled. User can add VM parameter:`-Deaseagent.server.enabled=[true or false]` to override. |
| `easeagent.server.port` | 9900 | Internal HTTP Server port. User can add VM parameter:`-Deaseagent.server.port=[new port]` to override. |

#### Output Data Server: Kafka and HTTP/Zipkin Server
Expand Down Expand Up @@ -384,6 +385,20 @@ After Spring sending `ApplicationReadyEvent`, EaseAgent will change readiness st
The response status will be 200(OK)
```
## Agent info Endpoint
EaseAgent supply the `agent info` http api.
```
[GET] http://[ip]:[easeagent.server.port]/agent-info
```
The response status will be 200(OK)
Response Body:
```json
{
"type": "EaseAgent",
"version": "x.x.x"
}
```

## Tracing
EaseAgent use [brave](https://github.com/openzipkin/brave) to collect tracing logs.The data format stored in `Kafka` is [Zipkin Data Model](https://zipkin.io/pages/data_model.html). User can send tracing logs to [Zipkin server](https://zipkin.io/pages/quickstart.html).

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.plugin.bridge;

public class AgentInfo {
private final String type;
private final String version;

public AgentInfo(String type, String version) {
this.type = type;
this.version = version;
}

public String getType() {
return type;
}

public String getVersion() {
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* the bridge api will be initiated when agent startup
*/
public final class EaseAgent {
public static AgentInfo agentInfo;
public static MetricRegistrySupplier metricRegistrySupplier = NoOpMetrics.NO_OP_METRIC_SUPPLIER;
public static IContextManager initializeContextSupplier = () -> NoOpContext.NO_OP_CONTEXT;
public static ILoggerFactory loggerFactory = NoOpLoggerFactory.INSTANCE;
Expand Down Expand Up @@ -136,4 +137,8 @@ public static AutoRefreshPluginConfigImpl getOrCreateAutoRefreshConfig(String do
public static Context getContext() {
return initializeContextSupplier.getContext();
}

public static AgentInfo getAgentInfo() {
return agentInfo;
}
}

0 comments on commit 4110e17

Please sign in to comment.