Skip to content

Commit

Permalink
Merge pull request #11567 from 985492783/glcc
Browse files Browse the repository at this point in the history
[ISSUE #10378]add lock query count and rt metrics
  • Loading branch information
JianweiWang authored Feb 20, 2024
2 parents 8966d8a + 5b6724e commit e682132
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public final class NacosMeterRegistryCenter {

// control plugin registeres.
public static final String CONTROL_DENIED_REGISTRY = "CONTROL_DENIED_REGISTRY";


public static final String LOCK_STABLE_REGISTRY = "LOCK_STABLE_REGISTRY";

private static final ConcurrentHashMap<String, CompositeMeterRegistry> METER_REGISTRIES = new ConcurrentHashMap<>();

private static CompositeMeterRegistry METER_REGISTRY = null;
Expand All @@ -61,7 +63,7 @@ public final class NacosMeterRegistryCenter {
Loggers.CORE.warn("Metrics init failed :", t);
}
registry(CORE_STABLE_REGISTRY, CONFIG_STABLE_REGISTRY, NAMING_STABLE_REGISTRY, TOPN_CONFIG_CHANGE_REGISTRY,
TOPN_SERVICE_CHANGE_REGISTRY, CONTROL_DENIED_REGISTRY);
TOPN_SERVICE_CHANGE_REGISTRY, CONTROL_DENIED_REGISTRY, LOCK_STABLE_REGISTRY);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.lock.aspect;

import com.alibaba.nacos.api.lock.remote.request.LockOperationRequest;
import com.alibaba.nacos.api.lock.remote.response.LockOperationResponse;
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.lock.monitor.LockMetricsMonitor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
* RequestLockAspect.
* @author goumang.zh@alibaba-inc.com
*/
@Aspect
@Component
public class RequestLockAspect {


/**
* count metrics and get handler time.
*/
@SuppressWarnings("checkstyle:linelength")
@Around(value = "execution(* com.alibaba.nacos.core.remote.RequestHandler.handleRequest(..)) && target(com.alibaba.nacos.lock.remote.rpc.handler.LockRequestHandler) && args(request, meta)", argNames = "pjp,request,meta")
public Object lockMeterPoint(ProceedingJoinPoint pjp, LockOperationRequest request, RequestMeta meta)
throws Throwable {
long st = System.currentTimeMillis();
try {
LockMetricsMonitor.getTotalMeter(request.getLockOperationEnum()).incrementAndGet();
LockOperationResponse result = (LockOperationResponse) pjp.proceed();
if (result.isSuccess()) {
LockMetricsMonitor.getSuccessMeter(request.getLockOperationEnum()).incrementAndGet();
}
return result;
} finally {
long rt = System.currentTimeMillis() - st;
LockMetricsMonitor.getLockHandlerTimer().record(rt, TimeUnit.MILLISECONDS);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.lock.monitor;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
* memory monitor.
*
* @author goumang.zh@alibaba-inc.com
*/
@Service
public class LockMemoryMonitor {

/**
* auto clean metrics per-day.
*/
@Scheduled(cron = "0 0 0 * * ?")
public void clear() {
LockMetricsMonitor.getGrpcLockTotal().set(0);
LockMetricsMonitor.getGrpcLockSuccess().set(0);
LockMetricsMonitor.getGrpcUnLockTotal().set(0);
LockMetricsMonitor.getGrpcUnLockSuccess().set(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.lock.monitor;

import com.alibaba.nacos.api.lock.remote.LockOperationEnum;
import com.alibaba.nacos.core.monitor.NacosMeterRegistryCenter;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* MetricsMonitor.
* @author goumang.zh@alibaba-inc.com
*/
public class LockMetricsMonitor {

private static final String METER_REGISTRY = NacosMeterRegistryCenter.LOCK_STABLE_REGISTRY;

private static AtomicInteger grpcLockSuccess = new AtomicInteger();

private static AtomicInteger grpcUnLockSuccess = new AtomicInteger();

private static AtomicInteger grpcLockTotal = new AtomicInteger();

private static AtomicInteger grpcUnLockTotal = new AtomicInteger();

private static AtomicInteger aliveLockCount = new AtomicInteger();

static {
ImmutableTag immutableTag = new ImmutableTag("module", "lock");
List<Tag> tags = new ArrayList<>();
tags.add(immutableTag);
tags.add(new ImmutableTag("name", "grpcLockTotal"));
NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcLockTotal);

tags = new ArrayList<>();
tags.add(immutableTag);
tags.add(new ImmutableTag("name", "grpcLockSuccess"));
NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcLockSuccess);

tags = new ArrayList<>();
tags.add(immutableTag);
tags.add(new ImmutableTag("name", "grpcUnLockTotal"));
NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcUnLockTotal);

tags = new ArrayList<>();
tags.add(immutableTag);
tags.add(new ImmutableTag("name", "grpcUnLockSuccess"));
NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcUnLockSuccess);

tags = new ArrayList<>();
tags.add(immutableTag);
tags.add(new ImmutableTag("name", "aliveLockCount"));
NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, aliveLockCount);
}

public static AtomicInteger getGrpcLockSuccess() {
return grpcLockSuccess;
}

public static AtomicInteger getGrpcUnLockSuccess() {
return grpcUnLockSuccess;
}

public static AtomicInteger getGrpcLockTotal() {
return grpcLockTotal;
}

public static AtomicInteger getGrpcUnLockTotal() {
return grpcUnLockTotal;
}

public static Timer getLockHandlerTimer() {
return NacosMeterRegistryCenter
.timer(METER_REGISTRY, "nacos_timer", "module", "lock", "name", "lockHandlerRt");
}

public static AtomicInteger getSuccessMeter(LockOperationEnum lockOperationEnum) {
if (lockOperationEnum == LockOperationEnum.ACQUIRE) {
return grpcLockSuccess;
} else {
return grpcUnLockSuccess;
}
}

public static AtomicInteger getTotalMeter(LockOperationEnum lockOperationEnum) {
if (lockOperationEnum == LockOperationEnum.ACQUIRE) {
return grpcLockTotal;
} else {
return grpcUnLockTotal;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public Boolean lock(LockInstance lockInstance) {
lockInstance.getLockType(), paramSize, e.getMessage());
throw e;
} catch (Exception e) {
LOGGER.error("lock fail.", e);
throw new NacosLockException("tryLock error.", e);
}
}
Expand Down

0 comments on commit e682132

Please sign in to comment.