Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recorder support #203

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.alipay.sofa.common</groupId>
<artifactId>sofa-common-tools</artifactId>
<version>2.1.0</version>
<version>2.1.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/alipay/sofa/common/insight/NoopRecorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 com.alipay.sofa.common.insight;

/**
* @author muqingcai
* @version 2024年4月19日 上午9:54:51
*/
public class NoopRecorder implements Recorder {
public static final NoopRecorder INSTANCE = new NoopRecorder();

private NoopRecorder() {
}

@Override
public void start(RecordScene scene, RecordContext context) {

}

@Override
public void stop(RecordScene scene, RecordContext context) {

}
}
222 changes: 222 additions & 0 deletions src/main/java/com/alipay/sofa/common/insight/RecordContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* 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 com.alipay.sofa.common.insight;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author muqingcai
* @version 2024年4月19日 上午9:54:59
*/
public class RecordContext {
private int requestId;
private String traceId;
private String rpcId;
private String targetServiceUniqueName;
private String methodName;
private String moduleName;
private String beanName;
private Map<String, Object> extraInfo;

/**
* Constructor.
*/
public RecordContext() {
this.extraInfo = new ConcurrentHashMap<>();
}
Comment on lines +39 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure thread safety for extraInfo initialization in all constructors.

Consider initializing extraInfo in the field declaration to ensure it's always initialized properly, regardless of the constructor used:

- private Map<String, Object> extraInfo;
+ private Map<String, Object> extraInfo = new ConcurrentHashMap<>();

Then, remove the initialization from all constructors.

Also applies to: 48-51, 59-63


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public RecordContext() {
this.extraInfo = new ConcurrentHashMap<>();
}
private Map<String, Object> extraInfo = new ConcurrentHashMap<>();
public RecordContext() {
}


/**
* Constructor.
*
* @param requestId the request id
*/
public RecordContext(int requestId) {
this.requestId = requestId;
this.extraInfo = new ConcurrentHashMap<>();
}

/**
* Constructor.
*
* @param moduleName the module name
* @param beanName the bean name
*/
public RecordContext(String moduleName, String beanName) {
this.extraInfo = new ConcurrentHashMap<>();
this.moduleName = moduleName;
this.beanName = beanName;
}

/**
* Gets get request id.
*
* @return the get request id
*/
public int getRequestId() {
return requestId;
}

/**
* Sets set request id.
*
* @param requestId the request id
*/
public void setRequestId(int requestId) {
this.requestId = requestId;
}

/**
* Gets get trace id.
*
* @return the get trace id
*/
public String getTraceId() {
return traceId;
}

/**
* Sets set trace id.
*
* @param traceId the trace id
*/
public void setTraceId(String traceId) {
this.traceId = traceId;
}

/**
* Gets get rpc id.
*
* @return the get rpc id
*/
public String getRpcId() {
return rpcId;
}

/**
* Sets set rpc id.
*
* @param rpcId the rpc id
*/
public void setRpcId(String rpcId) {
this.rpcId = rpcId;
}

/**
* Gets get target service unique name.
*
* @return the get target service unique name
*/
public String getTargetServiceUniqueName() {
return targetServiceUniqueName;
}

/**
* Sets set target service unique name.
*
* @param targetServiceUniqueName the target service unique name
*/
public void setTargetServiceUniqueName(String targetServiceUniqueName) {
this.targetServiceUniqueName = targetServiceUniqueName;
}

/**
* Gets get method name.
*
* @return the get method name
*/
public String getMethodName() {
return methodName;
}

/**
* Sets set method name.
*
* @param methodName the method name
*/
public void setMethodName(String methodName) {
this.methodName = methodName;
}

/**
* Gets get module name.
*
* @return the get module name
*/
public String getModuleName() {
return moduleName;
}

/**
* Sets set module name.
*
* @param moduleName the module name
*/
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}

/**
* Gets get bean name.
*
* @return the get bean name
*/
public String getBeanName() {
return beanName;
}

/**
* Sets set bean name.
*
* @param beanName the bean name
*/
public void setBeanName(String beanName) {
this.beanName = beanName;
}

/**
* Gets get extra info.
*
* @return the get extra info
*/
public Map<String, Object> getExtraInfo() {
return extraInfo;
}

/**
* Sets set extra info.
*
* @param extraInfo the extra info
*/
public void setExtraInfo(Map<String, Object> extraInfo) {
this.extraInfo = extraInfo;
}

/**
* To string string.
*
* @return the string
*/
@Override
public String toString() {
return "RecordContext{" + "requestId=" + requestId + ", traceId='" + traceId + '\''
+ ", rpcId='" + rpcId + '\'' + ", targetServiceUniqueName='"
+ targetServiceUniqueName + '\'' + ", methodName='" + methodName + '\''
+ ", moduleName='" + moduleName + '\'' + ", beanName='" + beanName + '\''
+ ", extraInfo=" + extraInfo + '}';
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/alipay/sofa/common/insight/RecordScene.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 com.alipay.sofa.common.insight;

/**
* @author muqingcai
* @version 2024年4月19日 上午9:55:54
*/
public enum RecordScene {
/**
* record 场景
*/
BOLT_REQUEST_HANDLE("boltRequestHandle", "bolt 协议 RPC 请求处理"),

TR_REQUEST_HANDLE("trRequestHandle", "tr 协议 RPC 请求处理"),

SOFA_STARTUP("sofaStartup", "应用启动"),

SPRING_BEAN_REFRESH("springBeanRefresh", "应用 bean 刷新");

private final String scene;
private final String desc;

RecordScene(String scene, String desc) {
this.scene = scene;
this.desc = desc;
}

/**
* Gets get scene.
*
* @return the get scene
*/
public String getScene() {
return scene;
}

/**
* Gets get desc.
*
* @return the get desc
*/
public String getDesc() {
return desc;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/alipay/sofa/common/insight/Recorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 com.alipay.sofa.common.insight;

/**
* @author muqingcai
* @version 2024年4月11日 下午9:56:03
*/
public interface Recorder {

/**
* Start record
*
* @param scene the scene
* @param context the context
*/
void start(RecordScene scene, RecordContext context);

/**
* Stop record
*
* @param scene the scene
* @param context the context
*/
void stop(RecordScene scene, RecordContext context);
}
Loading
Loading