forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#4558] Add gRPC StatusError
- Loading branch information
1 parent
dac7f65
commit 6e6a2b3
Showing
7 changed files
with
211 additions
and
6 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2019 NAVER Corp. | ||
* | ||
* 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.navercorp.pinpoint.grpc; | ||
|
||
/** | ||
* @author jaehong.kim | ||
*/ | ||
public interface StatusError { | ||
|
||
boolean isSimpleError(); | ||
|
||
String getMessage(); | ||
|
||
Throwable getThrowable(); | ||
} |
110 changes: 110 additions & 0 deletions
110
grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2019 NAVER Corp. | ||
* | ||
* 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.navercorp.pinpoint.grpc; | ||
|
||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
|
||
/** | ||
* @author jaehong.kim | ||
*/ | ||
public class StatusErrors { | ||
private static final String CONNECTION_REFUSED_MESSAGE = "Connection refused: no further information"; | ||
|
||
public static StatusError throwable(final Throwable t) { | ||
if (t instanceof StatusRuntimeException) { | ||
StatusRuntimeException exception = (StatusRuntimeException) t; | ||
if (exception.getStatus().getCode() == Status.UNAVAILABLE.getCode()) { | ||
final String causeMessage = findCauseMessage(t, CONNECTION_REFUSED_MESSAGE, 2); | ||
if (causeMessage != null) { | ||
final String message = exception.getStatus().getDescription() + ": " + causeMessage; | ||
return new SimpleStatusError(message, t); | ||
} | ||
} | ||
} | ||
return new DefaultStatusError(t); | ||
} | ||
|
||
private static String findCauseMessage(final Throwable t, final String message, final int maxDepth) { | ||
int depth = 0; | ||
Throwable cause = t.getCause(); | ||
while (cause != null && depth < maxDepth) { | ||
if (cause.getMessage().startsWith(message)) { | ||
return cause.getMessage(); | ||
} | ||
|
||
if (cause.getCause() == cause) { | ||
break; | ||
} | ||
cause = cause.getCause(); | ||
depth += 1; | ||
} | ||
// Not found | ||
return null; | ||
} | ||
|
||
private static class SimpleStatusError implements StatusError { | ||
private final String message; | ||
private final Throwable throwable; | ||
|
||
public SimpleStatusError(final String message, final Throwable throwable) { | ||
this.message = message; | ||
this.throwable = throwable; | ||
} | ||
|
||
@Override | ||
public boolean isSimpleError() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return this.message; | ||
} | ||
|
||
@Override | ||
public Throwable getThrowable() { | ||
return this.throwable; | ||
} | ||
} | ||
|
||
private static class DefaultStatusError implements StatusError { | ||
private final Throwable throwable; | ||
|
||
public DefaultStatusError(final Throwable throwable) { | ||
this.throwable = throwable; | ||
} | ||
|
||
@Override | ||
public boolean isSimpleError() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
if (this.throwable != null) { | ||
return this.throwable.getMessage(); | ||
} | ||
return ""; | ||
} | ||
|
||
@Override | ||
public Throwable getThrowable() { | ||
return this.throwable; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
grpc/src/test/java/com/navercorp/pinpoint/grpc/StatusErrorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2019 NAVER Corp. | ||
* | ||
* 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.navercorp.pinpoint.grpc; | ||
|
||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author jaehong.kim | ||
*/ | ||
public class StatusErrorsTest { | ||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Test | ||
public void throwable() { | ||
StatusError statusError = StatusErrors.throwable(new RuntimeException("test")); | ||
assertEquals("test", statusError.getMessage()); | ||
assertFalse(statusError.isSimpleError()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters