Search in sources :

Example 1 with Metrics

use of org.apache.skywalking.oap.server.core.analysis.metrics.Metrics in project skywalking by apache.

the class MetricsDAO method multiGet.

@Override
public List<Metrics> multiGet(Model model, List<Metrics> metrics) throws IOException {
    final TableMetaInfo metaInfo = TableMetaInfo.get(model.getName());
    final String queryStr;
    if (model.getName().endsWith("_traffic")) {
        final Function<Metrics, Clause> clauseFunction;
        switch(model.getName()) {
            case EndpointTraffic.INDEX_NAME:
                {
                    clauseFunction = m -> eq(TagName.SERVICE_ID, ((EndpointTraffic) m).getServiceId());
                    break;
                }
            case ServiceTraffic.INDEX_NAME:
                {
                    clauseFunction = m -> eq(TagName.NAME, ((ServiceTraffic) m).getName());
                    break;
                }
            case InstanceTraffic.INDEX_NAME:
                {
                    clauseFunction = m -> eq(TagName.SERVICE_ID, ((InstanceTraffic) m).getServiceId());
                    break;
                }
            default:
                throw new IOException("Unknown metadata type, " + model.getName());
        }
        queryStr = metrics.stream().map(m -> select().raw(ALL_FIELDS).from(client.getDatabase(), model.getName()).where(clauseFunction.apply(m)).and(eq(ID_COLUMN, m.id())).buildQueryString()).collect(Collectors.joining(";"));
    } else {
        queryStr = metrics.stream().map(m -> select().raw(ALL_FIELDS).from(client.getDatabase(), model.getName()).where(eq(TagName.TIME_BUCKET, String.valueOf(m.getTimeBucket()))).and(eq(ID_COLUMN, m.id())).buildQueryString()).collect(Collectors.joining(";"));
    }
    final Query query = new Query(queryStr);
    final List<QueryResult.Result> results = client.query(query);
    if (log.isDebugEnabled()) {
        log.debug("SQL: {} result: {}", query.getCommand(), results);
    }
    if (CollectionUtils.isEmpty(results)) {
        return Collections.emptyList();
    }
    final List<Metrics> newMetrics = Lists.newArrayList();
    final Map<String, String> storageAndColumnMap = metaInfo.getStorageAndColumnMap();
    results.stream().map(QueryResult.Result::getSeries).filter(Objects::nonNull).flatMap(Collection::stream).filter(Objects::nonNull).forEach(series -> {
        final List<String> columns = series.getColumns();
        series.getValues().forEach(values -> {
            Map<String, Object> data = Maps.newHashMap();
            for (int i = 1; i < columns.size(); i++) {
                Object value = values.get(i);
                if (value instanceof StorageDataComplexObject) {
                    value = ((StorageDataComplexObject) value).toStorageData();
                }
                data.put(storageAndColumnMap.get(columns.get(i)), value);
            }
            newMetrics.add(storageBuilder.storage2Entity(data));
        });
    });
    return newMetrics;
}
Also used : TagName(org.apache.skywalking.oap.server.storage.plugin.influxdb.InfluxConstants.TagName) TimeBucket(org.apache.skywalking.oap.server.core.analysis.TimeBucket) Query(org.influxdb.dto.Query) ID_COLUMN(org.apache.skywalking.oap.server.storage.plugin.influxdb.InfluxConstants.ID_COLUMN) Function(java.util.function.Function) Clause(org.influxdb.querybuilder.clauses.Clause) InfluxClient(org.apache.skywalking.oap.server.storage.plugin.influxdb.InfluxClient) QueryBuilder.select(org.influxdb.querybuilder.BuiltQuery.QueryBuilder.select) ALL_FIELDS(org.apache.skywalking.oap.server.storage.plugin.influxdb.InfluxConstants.ALL_FIELDS) Lists(com.google.common.collect.Lists) StorageDataComplexObject(org.apache.skywalking.oap.server.core.storage.type.StorageDataComplexObject) InsertRequest(org.apache.skywalking.oap.server.library.client.request.InsertRequest) Map(java.util.Map) UpdateRequest(org.apache.skywalking.oap.server.library.client.request.UpdateRequest) IMetricsDAO(org.apache.skywalking.oap.server.core.storage.IMetricsDAO) TableMetaInfo(org.apache.skywalking.oap.server.storage.plugin.influxdb.TableMetaInfo) Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) EndpointTraffic(org.apache.skywalking.oap.server.core.analysis.manual.endpoint.EndpointTraffic) CollectionUtils(org.apache.skywalking.oap.server.library.util.CollectionUtils) Collection(java.util.Collection) ServiceTraffic(org.apache.skywalking.oap.server.core.analysis.manual.service.ServiceTraffic) IOException(java.io.IOException) StorageHashMapBuilder(org.apache.skywalking.oap.server.core.storage.StorageHashMapBuilder) Model(org.apache.skywalking.oap.server.core.storage.model.Model) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) QueryResult(org.influxdb.dto.QueryResult) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) InstanceTraffic(org.apache.skywalking.oap.server.core.analysis.manual.instance.InstanceTraffic) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) QueryBuilder.eq(org.influxdb.querybuilder.BuiltQuery.QueryBuilder.eq) Collections(java.util.Collections) Query(org.influxdb.dto.Query) IOException(java.io.IOException) QueryResult(org.influxdb.dto.QueryResult) TableMetaInfo(org.apache.skywalking.oap.server.storage.plugin.influxdb.TableMetaInfo) Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) Collection(java.util.Collection) StorageDataComplexObject(org.apache.skywalking.oap.server.core.storage.type.StorageDataComplexObject) Clause(org.influxdb.querybuilder.clauses.Clause) StorageDataComplexObject(org.apache.skywalking.oap.server.core.storage.type.StorageDataComplexObject)

