Skip to content

Commit

Permalink
[pinpoint-apm#9380] add location field
Browse files Browse the repository at this point in the history
Fix intelliJ warning
   * Warning:(91, 29) 'StringBuilder sb' can be replaced with 'String'
  • Loading branch information
feelform committed Nov 28, 2022
1 parent 39ecdce commit 1af5e9a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import static org.mockito.Mockito.*;

public class GrpcApiMetaDataHandlerTest {
// ApiMetaDataBo{agentId='express-node-sample-id', startTime=1668495162817, apiId=11, apiInfo='express.Function.proto.get(path, callback)', lineNumber=177, methodTypeEnum=DEFAULT}
// from Node agent [11, 'express.Function.proto.get(path, callback)', 24, null, '/Users/workspace/pinpoint/@pinpoint-naver-apm/pinpoint-agent-node/samples/express/src/routes/index.js']
@Test
public void stubToApiMetaData() {
// ApiMetaDataBo{agentId='express-node-sample-id', startTime=1668495162817, apiId=11, apiInfo='express.Function.proto.get(path, callback)', lineNumber=177, methodTypeEnum=DEFAULT}
// from Node agent [11, 'express.Function.proto.get(path, callback)', 24, null, '/Users/workspace/pinpoint/@pinpoint-naver-apm/pinpoint-agent-node/samples/express/src/routes/index.js']
ApiMetaDataService mockedService = mock(ApiMetaDataService.class);
GrpcApiMetaDataHandler dut = new GrpcApiMetaDataHandler(mockedService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ public List<ApiMetaDataBo> mapRow(Result result, int rowNum) throws Exception {
if (buffer.hasRemaining()) {
methodTypeEnum = MethodTypeEnum.valueOf(buffer.readInt());
}
String location = null;
if (buffer.hasRemaining()) {
location = buffer.readPrefixedString();
}

ApiMetaDataBo apiMetaDataBo = new ApiMetaDataBo(key.getAgentId(), key.getAgentStartTime(), key.getId(), lineNumber, methodTypeEnum, apiInfo);
ApiMetaDataBo.Builder builder = new ApiMetaDataBo.Builder(key.getAgentId(), key.getAgentStartTime(), key.getId(), lineNumber, methodTypeEnum, apiInfo);
if (location != null) {
builder.setLocation(location);
}
ApiMetaDataBo apiMetaDataBo = builder.build();

apiMetaDataList.add(apiMetaDataBo);
if (logger.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.navercorp.pinpoint.web.mapper;

import com.navercorp.pinpoint.common.buffer.AutomaticBuffer;
import com.navercorp.pinpoint.common.buffer.Buffer;
import com.navercorp.pinpoint.common.server.bo.ApiMetaDataBo;
import com.navercorp.pinpoint.common.server.bo.MethodTypeEnum;
import com.navercorp.pinpoint.common.server.bo.serializer.metadata.MetadataEncoder;
import com.navercorp.pinpoint.common.server.hbase.DistributorConfiguration;
import com.sematext.hbase.wd.RowKeyDistributorByHashPrefix;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ApiMetaDataMapperTest {
// from node: ApiMetaDataBo{agentId='express-node-sample-id', startTime=1669280767548, apiId=12, apiInfo='express.Function.proto.get(path, callback)', lineNumber=169, methodTypeEnum=DEFAULT, location='/Users/workspace/pinpoint/@pinpoint-naver-apm/pinpoint-agent-node/samples/express/src/routes/index.js'}
@Test
public void testMapRow() throws Exception {
ApiMetaDataBo expected = new ApiMetaDataBo.Builder("express-node-sample-id", 1669280767548L, 12, 169, MethodTypeEnum.DEFAULT, "express.Function.proto.get(path, callback)")
.setLocation("/Users/workspace/pinpoint/@pinpoint-naver-apm/pinpoint-agent-node/samples/express/src/routes/index.js")
.build();

RowKeyDistributorByHashPrefix givenRowKeyDistributorByHashPrefix = new DistributorConfiguration().getMetadataRowKeyDistributor();
final byte[] rowKey = givenRowKeyDistributorByHashPrefix.getDistributedKey(new MetadataEncoder().encodeRowKey(expected));
final Buffer buffer = new AutomaticBuffer(64);
final String api = expected.getApiInfo();
buffer.putPrefixedString(api);
buffer.putInt(expected.getLineNumber());
buffer.putInt(expected.getMethodTypeEnum().getCode());

String location = expected.getLocation();
if (location != null) {
buffer.putPrefixedString(location);
}
byte[] bufferArray = buffer.getBuffer();
byte[] valueArray = Bytes.toBytes(1L);
Cell cell = CellUtil.createCell(HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, bufferArray, HConstants.LATEST_TIMESTAMP, KeyValue.Type.Maximum.getCode(), valueArray);

Result mockedResult = mock(Result.class);
when(mockedResult.rawCells()).thenReturn(new Cell[] { cell });
when(mockedResult.getRow()).thenReturn(rowKey);

ApiMetaDataMapper dut = new ApiMetaDataMapper(givenRowKeyDistributorByHashPrefix);
ApiMetaDataBo actual = dut.mapRow(mockedResult, 0).get(0);

assertThat(actual).extracting("agentId", "startTime", "apiId", "apiInfo", "lineNumber", "methodTypeEnum", "location")
.contains(expected.getAgentId(), expected.getAgentStartTime(), expected.getId(), expected.getApiInfo(), expected.getLineNumber(), expected.getMethodTypeEnum(), expected.getLocation());
}
}

0 comments on commit 1af5e9a

Please sign in to comment.