Skip to content
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

[ISSUE #340]Integrate With OpenTelemetry for metrics in EventMesh #467

Merged
merged 21 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions eventmesh-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ List metrics = [
"io.dropwizard.metrics:metrics-json:4.1.0"
]


List open_telemetry = [
"io.opentelemetry:opentelemetry-api:1.3.0",
"io.opentelemetry:opentelemetry-sdk:1.3.0",
"io.opentelemetry:opentelemetry-sdk-metrics:1.3.0-alpha",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use other stable version? Using alpha does not seem to be a good choice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok to use that. Otel's sdk metrics is in alpha although, it is widely used in https://github.com/open-telemetry/opentelemetry-java-instrumentation. This agent has been validated in enough prod environments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

"io.opentelemetry:opentelemetry-exporter-prometheus:1.3.0-alpha",
"io.prometheus:simpleclient:0.8.1",
"io.prometheus:simpleclient_httpserver:0.8.1"
]

List open_message = [
"io.openmessaging:openmessaging-api:2.2.1-pubsub"
]


dependencies {
implementation metrics, open_message, project(":eventmesh-connector-plugin:eventmesh-connector-api")
testImplementation metrics, open_message, project(":eventmesh-connector-plugin:eventmesh-connector-api")
implementation metrics, open_telemetry, open_message, project(":eventmesh-connector-plugin:eventmesh-connector-api")
testImplementation metrics, open_telemetry,open_message, project(":eventmesh-connector-plugin:eventmesh-connector-api")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.codahale.metrics.MetricRegistry;

import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer;
import org.apache.eventmesh.runtime.metrics.openTelemetry.OpenTelemetryExporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -43,6 +44,8 @@ public class HTTPMetricsServer {

public GroupMetrics groupMetrics;

public OpenTelemetryExporter openTelemetryExporter;

private Logger httpLogger = LoggerFactory.getLogger("httpMonitor");

private Logger logger = LoggerFactory.getLogger(this.getClass());
Expand All @@ -56,10 +59,14 @@ public void init() throws Exception {
topicMetrics = new TopicMetrics(this.eventMeshHTTPServer, this.metricRegistry);
groupMetrics = new GroupMetrics(this.eventMeshHTTPServer, this.metricRegistry);
healthMetrics = new HealthMetrics(this.eventMeshHTTPServer, this.metricRegistry);

openTelemetryExporter = new OpenTelemetryExporter(summaryMetrics);

logger.info("HTTPMetricsServer inited......");
}

public void start() throws Exception {
openTelemetryExporter.start();
metricsSchedule.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -90,6 +97,7 @@ public void run() {

public void shutdown() throws Exception {
metricsSchedule.shutdown();
openTelemetryExporter.shutdown();
logger.info("HTTPMetricsServer shutdown......");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.eventmesh.runtime.metrics.openTelemetry;

import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.api.metrics.common.Labels;
import org.apache.eventmesh.runtime.metrics.http.SummaryMetrics;

/**
* test
*/
public class OpenTelemetryExporter {
OpenTelemetryExporterConfiguration configuration = new OpenTelemetryExporterConfiguration();

private SummaryMetrics summaryMetrics;

private Meter meter;

public OpenTelemetryExporter(SummaryMetrics summaryMetrics) {
this.summaryMetrics = summaryMetrics;

// it is important to initialize the OpenTelemetry SDK as early as possible in your process.
MeterProvider meterProvider = configuration.initializeOpenTelemetry();

meter = meterProvider.get("OpenTelemetryExporter", "0.13.1");
}

public void start(){
//maxHTTPTPS
meter
.doubleValueObserverBuilder("eventmesh.http.request.tps.elapsed.max")
.setDescription("max TPS of HTTP")
.setUnit("HTTP")
.setUpdater(result -> result.observe(summaryMetrics.maxHTTPTPS(),Labels.empty()))
.build();

//maxHTTPCost
meter
.longValueObserverBuilder("eventmesh.http.request.elapsed.max")
.setDescription("max cost of HTTP")
.setUnit("HTTP")
.setUpdater(result -> result.observe(summaryMetrics.maxHTTPCost(), Labels.empty()))
.build();

//avgHTTPCost
meter
.doubleValueObserverBuilder("eventmesh.http.request.elapsed.avg")
.setDescription("avg cost of HTTP")
.setUnit("HTTP")
.setUpdater(result -> result.observe(summaryMetrics.avgHTTPCost(), Labels.empty()))
.build();
}

public void shutdown(){
configuration.shutdownPrometheusEndpoint();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.eventmesh.runtime.metrics.openTelemetry;

import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.exporter.prometheus.PrometheusCollector;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.prometheus.client.exporter.HTTPServer;

import java.io.IOException;

//ues openTelemetry to export metrics data
public class OpenTelemetryExporterConfiguration {

private HTTPServer server;//Prometheus server

int prometheusPort = 19090;//the endpoint to export metrics
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to add this to configuration properties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought about it, but I'm not very good at it. What language is involved? script?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most configuration is at eventmesh-runtime/conf/eventmesh.properties, in the future, I think it may be split into multiple files, but now you can just add the prometheusPort in this file, and load this config, you can see code in EventMeshHTTPConfiguration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a try.


/**
* Initializes the Meter SDK and configures the prometheus collector with all default settings.
*
*
* @return A MeterProvider for use in instrumentation.
*/
public MeterProvider initializeOpenTelemetry() {
SdkMeterProvider meterProvider = SdkMeterProvider.builder().buildAndRegisterGlobal();

PrometheusCollector.builder().setMetricProducer(meterProvider).buildAndRegister();

try {
server = new HTTPServer(prometheusPort,true);//Use the daemon thread to start an HTTP server to serve the default Prometheus registry.
} catch (IOException e) {
e.printStackTrace();
}

return meterProvider;
}

public void shutdownPrometheusEndpoint() {
server.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Open Telemetry exporter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readme file should not exist in src

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I delete it,or put it in the design doc


we can use Prometheus UI to see the metrics exported by openTelemetry

# How to run Prometheus

download Prometheus from https://prometheus.io/download/
remember to fix the [prometheus.yml](prometheus.yml)

---
or use docker
Start Prometheus instance with a configuration that sets up a HTTP collection job for ```127.0.0.1:19090```

See [prometheus.yml](prometheus.yml)

```shell script
docker run --network="host" --rm -it \
--name prometheus \
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus

```

you can run the quickstart and open the Prometheus UI:
http://localhost:9090/graph?g0.expr=max_HTTPCost&g0.tab=1&g0.stacked=0&g0.show_exemplars=0&g0.range_input=1h


search the key word:

*eventmesh_http_request_tps_elapsed_max*

*eventmesh_http_request_elapsed_max*

*eventmesh_http_request_elapsed_avg*

## special explanation
Prometheus runs on port 9090,Open telemetry exports data to port 19090,Prometheus will collect data from port 19090

the exporter is exporting the data in 'SummaryMetrics'(package org.apache.eventmesh.runtime.metrics.http;)

The export mechanism I set is to export every 3 seconds, because QuickStart only has httpcost at a short time. If the interval is set too long, it will always be 0. In practical application, I think it should be set to more than 30 seconds
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yml file should not exist in src

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I put it in conf like this
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is ok.

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: []
scheme: http
timeout: 10s
api_version: v1
scrape_configs:
- job_name: prometheus
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- localhost:9090
- job_name: EventMesh_HTTP_export_test
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- 127.0.0.1:19090