Example 2 with Metrics

use of org.apache.skywalking.oap.server.core.analysis.metrics.Metrics in project incubator-skywalking by apache.

the class IoTDBMetricsDAO method multiGet.

@Override
public List<Metrics> multiGet(Model model, List<Metrics> metrics) throws IOException {
    StringBuilder query = new StringBuilder();
    query.append("select * from ");
    for (Metrics metric : metrics) {
        query.append(", ");
        query = client.addModelPath(query, model.getName());
        query.append(IoTDBClient.DOT).append(client.indexValue2LayerName(metric.id()));
    }
    query.append(IoTDBClient.ALIGN_BY_DEVICE);
    String queryString = query.toString().replaceFirst(", ", "");
    List<? super StorageData> storageDataList = client.filterQuery(model.getName(), queryString, storageBuilder);
    List<Metrics> newMetrics = new ArrayList<>(storageDataList.size());
    storageDataList.forEach(storageData -> newMetrics.add((Metrics) storageData));
    return newMetrics;
}
Also used : Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) ArrayList(java.util.ArrayList)

Example 3 with Metrics

use of org.apache.skywalking.oap.server.core.analysis.metrics.Metrics in project incubator-skywalking by apache.

the class MetricsPersistentWorker method buildBatchRequests.

@Override
public List<PrepareRequest> buildBatchRequests() {
    if (persistentCounter++ % persistentMod != 0) {
        return Collections.emptyList();
    }
    final List<Metrics> lastCollection = getCache().read();
    long start = System.currentTimeMillis();
    if (lastCollection.size() == 0) {
        return Collections.emptyList();
    }
    /*
         * Hard coded the max size. This only affect the multiIDRead if the data doesn't hit the cache.
         */
    int maxBatchGetSize = 2000;
    final int batchSize = Math.min(maxBatchGetSize, lastCollection.size());
    List<Metrics> metricsList = new ArrayList<>();
    List<PrepareRequest> prepareRequests = new ArrayList<>(lastCollection.size());
    for (Metrics data : lastCollection) {
        transWorker.ifPresent(metricsTransWorker -> metricsTransWorker.in(data));
        metricsList.add(data);
        if (metricsList.size() == batchSize) {
            flushDataToStorage(metricsList, prepareRequests);
        }
    }
    if (metricsList.size() > 0) {
        flushDataToStorage(metricsList, prepareRequests);
    }
    if (prepareRequests.size() > 0) {
        log.debug("prepare batch requests for model {}, took time: {}, size: {}", model.getName(), System.currentTimeMillis() - start, prepareRequests.size());
    }
    return prepareRequests;
}
Also used : Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) CounterMetrics(org.apache.skywalking.oap.server.telemetry.api.CounterMetrics) ArrayList(java.util.ArrayList) PrepareRequest(org.apache.skywalking.oap.server.library.client.request.PrepareRequest)

