-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Docs]OSPP-Add more observability in apollo config client #5229
Open
Rawven
wants to merge
3
commits into
apolloconfig:master
Choose a base branch
from
Rawven:docs/observability
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
docs/en/extension/java-client-how-to-use-custom-monitor-system.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
In version 2.4.0 and above of the Java client, support for metrics collection and export has been added, allowing users to extend and integrate with different monitoring systems. | ||
|
||
## Taking Prometheus Integration as an Example | ||
|
||
Create the `PrometheusApolloClientMetricsExporter` class, which extends `AbstractApolloClientMetricsExporter` (the generic metrics export framework). | ||
|
||
The code after extending is roughly as follows: | ||
|
||
```java | ||
|
||
public class PrometheusApolloClientMetricsExporter extends | ||
AbstractApolloClientMetricsExporter implements ApolloClientMetricsExporter { | ||
|
||
@Override | ||
public void doInit() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isSupport(String form) { | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void registerOrUpdateCounterSample(String name, Map<String, String> tags, double incrValue) { | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void registerOrUpdateGaugeSample(String name, Map<String, String> tags, double value) { | ||
|
||
} | ||
|
||
@Override | ||
public String response() { | ||
|
||
} | ||
} | ||
|
||
``` | ||
|
||
The doInit method is provided for users to extend during initialization and will be called in the init method of AbstractApolloClientMetricsExporter. | ||
|
||
```java | ||
@Override | ||
public void init(List<ApolloClientMonitorEventListener> collectors, long collectPeriod) { | ||
log.info("Initializing metrics exporter with {} collectors and collect period of {} seconds.", | ||
collectors.size(), collectPeriod); | ||
doInit(); | ||
this.collectors = collectors; | ||
initScheduleMetricsCollectSync(collectPeriod); | ||
log.info("Metrics collection scheduled with a period of {} seconds.", collectPeriod); | ||
} | ||
``` | ||
|
||
Here, the Prometheus Java client is introduced, and the CollectorRegistry and cache map need to be initialized. | ||
|
||
```java | ||
private CollectorRegistry registry; | ||
private Map<String, Collector.Describable> map; | ||
|
||
@Override | ||
public void doInit() { | ||
registry = new CollectorRegistry(); | ||
map = new HashMap<>(); | ||
} | ||
``` | ||
|
||
The isSupport method will be called in DefaultApolloClientMetricsExporterFactory via SPI to check which MetricsExporter to enable, allowing accurate activation of the configured exporter when multiple SPI implementations exist. | ||
|
||
For example, if you want to enable Prometheus and specify the value as "prometheus," it should synchronize here: | ||
|
||
```java | ||
@Override | ||
public boolean isSupport(String form) { | ||
return PROMETHEUS.equals(form); | ||
} | ||
``` | ||
|
||
The methods registerOrUpdateCounterSample and registerOrUpdateGaugeSample are used to register Counter and Gauge type metrics, simply registering based on the provided parameters. | ||
|
||
```java | ||
@Override | ||
public void registerOrUpdateCounterSample(String name, Map<String, String> tags, | ||
double incrValue) { | ||
Counter counter = (Counter) map.get(name); | ||
if (counter == null) { | ||
counter = createCounter(name, tags); | ||
map.put(name, counter); | ||
} | ||
counter.labels(tags.values().toArray(new String[0])).inc(incrValue); | ||
} | ||
|
||
private Counter createCounter(String name, Map<String, String> tags) { | ||
return Counter.build() | ||
.name(name) | ||
.help("apollo") | ||
.labelNames(tags.keySet().toArray(new String[0])) | ||
.register(registry); | ||
} | ||
|
||
@Override | ||
public void registerOrUpdateGaugeSample(String name, Map<String, String> tags, double value) { | ||
Gauge gauge = (Gauge) map.get(name); | ||
if (gauge == null) { | ||
gauge = createGauge(name, tags); | ||
map.put(name, gauge); | ||
} | ||
gauge.labels(tags.values().toArray(new String[0])).set(value); | ||
} | ||
|
||
private Gauge createGauge(String name, Map<String, String> tags) { | ||
return Gauge.build() | ||
.name(name) | ||
.help("apollo") | ||
.labelNames(tags.keySet().toArray(new String[0])) | ||
.register(registry); | ||
} | ||
``` | ||
|
||
Finally, you need to implement the response method, which is used to export data in the format of the integrated monitoring system. It will ultimately be obtained in the getExporterData method of ConfigMonitor, allowing users to expose an endpoint for monitoring systems to pull data. | ||
|
||
```java | ||
@Override | ||
public String response() { | ||
try (StringWriter writer = new StringWriter()) { | ||
TextFormat.writeFormat(TextFormat.CONTENT_TYPE_OPENMETRICS_100, writer, | ||
registry.metricFamilySamples()); | ||
return writer.toString(); | ||
} catch (IOException e) { | ||
logger.error("Write metrics to Prometheus format failed", e); | ||
return ""; | ||
} | ||
} | ||
``` | ||
|
||
At this point, the client's metric data has been integrated with Prometheus. | ||
|
||
Full code:https://github.com/apolloconfig/apollo-java/main/master/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/PrometheusApolloClientMetricsExporter.java | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the bare URL.
The bare URL at the end of the documentation should be wrapped in angle brackets to make it a valid Markdown link.
Apply this diff to fix the bare URL:
Committable suggestion
Tools
Markdownlint