MetricsUtils.java 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.proxy.backend.metrics;
19

L
Liang Zhang 已提交
20 21
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
22
import org.apache.shardingsphere.control.panel.spi.engine.SingletonFacadeEngine;
23 24 25
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
L
Liang Zhang 已提交
26 27 28
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
29 30 31 32

import java.util.Collection;

/**
L
Liang Zhang 已提交
33
 * Metrics utility.
34
 */
L
Liang Zhang 已提交
35
@NoArgsConstructor(access = AccessLevel.PRIVATE)
36 37 38
public final class MetricsUtils {
    
    /**
kimmking's avatar
kimmking 已提交
39
     * Collect sharding metrics.
40 41 42
     *
     * @param routeUnits route units
     */
kimmking's avatar
kimmking 已提交
43
    public static void collectRouteUnitMetrics(final Collection<RouteUnit> routeUnits) {
44 45 46 47
        if (!routeUnits.isEmpty()) {
            for (RouteUnit each : routeUnits) {
                Collection<RouteMapper> tableMappers = each.getTableMappers();
                RouteMapper dataSourceMapper = each.getDataSourceMapper();
48
                SingletonFacadeEngine.buildMetrics()
49
                        .ifPresent(metricsHandlerFacade -> metricsHandlerFacade.counterIncrement(MetricsLabelEnum.SHARDING_DATASOURCE.getName(), dataSourceMapper.getActualName()));
50
                for (RouteMapper table : tableMappers) {
51
                    SingletonFacadeEngine.buildMetrics().ifPresent(metricsHandlerFacade -> metricsHandlerFacade.counterIncrement(MetricsLabelEnum.SHARDING_TABLE.getName(), table.getActualName()));
52 53 54 55 56 57
                }
            }
        }
    }
    
    /**
kimmking's avatar
kimmking 已提交
58
     * Collect transaction metric.
59 60 61
     *
     * @param labelValue label value
     */
kimmking's avatar
kimmking 已提交
62
    public static void collectTransactionMetric(final String labelValue) {
63
        SingletonFacadeEngine.buildMetrics().ifPresent(metricsHandlerFacade -> metricsHandlerFacade.counterIncrement(MetricsLabelEnum.TRANSACTION.getName(), labelValue));
64
    }
65 66
    
    /**
kimmking's avatar
kimmking 已提交
67
     * Collect sharding rule metrics.
68 69 70 71
     *
     * @param routeContext route context
     * @param rules rules
     */
kimmking's avatar
kimmking 已提交
72
    public static void collectShardingRuleMetrics(final RouteContext routeContext, final Collection<ShardingSphereRule> rules) {
73 74
        routeContext.getRouteResult().getActualDataSourceNames().forEach(dataSourceName -> rules.forEach(each -> {
            if (each instanceof ShadowRule && ((ShadowRule) each).getShadowMappings().containsValue(dataSourceName)) {
75
                SingletonFacadeEngine.buildMetrics().ifPresent(metricsHandlerFacade -> metricsHandlerFacade.counterIncrement(MetricsLabelEnum.SHADOW_HIT_TOTAL.getName()));
76 77 78
            }
        }));
    }
79
}