Example 4 with Metrics

use of org.apache.skywalking.oap.server.core.analysis.metrics.Metrics in project incubator-skywalking by apache.

the class MeterSystem method create.

/**
 * Create streaming calculation of the given metrics name. This methods is synchronized due to heavy implementation
 * including creating dynamic class. Don't use this in concurrency runtime.
 *
 * @param metricsName  The name used as the storage eneity and in the query stage.
 * @param functionName The function provided through {@link MeterFunction}.
 * @throws IllegalArgumentException if the parameter can't match the expectation.
 * @throws UnexpectedException      if binary code manipulation fails or stream core failure.
 */
public synchronized <T> void create(String metricsName, String functionName, ScopeType type, Class<T> dataType) throws IllegalArgumentException {
    /**
     * Create a new meter class dynamically.
     */
    final Class<? extends AcceptableValue> meterFunction = functionRegister.get(functionName);
    if (meterFunction == null) {
        throw new IllegalArgumentException("Function " + functionName + " can't be found.");
    }
    boolean foundDataType = false;
    String acceptance = null;
    for (final Type genericInterface : meterFunction.getGenericInterfaces()) {
        if (genericInterface instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
            if (parameterizedType.getRawType().getTypeName().equals(AcceptableValue.class.getName())) {
                Type[] arguments = parameterizedType.getActualTypeArguments();
                if (arguments[0].equals(dataType)) {
                    foundDataType = true;
                } else {
                    acceptance = arguments[0].getTypeName();
                }
            }
            if (foundDataType) {
                break;
            }
        }
    }
    if (!foundDataType) {
        throw new IllegalArgumentException("Function " + functionName + " requires <" + acceptance + "> in AcceptableValue" + " but using " + dataType.getName() + " in the creation");
    }
    final CtClass parentClass;
    try {
        parentClass = classPool.get(meterFunction.getCanonicalName());
        if (!Metrics.class.isAssignableFrom(meterFunction)) {
            throw new IllegalArgumentException("Function " + functionName + " doesn't inherit from Metrics.");
        }
    } catch (NotFoundException e) {
        throw new IllegalArgumentException("Function " + functionName + " can't be found by javaassist.");
    }
    final String className = formatName(metricsName);
    /**
     * Check whether the metrics class is already defined or not
     */
    try {
        CtClass existingMetric = classPool.get(METER_CLASS_PACKAGE + className);
        if (existingMetric.getSuperclass() != parentClass || type != meterPrototypes.get(metricsName).getScopeType()) {
            throw new IllegalArgumentException(metricsName + " has been defined, but calculate function or/are scope type is/are different.");
        }
        log.info("Metric {} is already defined, so skip the metric creation.", metricsName);
        return;
    } catch (NotFoundException e) {
    }
    CtClass metricsClass = classPool.makeClass(METER_CLASS_PACKAGE + className, parentClass);
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsClass);
        metricsClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new UnexpectedException(e.getMessage(), e);
    }
    /**
     * Generate `AcceptableValue<T> createNew()` method.
     */
    try {
        metricsClass.addMethod(CtNewMethod.make("" + "public org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue createNew() {" + "    org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue meterVar = new " + METER_CLASS_PACKAGE + className + "();" + "    ((org.apache.skywalking.oap.server.core.analysis.meter.Meter)meterVar).initMeta(\"" + metricsName + "\", " + type.getScopeId() + ");" + "    return meterVar;" + " }", metricsClass));
    } catch (CannotCompileException e) {
        log.error("Can't generate createNew method for " + className + ".", e);
        throw new UnexpectedException(e.getMessage(), e);
    }
    Class targetClass;
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            targetClass = metricsClass.toClass(MeterSystem.class.getClassLoader(), null);
        } else {
            targetClass = metricsClass.toClass(MeterClassPackageHolder.class);
        }
        AcceptableValue prototype = (AcceptableValue) targetClass.newInstance();
        meterPrototypes.put(metricsName, new MeterDefinition(type, prototype, dataType));
        log.debug("Generate metrics class, " + metricsClass.getName());
        MetricsStreamProcessor.getInstance().create(manager, new StreamDefinition(metricsName, type.getScopeId(), prototype.builder(), MetricsStreamProcessor.class), targetClass);
    } catch (CannotCompileException | IllegalAccessException | InstantiationException | StorageException e) {
        log.error("Can't compile/load/init " + className + ".", e);
        throw new UnexpectedException(e.getMessage(), e);
    }
}
Also used : UnexpectedException(org.apache.skywalking.oap.server.core.UnexpectedException) StreamDefinition(org.apache.skywalking.oap.server.core.analysis.StreamDefinition) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) MetricsStreamProcessor(org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor) CtConstructor(javassist.CtConstructor) ParameterizedType(java.lang.reflect.ParameterizedType) CtClass(javassist.CtClass) MeterClassPackageHolder(org.apache.skywalking.oap.server.core.analysis.meter.dynamic.MeterClassPackageHolder) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) AcceptableValue(org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue) CtClass(javassist.CtClass) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException)

