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

[SCB-422] prometheus switch to new mechanism #639

Merged
merged 1 commit into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ public static void run() {
try {
String content = restTemplate.getForObject("cse://springmvc/codeFirstSpringmvc/prometheusForTest", String.class);

TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_addDate"));
TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_sayHello"));
TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_fallbackFromCache"));
TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_isTrue"));
TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_add"));
TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_sayHi2"));
TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_saySomething"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.addDate"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.sayHello"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.fallbackFromCache"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.isTrue"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.add"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.sayHi2"));
TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.saySomething"));

String[] metricLines = content.split("\n");
if (metricLines.length > 0) {
Expand Down
8 changes: 5 additions & 3 deletions metrics/metrics-integration/metrics-prometheus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>metrics-integration</artifactId>
Expand All @@ -43,7 +42,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>metrics-core</artifactId>
</dependency>

<dependency>
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the scope set to 'test' here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why test?
all servicecomb measurement come from metrics-core.

Copy link
Member

Choose a reason for hiding this comment

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

I mean 'foundation-test-scaffolding'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

'foundation-test-scaffolding' already in 'test' scope, no need to set again.

<groupId>org.apache.servicecomb</groupId>
<artifactId>foundation-test-scaffolding</artifactId>
</dependency>
</dependencies>

</project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.servicecomb.metrics.prometheus;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.spectator.api.CompositeRegistry;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Tag;

import io.prometheus.client.Collector;
import io.prometheus.client.Collector.MetricFamilySamples.Sample;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;

public class PrometheusPublisher extends Collector implements Collector.Describable, MetricsInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusPublisher.class);

static final String METRICS_PROMETHEUS_ADDRESS = "servicecomb.metrics.prometheus.address";

private HTTPServer httpServer;

private CompositeRegistry globalRegistry;

@Override
public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
this.globalRegistry = globalRegistry;

//prometheus default port allocation is here : https://github.com/prometheus/prometheus/wiki/Default-port-allocations
String address =
DynamicPropertyFactory.getInstance().getStringProperty(METRICS_PROMETHEUS_ADDRESS, "0.0.0.0:9696").get();

try {
InetSocketAddress socketAddress = getSocketAddress(address);
register();
this.httpServer = new HTTPServer(socketAddress, CollectorRegistry.defaultRegistry, true);
Copy link
Contributor

@liubao68 liubao68 Apr 9, 2018

Choose a reason for hiding this comment

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

For future customization, I think we 'd provide way to export to servicecomb rest framework. And contribute the code to https://github.com/prometheus/client_java. If we can create a task to tracking this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By MetricsRestPublisher in previous PR, we already support metrics endpoint: /metrics

and created a issue for contribute: https://issues.apache.org/jira/browse/SCB-466


LOGGER.info("Prometheus httpServer listened : {}.", address);
} catch (Exception e) {
throw new ServiceCombException("create http publish server failed,may bad address : " + address, e);
}
}

private InetSocketAddress getSocketAddress(String address) {
String[] hostAndPort = address.split(":");
if (hostAndPort.length == 2) {
return new InetSocketAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
}
throw new ServiceCombException("create http publish server failed,bad address : " + address);
}

@Override
public List<MetricFamilySamples> describe() {
List<MetricFamilySamples> familySamples = new ArrayList<>();
if (globalRegistry == null) {
return familySamples;
}

List<Sample> samples = new ArrayList<>();
globalRegistry
.iterator()
.forEachRemaining(meter -> {
meter.measure().forEach(measurement -> {
Sample sample = convertMeasurementToSample(measurement);
samples.add(sample);
});
});
familySamples.add(new MetricFamilySamples("ServiceComb Metrics", Type.UNTYPED, "ServiceComb Metrics", samples));

return familySamples;
}

protected Sample convertMeasurementToSample(Measurement measurement) {
String prometheusName = measurement.id().name().replace(".", "_");
List<String> labelNames = new ArrayList<>();
List<String> labelValues = new ArrayList<>();

for (Tag tag : measurement.id().tags()) {
labelNames.add(tag.key());
labelValues.add(tag.value());
}

return new Sample(prometheusName, labelNames, labelValues, measurement.value());
}

@Override
public List<MetricFamilySamples> collect() {
return describe();
}

@Override
public void uninit() {
if (httpServer == null) {
return;
}

httpServer.stop();
httpServer = null;
LOGGER.info("Prometheus httpServer stopped.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.servicecomb.metrics.prometheus.PrometheusPublisher