Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected void doInvoke() throws Throwable {
invocation.next(resp -> {
sendResponseQuietly(resp);

invocation.triggerFinishedEvent();
invocation.triggerFinishedEvent(resp.getStatusCode(), resp.isSuccessed());
endMetrics();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ public void triggerStartProcessingEvent() {
operationMeta.getMicroserviceQualifiedName(), this.invocationType, startProcessingTime - startTime));
}

public void triggerFinishedEvent() {
public void triggerFinishedEvent(int statusCode, boolean success) {
long finishedTime = System.nanoTime();
EventUtils
.triggerEvent(new InvocationFinishedEvent(operationMeta.getMicroserviceQualifiedName(),
this.invocationType, finishedTime - startProcessingTime,
finishedTime - startTime));
finishedTime - startTime, statusCode, success));
}

public boolean isSync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class InvocationFinishedEvent implements Event {

private final long totalElapsedNanoTime;

private final int statusCode;

private final boolean success;

public String getOperationName() {
return operationName;
}
Expand All @@ -45,12 +49,21 @@ public long getTotalElapsedNanoTime() {
return totalElapsedNanoTime;
}

public int getStatusCode() {
return statusCode;
}

public boolean isSuccess() {
return success;
}

public InvocationFinishedEvent(String operationName, InvocationType invocationType,
long processElapsedNanoTime,
long totalElapsedNanoTime) {
long processElapsedNanoTime, long totalElapsedNanoTime, int statusCode, boolean success) {
this.operationName = operationName;
this.invocationType = invocationType;
this.processElapsedNanoTime = processElapsedNanoTime;
this.totalElapsedNanoTime = totalElapsedNanoTime;
this.statusCode = statusCode;
this.success = success;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,26 @@ public static Object syncInvoke(Invocation invocation) throws InvocationExceptio
}

public static Response innerSyncInvoke(Invocation invocation) {
boolean success = false;
int statusCode = 0;
try {
triggerStartedEvent(invocation);
SyncResponseExecutor respExecutor = new SyncResponseExecutor();
invocation.setResponseExecutor(respExecutor);

invocation.next(respExecutor::setResponse);

return respExecutor.waitResponse();
Response response = respExecutor.waitResponse();
success = response.isSuccessed();
statusCode = response.getStatusCode();
return response;
} catch (Throwable e) {
String msg =
String.format("invoke failed, %s", invocation.getOperationMeta().getMicroserviceQualifiedName());
LOGGER.debug(msg, e);
return Response.createConsumerFail(e);
} finally {
invocation.triggerFinishedEvent();
invocation.triggerFinishedEvent(statusCode, success);
}
}

Expand All @@ -89,12 +94,12 @@ public static void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp
invocation.setResponseExecutor(respExecutor);

invocation.next(ar -> {
invocation.triggerFinishedEvent();
invocation.triggerFinishedEvent(ar.getStatusCode(), ar.isSuccessed());
asyncResp.handle(ar);
});

} catch (Throwable e) {
invocation.triggerFinishedEvent();
//if throw exception,we can use 500 for status code ?
invocation.triggerFinishedEvent(500, false);
LOGGER.error("invoke failed, {}", invocation.getOperationMeta().getMicroserviceQualifiedName());
asyncResp.consumerFail(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
import org.apache.servicecomb.metrics.common.MetricsDimension;
import org.apache.servicecomb.metrics.common.MetricsPublisher;
import org.apache.servicecomb.metrics.common.RegistryMetric;
import org.apache.servicecomb.provider.springmvc.reference.CseRestTemplate;
Expand Down Expand Up @@ -58,7 +59,7 @@ public static void main(String[] args) throws Exception {
TestMgr.summary();
}

public static void run() throws Exception {
public static void run() {
templateUrlWithServiceName.setRequestFactory(new UrlWithServiceNameClientHttpRequestFactory());
restTemplate = RestTemplateBuilder.create();
controller = BeanUtils.getBean("controller");
Expand Down Expand Up @@ -125,7 +126,8 @@ public static void run() throws Exception {
.check(true, metric.getInstanceMetric().getSystemMetric().getHeapUsed() != 0);
TestMgr.check(true, metric.getProducerMetrics().size() > 0);
TestMgr.check(true,
metric.getProducerMetrics().get("springmvc.codeFirst.saySomething").getProducerCall().getTotal() > 0);
metric.getProducerMetrics().get("springmvc.codeFirst.saySomething").getProducerCall()
.getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue() > 0);
} catch (Exception e) {
TestMgr.check("true", "false");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map.Entry;

import org.apache.servicecomb.metrics.common.ConsumerInvocationMetric;
import org.apache.servicecomb.metrics.common.MetricsDimension;
import org.apache.servicecomb.metrics.common.ProducerInvocationMetric;
import org.apache.servicecomb.metrics.common.RegistryMetric;
import org.apache.servicecomb.metrics.core.publish.DataSource;
Expand Down Expand Up @@ -54,8 +55,11 @@ public void onCycle() {
String opName = entry.getKey();
sb.append(String
.format(" %-16d%-16d%-16.3f%s\n",
entry.getValue().getConsumerCall().getTotal(),
(long) entry.getValue().getConsumerCall().getTps(),
entry.getValue().getConsumerCall()
.getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue(),
entry.getValue().getConsumerCall()
.getTpsValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue()
.longValue(),
entry.getValue().getConsumerLatency().getAverage(),
opName));
}
Expand All @@ -66,8 +70,11 @@ public void onCycle() {
String opName = entry.getKey();
sb.append(
String.format(" %-16d%-16d%-16.3f%-16.3f%-16.3f%s\n",
entry.getValue().getProducerCall().getTotal(),
(long) entry.getValue().getProducerCall().getTps(),
entry.getValue().getProducerCall()
.getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue(),
entry.getValue().getProducerCall()
.getTpsValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue()
.longValue(),
entry.getValue().getProducerLatency().getAverage(),
entry.getValue().getLifeTimeInQueue().getAverage(),
entry.getValue().getExecutionTime().getAverage(),
Expand Down
4 changes: 2 additions & 2 deletions metrics/metrics-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<groupId>org.apache.servicecomb</groupId>
<artifactId>foundation-common</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,76 @@

package org.apache.servicecomb.metrics.common;

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

import com.fasterxml.jackson.annotation.JsonProperty;

public class CallMetric {
private final String prefix;

private final long total;
private final List<LongMetricValue> totalValues;

private final double tps;
private final List<DoubleMetricValue> tpsValues;

public long getTotal() {
return total;
public String getPrefix() {
return prefix;
}

public double getTps() {
return tps;
public List<LongMetricValue> getTotalValues() {
return totalValues;
}

public LongMetricValue getTotalValue(String dimensionKey, String dimensionValue) {
for (LongMetricValue value : totalValues) {
if (value.containDimension(dimensionKey, dimensionValue)) {
return value;
}
}
return new LongMetricValue(dimensionValue, 0L, null);
}

public List<DoubleMetricValue> getTpsValues() {
return tpsValues;
}

public DoubleMetricValue getTpsValue(String dimensionKey, String dimensionValue) {
for (DoubleMetricValue value : tpsValues) {
if (value.containDimension(dimensionKey, dimensionValue)) {
return value;
}
}
return new DoubleMetricValue(dimensionValue, 0.0, null);
}

public CallMetric(String prefix) {
this(prefix, 0, 0);
this(prefix, new ArrayList<>(), new ArrayList<>());
}

public CallMetric(@JsonProperty("prefix") String prefix, @JsonProperty("total") long total,
@JsonProperty("tps") double tps) {
public CallMetric(@JsonProperty("prefix") String prefix,
@JsonProperty("totalValues") List<LongMetricValue> totalValues,
@JsonProperty("tpsValues") List<DoubleMetricValue> tpsValues) {
this.prefix = prefix;
this.total = total;
this.tps = tps;
this.totalValues = totalValues;
this.tpsValues = tpsValues;
}

public CallMetric merge(CallMetric metric) {
return new CallMetric(this.prefix, this.total + metric.total, this.tps + metric.tps);
return new CallMetric(this.prefix,
LongMetricValue.merge(metric.getTotalValues(), this.getTotalValues()),
DoubleMetricValue.merge(metric.getTpsValues(), this.getTpsValues()));
}

public Map<String, Number> toMap() {
Map<String, Number> metrics = new HashMap<>();
metrics.put(prefix + ".total", total);
metrics.put(prefix + ".tps", tps);
for (MetricValue totalValue : totalValues) {
metrics.put(prefix + ".total." + totalValue.getKey(), totalValue.getValue());
}
for (MetricValue tpsValue : tpsValues) {
metrics.put(prefix + ".tps." + tpsValue.getKey(), tpsValue.getValue());
}
return metrics;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.common;

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

import com.fasterxml.jackson.annotation.JsonProperty;

public class DoubleMetricValue extends MetricValue<Double> {
public DoubleMetricValue(Double value, Map<String, String> dimensions) {
super(value, dimensions);
}

public DoubleMetricValue(@JsonProperty("key") String key,
@JsonProperty("value") Double value,
@JsonProperty("dimensions") Map<String, String> dimensions) {
super(key, value, dimensions);
}

private DoubleMetricValue merge(DoubleMetricValue value) {
return new DoubleMetricValue(this.getKey(), this.getValue() + value.getValue(), this.getDimensions());
}

public static List<DoubleMetricValue> merge(List<DoubleMetricValue> source, List<DoubleMetricValue> target) {
Map<String, DoubleMetricValue> finalValues = new HashMap<>();
for (DoubleMetricValue t : target) {
finalValues.put(t.getKey(), t);
}
for (DoubleMetricValue s : source) {
if (finalValues.containsKey(s.getKey())) {
finalValues.put(s.getKey(), finalValues.get(s.getKey()).merge(s));
} else {
finalValues.put(s.getKey(), s);
}
}
return new ArrayList<>(finalValues.values());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.common;

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

import com.fasterxml.jackson.annotation.JsonProperty;

public class LongMetricValue extends MetricValue<Long> {
public LongMetricValue(Long value, Map<String, String> dimensions) {
super(value, dimensions);
}

public LongMetricValue(@JsonProperty("key") String key,
@JsonProperty("value") Long value,
@JsonProperty("dimensions") Map<String, String> dimensions) {
super(key, value, dimensions);
}

private LongMetricValue merge(LongMetricValue value) {
return new LongMetricValue(this.getKey(), this.getValue() + value.getValue(), this.getDimensions());
}

public static List<LongMetricValue> merge(List<LongMetricValue> source, List<LongMetricValue> target) {
Map<String, LongMetricValue> finalValues = new HashMap<>();
for (LongMetricValue t : target) {
finalValues.put(t.getKey(), t);
}
for (LongMetricValue s : source) {
if (finalValues.containsKey(s.getKey())) {
finalValues.put(s.getKey(), finalValues.get(s.getKey()).merge(s));
} else {
finalValues.put(s.getKey(), s);
}
}
return new ArrayList<>(finalValues.values());
}
}
Loading