Example 5 with Metrics

use of org.apache.skywalking.oap.server.core.analysis.metrics.Metrics in project incubator-skywalking by apache.

the class H2MetricsDAO method multiGet.

@Override
public List<Metrics> multiGet(Model model, List<Metrics> metrics) throws IOException {
    String[] ids = metrics.stream().map(Metrics::id).collect(Collectors.toList()).toArray(new String[] {});
    List<StorageData> storageDataList = getByIDs(h2Client, model.getName(), ids, storageBuilder);
    List<Metrics> result = new ArrayList<>(storageDataList.size());
    for (StorageData storageData : storageDataList) {
        result.add((Metrics) storageData);
    }
    return result;
}
Also used : Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) ArrayList(java.util.ArrayList) StorageData(org.apache.skywalking.oap.server.core.storage.StorageData)

Aggregations

Metrics (org.apache.skywalking.oap.server.core.analysis.metrics.Metrics)26 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)6 List (java.util.List)6 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 TimeBucket (org.apache.skywalking.oap.server.core.analysis.TimeBucket)6 IMetricsDAO (org.apache.skywalking.oap.server.core.storage.IMetricsDAO)6 Model (org.apache.skywalking.oap.server.core.storage.model.Model)6 CounterMetrics (org.apache.skywalking.oap.server.telemetry.api.CounterMetrics)6 Collections (java.util.Collections)4 Objects (java.util.Objects)4 DownSampling (org.apache.skywalking.oap.server.core.analysis.DownSampling)4 MetricsMetaInfo (org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo)4 InsertRequest (org.apache.skywalking.oap.server.library.client.request.InsertRequest)4 UpdateRequest (org.apache.skywalking.oap.server.library.client.request.UpdateRequest)4 CollectionUtils (org.apache.skywalking.oap.server.library.util.CollectionUtils)4 Lists (com.google.common.collect.Lists)2