未验证 提交 67bc8f8b 编写于 作者: wu-sheng's avatar wu-sheng 提交者: GitHub

Provide `listMetrics` GraphQL query service. (#5866)

* Provide `listMetrics` GraphQL query service.

* Update chagnelog.
上级 d6c9926e
......@@ -27,6 +27,7 @@ Release Notes.
* Add the thread pool to the Kafka fetcher to increase the performance.
* Add `contain` and `not contain` OPS in OAL.
* Add Envoy ALS analyzer based on metadata exchange.
* Add `listMetrics` GraphQL query.
* Support keeping collecting the slowly segments in the sampling mechanism.
* Support choose files to active the meter analyzer.
* Support nested class definition in the Service, ServiceInstance, Endpoint, ServiceRelation, and ServiceInstanceRelation sources.
......
/*
* 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.skywalking.oap.server.core.query;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.skywalking.oap.server.core.query.enumeration.MetricsType;
/**
* Define the metrics provided in the OAP server.
*
* @since 8.3.0
*/
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MetricDefinition {
private String name;
private MetricsType type;
/**
* Catalog includes SERVICE_CATALOG,SERVICE_INSTANCE_CATALOG,ENDPOINT_CATALOG,
* SERVICE_RELATION_CATALOG,SERVICE_INSTANCE_RELATION_CATALOG_NAME,ENDPOINT_RELATION_CATALOG_NAME
*/
private String catalog;
}
......@@ -18,8 +18,12 @@
package org.apache.skywalking.oap.server.core.query;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.skywalking.apm.util.StringUtil;
import org.apache.skywalking.oap.server.core.query.enumeration.MetricsType;
import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
import org.apache.skywalking.oap.server.core.storage.annotation.ValueColumnMetadata;
import org.apache.skywalking.oap.server.library.module.Service;
......@@ -48,4 +52,21 @@ public class MetricsMetadataQueryService implements Service {
return MetricsType.UNKNOWN;
}
}
public List<MetricDefinition> listMetrics(String regex) {
return ValueColumnMetadata.INSTANCE.getAllMetadata()
.entrySet()
.stream()
.filter(
metadata ->
StringUtil.isNotEmpty(regex) ?
metadata.getKey().matches(regex) : true)
.map(metadata -> new MetricDefinition(
metadata.getKey(),
typeOfMetrics(metadata.getKey()),
DefaultScopeDefine.catalogOf(metadata.getValue().getScopeId())
)
)
.collect(Collectors.toList());
}
}
......@@ -295,6 +295,34 @@ public class DefaultScopeDefine {
return ENDPOINT_RELATION_CATALOG.containsKey(scopeId);
}
/**
* Get the catalog string name of the given scope
*
* @param scope id of the source scope.
* @return literal string name of the catalog owning the scope.
*/
public static String catalogOf(int scope) {
if (inServiceCatalog(scope)) {
return SERVICE_CATALOG_NAME;
}
if (inServiceInstanceCatalog(scope)) {
return SERVICE_INSTANCE_CATALOG_NAME;
}
if (inEndpointCatalog(scope)) {
return ENDPOINT_CATALOG_NAME;
}
if (inServiceRelationCatalog(scope)) {
return SERVICE_RELATION_CATALOG_NAME;
}
if (inServiceInstanceRelationCatalog(scope)) {
return SERVICE_INSTANCE_RELATION_CATALOG_NAME;
}
if (inEndpointRelationCatalog(scope)) {
return ENDPOINT_RELATION_CATALOG_NAME;
}
return "UNKNOWN";
}
/**
* Get the default columns defined in Scope. All those columns will forward to persistent entity.
*
......
......@@ -41,8 +41,9 @@ public enum ValueColumnMetadata {
String valueCName,
Column.ValueDataType dataType,
Function function,
int defaultValue) {
mapping.putIfAbsent(modelName, new ValueColumn(valueCName, dataType, function, defaultValue));
int defaultValue,
int scopeId) {
mapping.putIfAbsent(modelName, new ValueColumn(valueCName, dataType, function, defaultValue, scopeId));
}
/**
......@@ -63,10 +64,20 @@ public enum ValueColumnMetadata {
return findColumn(metricsName).defaultValue;
}
/**
* @return metric metadata if found
*/
public Optional<ValueColumn> readValueColumnDefinition(String metricsName) {
return Optional.ofNullable(mapping.get(metricsName));
}
/**
* @return all metrics metadata.
*/
public Map<String, ValueColumn> getAllMetadata() {
return mapping;
}
private ValueColumn findColumn(String metricsName) {
ValueColumn column = mapping.get(metricsName);
if (column == null) {
......@@ -82,5 +93,6 @@ public enum ValueColumnMetadata {
private final Column.ValueDataType dataType;
private final Function function;
private final int defaultValue;
private final int scopeId;
}
}
......@@ -56,7 +56,7 @@ public class StorageModels implements IModelManager, ModelCreator, ModelManipula
List<ModelColumn> modelColumns = new ArrayList<>();
List<ExtraQueryIndex> extraQueryIndices = new ArrayList<>();
retrieval(aClass, storage.getModelName(), modelColumns, extraQueryIndices);
retrieval(aClass, storage.getModelName(), modelColumns, extraQueryIndices, scopeId);
Model model = new Model(
storage.getModelName(), modelColumns, extraQueryIndices, scopeId,
......@@ -91,10 +91,11 @@ public class StorageModels implements IModelManager, ModelCreator, ModelManipula
/**
* Read model column metadata based on the class level definition.
*/
private void retrieval(Class<?> clazz,
String modelName,
List<ModelColumn> modelColumns,
List<ExtraQueryIndex> extraQueryIndices) {
private void retrieval(final Class<?> clazz,
final String modelName,
final List<ModelColumn> modelColumns,
final List<ExtraQueryIndex> extraQueryIndices,
final int scopeId) {
if (log.isDebugEnabled()) {
log.debug("Analysis {} to generate Model.", clazz.getName());
}
......@@ -131,7 +132,7 @@ public class StorageModels implements IModelManager, ModelCreator, ModelManipula
if (column.dataType().isValue()) {
ValueColumnMetadata.INSTANCE.putIfAbsent(
modelName, column.columnName(), column.dataType(), column.function(),
column.defaultValue()
column.defaultValue(), scopeId
);
}
......@@ -152,7 +153,7 @@ public class StorageModels implements IModelManager, ModelCreator, ModelManipula
}
if (Objects.nonNull(clazz.getSuperclass())) {
retrieval(clazz.getSuperclass(), modelName, modelColumns, extraQueryIndices);
retrieval(clazz.getSuperclass(), modelName, modelColumns, extraQueryIndices, scopeId);
}
}
......
......@@ -37,6 +37,7 @@ import org.junit.runners.Parameterized;
import static com.google.common.collect.ImmutableMap.of;
import static java.util.Arrays.asList;
import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.SERVICE;
import static org.apache.skywalking.oap.server.core.storage.annotation.Column.ValueDataType.LABELED_VALUE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
......@@ -108,7 +109,7 @@ public class MetricsQueryUtilTest {
@Before
public void setup() {
ValueColumnMetadata.INSTANCE.putIfAbsent(
MODULE_NAME, "value", LABELED_VALUE, Function.None, DEFAULT_VALUE
MODULE_NAME, "value", LABELED_VALUE, Function.None, DEFAULT_VALUE, SERVICE
);
}
......
......@@ -26,6 +26,7 @@ import java.util.List;
import org.apache.skywalking.oap.server.core.CoreModule;
import org.apache.skywalking.oap.server.core.analysis.metrics.DataTable;
import org.apache.skywalking.oap.server.core.query.AggregationQueryService;
import org.apache.skywalking.oap.server.core.query.MetricDefinition;
import org.apache.skywalking.oap.server.core.query.MetricsMetadataQueryService;
import org.apache.skywalking.oap.server.core.query.MetricsQueryService;
import org.apache.skywalking.oap.server.core.query.PointOfTime;
......@@ -99,6 +100,16 @@ public class MetricsQuery implements GraphQLQueryResolver {
return getMetricsMetadataQueryService().typeOfMetrics(name);
}
/**
* Get the list of all available metrics in the current OAP server.
*
* @param regex to filter the metrics by name, if existing.
* @return all available metrics.
*/
public List<MetricDefinition> listMetrics(String regex) {
return getMetricsMetadataQueryService().listMetrics(regex);
}
/**
* Read metrics single value in the duration of required metrics
*/
......
Subproject commit 77afd814040c5532e5e4a68f5ef1694a80dad0c4
Subproject commit d3608e86f7c9aaf54aa4ad2c8d2afd09d0680dd6
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册