-
Notifications
You must be signed in to change notification settings - Fork 801
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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.
Shouldn't the scope set to 'test' here?
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.
why test?
all servicecomb measurement come from metrics-core.
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.
I mean 'foundation-test-scaffolding'
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.
'foundation-test-scaffolding' already in 'test' scope, no need to set again.