Skip to content

Commit

Permalink
Support rest server for lookout. (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
leizhiyuan authored and ujjboy committed Feb 20, 2019
1 parent e161d3c commit 9a174e1
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.rpc.lookout;

/**
* @author bystander
* @version $Id: RestConstants.java, v 0.1 2019年02月15日 13:17 bystander Exp $
*/
public class RestConstants {

public static final String REST_SERVICE_KEY = "rest_service";
public static final String REST_METHODNAME_KEY = "rest_methodname";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.rpc.lookout;

import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.event.EventBus;
import com.alipay.sofa.rpc.event.ServerSendEvent;
import com.alipay.sofa.rpc.event.rest.RestServerSendEvent;

import static com.alipay.sofa.rpc.common.RpcConstants.INTERNAL_KEY_PREFIX;

/**
* @author bystander
* @version $Id: RestLookoutAdapter.java, v 0.1 2019年01月28日 16:44 bystander Exp $
*/
public class RestLookoutAdapter {

public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) {
//this is special for rest
if (EventBus.isEnable(ServerSendEvent.class)) {
SofaRequest request = new SofaRequest();

String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
request.setTargetAppName(appName);
request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders()
.getHeaderString(RemotingConstants.HEAD_APP_NAME));
RpcInternalContext context = RpcInternalContext.getContext();
request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX +
RestConstants.REST_SERVICE_KEY));

request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX +
RestConstants.REST_METHODNAME_KEY));
request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
SofaResponse response = new SofaResponse();

if (restServerSendEvent.getThrowable() != null) {
response.setErrorMsg(restServerSendEvent.getThrowable().getMessage());
}
final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable());
EventBus.post(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alipay.sofa.rpc.event.ServerEndHandleEvent;
import com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent;
import com.alipay.sofa.rpc.event.rest.RestServerSendEvent;
import com.alipay.sofa.rpc.lookout.RestLookoutAdapter;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
Expand Down Expand Up @@ -111,6 +112,8 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except
if (EventBus.isEnable(RestServerSendEvent.class)) {
EventBus.post(new RestServerSendEvent(request, response, exception));
}

RestLookoutAdapter.sendRestServerSendEvent(new RestServerSendEvent(request, response, exception));
}

if (!request.getAsyncContext().isSuspended()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.rpc.server.rest;

import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.lookout.RestConstants;
import org.jboss.resteasy.core.interception.PostMatchContainerRequestContext;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

import static com.alipay.sofa.rpc.common.RpcConstants.INTERNAL_KEY_PREFIX;

/**
* @author <a href="mailto:[email protected]">liangen</a>
*/
@Provider
@Priority(100)
public class LookoutRequestFilter implements ContainerRequestFilter {

private static final Logger logger = LoggerFactory.getLogger(LookoutRequestFilter.class);

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {

try {
SofaResourceMethodInvoker resourceMethodInvoker = (SofaResourceMethodInvoker)
((PostMatchContainerRequestContext) requestContext)
.getResourceMethod();

SofaResourceFactory factory = resourceMethodInvoker.getResource();
String serviceName = factory.getServiceName();
String appName = factory.getAppName();

if (serviceName == null) {
serviceName = resourceMethodInvoker.getResourceClass().getName();
}

String methodName = resourceMethodInvoker.getMethod().getName();

RpcInternalContext context = RpcInternalContext.getContext();
context.setAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_SERVICE_KEY, serviceName);
context.setAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_METHODNAME_KEY, methodName);

context.setAttachment(RemotingConstants.HEAD_APP_NAME, appName);
} catch (Exception e) {
logger.error("the process of rest tracer server request occur error ", e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.lookout.api.Lookout;
import com.alipay.lookout.api.Measurement;
import com.alipay.lookout.api.Metric;
import com.alipay.lookout.api.NoopRegistry;
import com.alipay.lookout.api.Registry;
import com.alipay.lookout.api.Tag;
import com.alipay.lookout.core.DefaultRegistry;
Expand Down Expand Up @@ -84,8 +85,10 @@ public static void beforeClass() {
}

Registry registry = new DefaultRegistry();
Lookout.setRegistry(registry);

final Registry currentRegistry = Lookout.registry();
if (currentRegistry == NoopRegistry.INSTANCE) {
Lookout.setRegistry(registry);
}
RpcRunningState.setUnitTestMode(false);

try {
Expand Down
Loading

0 comments on commit 9a174e1

Please sign in to comment.