You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2020/11/19 14:27:24 UTC
[skywalking] branch master updated: Provide `listMetrics` GraphQL
query service. (#5866)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/master by this push:
new 67bc8f8 Provide `listMetrics` GraphQL query service. (#5866)
67bc8f8 is described below
commit 67bc8f8b7db13b7cbe97a3f50ccf6b6485c7f71d
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Thu Nov 19 22:26:54 2020 +0800
Provide `listMetrics` GraphQL query service. (#5866)
* Provide `listMetrics` GraphQL query service.
* Update chagnelog.
---
CHANGES.md | 1 +
.../oap/server/core/query/MetricDefinition.java | 44 ++++++++++++++++++++++
.../core/query/MetricsMetadataQueryService.java | 21 +++++++++++
.../oap/server/core/source/DefaultScopeDefine.java | 28 ++++++++++++++
.../storage/annotation/ValueColumnMetadata.java | 16 +++++++-
.../server/core/storage/model/StorageModels.java | 15 ++++----
.../core/storage/query/MetricsQueryUtilTest.java | 3 +-
.../oap/query/graphql/resolver/MetricsQuery.java | 11 ++++++
.../src/main/resources/query-protocol | 2 +-
9 files changed, 130 insertions(+), 11 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index 2984813..72fd2a9 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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.
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricDefinition.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricDefinition.java
new file mode 100644
index 0000000..ddd28b3
--- /dev/null
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricDefinition.java
@@ -0,0 +1,44 @@
+/*
+ * 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;
+}
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricsMetadataQueryService.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricsMetadataQueryService.java
index 93483a1..f504c15 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricsMetadataQueryService.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricsMetadataQueryService.java
@@ -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());
+ }
}
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/DefaultScopeDefine.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/DefaultScopeDefine.java
index 17bf1bb..c6d91fe 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/DefaultScopeDefine.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/DefaultScopeDefine.java
@@ -296,6 +296,34 @@ public class DefaultScopeDefine {
}
/**
+ * 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.
*
* @param scopeName of the default columns
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/ValueColumnMetadata.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/ValueColumnMetadata.java
index 76af49d..67b2318 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/ValueColumnMetadata.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/ValueColumnMetadata.java
@@ -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;
}
}
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java
index 600dbe9..b8d3b71 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java
@@ -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);
}
}
diff --git a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/query/MetricsQueryUtilTest.java b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/query/MetricsQueryUtilTest.java
index 683db20..d3e97f9 100644
--- a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/query/MetricsQueryUtilTest.java
+++ b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/query/MetricsQueryUtilTest.java
@@ -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
);
}
diff --git a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsQuery.java b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsQuery.java
index 3c3d40a..2ed2d0b 100644
--- a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsQuery.java
+++ b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsQuery.java
@@ -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;
@@ -100,6 +101,16 @@ public class MetricsQuery implements GraphQLQueryResolver {
}
/**
+ * 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
*/
public long readMetricsValue(MetricsCondition condition, Duration duration) throws IOException {
diff --git a/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol b/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
index 77afd81..d3608e8 160000
--- a/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
+++ b/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
@@ -1 +1 @@
-Subproject commit 77afd814040c5532e5e4a68f5ef1694a80dad0c4
+Subproject commit d3608e86f7c9aaf54aa4ad2c8d2afd09d0680dd6