-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from broadinstitute/bug
[IA-1857] add error reporting
- Loading branch information
Showing
12 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Changelog | ||
|
||
This file documents changes to the `workbench-error-reporting` library, including notes on how to upgrade to new versions. | ||
|
||
## 0.1 | ||
|
||
### Added | ||
- Add `ErrorReporting` | ||
|
||
SBT dependency: `"org.broadinstitute.dsde.workbench" %% "workbench-error-reporting" % "0.1-TRAVIS-REPLACE-ME"` |
50 changes: 50 additions & 0 deletions
50
...ting/src/main/scala/org/broadinstitute/dsde/workbench/errorReporting/ErrorReporting.scala
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,50 @@ | ||
package org.broadinstitute.dsde.workbench.errorReporting | ||
|
||
import java.nio.file.Path | ||
|
||
import cats.effect.{Resource, Sync} | ||
import com.google.api.gax.core.FixedCredentialsProvider | ||
import com.google.auth.oauth2.{GoogleCredentials, ServiceAccountCredentials} | ||
import com.google.cloud.errorreporting.v1beta1.{ReportErrorsServiceClient, ReportErrorsServiceSettings} | ||
import com.google.devtools.clouderrorreporting.v1beta1.{ProjectName, SourceLocation} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
trait ErrorReporting[F[_]] { | ||
def reportError(msg: String, sourceLocation: SourceLocation): F[Unit] | ||
|
||
/** | ||
* @param t This throwable can not be NoStackTrace | ||
* @return | ||
*/ | ||
def reportError(t: Throwable): F[Unit] | ||
} | ||
|
||
object ErrorReporting { | ||
def fromPath[F[_]](pathToCredential: Path, appName: String, projectName: ProjectName)( | ||
implicit F: Sync[F] | ||
): Resource[F, ErrorReporting[F]] = | ||
for { | ||
crendtialFile <- org.broadinstitute.dsde.workbench.util2.readPath(pathToCredential) | ||
credential = ServiceAccountCredentials | ||
.fromStream(crendtialFile) | ||
.createScoped( | ||
Set("https://www.googleapis.com/auth/cloud-platform").asJava | ||
) | ||
client <- fromCredential(credential, appName, projectName) | ||
} yield client | ||
|
||
def fromCredential[F[_]](credentials: GoogleCredentials, appName: String, projectName: ProjectName)( | ||
implicit F: Sync[F] | ||
): Resource[F, ErrorReporting[F]] = { | ||
val settings = ReportErrorsServiceSettings | ||
.newBuilder() | ||
.setCredentialsProvider(FixedCredentialsProvider.create(credentials)) | ||
.build() | ||
Resource | ||
.make[F, ReportErrorsServiceClient](F.delay(ReportErrorsServiceClient.create(settings)))( | ||
c => F.delay(c.close()) | ||
) | ||
.map(c => new ErrorReportingInterpreter[F](appName, projectName, c)) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...in/scala/org/broadinstitute/dsde/workbench/errorReporting/ErrorReportingInterpreter.scala
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,46 @@ | ||
package org.broadinstitute.dsde.workbench.errorReporting | ||
|
||
import cats.effect.{Resource, Sync} | ||
import cats.implicits._ | ||
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient | ||
import com.google.devtools.clouderrorreporting.v1beta1._ | ||
import java.io.PrintWriter | ||
import java.io.StringWriter | ||
|
||
class ErrorReportingInterpreter[F[_]](appName: String, projectName: ProjectName, client: ReportErrorsServiceClient)( | ||
implicit F: Sync[F] | ||
) extends ErrorReporting[F] { | ||
val serviceContext = ServiceContext.newBuilder().setService(appName) | ||
|
||
override def reportError(msg: String, sourceLocation: SourceLocation): F[Unit] = { | ||
val errorEvent = ReportedErrorEvent | ||
.newBuilder() | ||
.setMessage(msg) | ||
.setServiceContext(serviceContext) | ||
.setContext(ErrorContext.newBuilder().setReportLocation(sourceLocation)) | ||
.build() | ||
|
||
F.delay(client.reportErrorEvent(projectName, errorEvent)) | ||
} | ||
|
||
override def reportError(t: Throwable): F[Unit] = { | ||
val stackTraceWriter = Resource.make { | ||
val sw = new StringWriter | ||
F.delay(StackTraceWriter(sw, new PrintWriter(sw))) | ||
}(sw => F.delay(sw.printWriter.close()) >> F.delay(sw.stringWriter.close())) | ||
|
||
stackTraceWriter.use { w => | ||
for { | ||
_ <- F.delay(t.printStackTrace(w.printWriter)) | ||
errorEvent = ReportedErrorEvent | ||
.newBuilder() | ||
.setMessage(w.stringWriter.toString) | ||
.setServiceContext(serviceContext) | ||
.build() | ||
_ <- F.delay(client.reportErrorEvent(projectName, errorEvent)) | ||
} yield () | ||
} | ||
} | ||
} | ||
|
||
final private case class StackTraceWriter(stringWriter: StringWriter, printWriter: PrintWriter) |
16 changes: 16 additions & 0 deletions
16
...orting/src/main/scala/org/broadinstitute/dsde/workbench/errorReporting/ReportWorthy.scala
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,16 @@ | ||
package org.broadinstitute.dsde.workbench.errorReporting | ||
|
||
/** | ||
* Defines a ReportWorthy type class which has `def isReportWorthy(a: A): Boolean` function | ||
*/ | ||
trait ReportWorthy[A] { | ||
def isReportWorthy(a: A): Boolean | ||
} | ||
|
||
final case class ReportWorthyOps[A](a: A)(implicit ev: ReportWorthy[A]) { | ||
def isReportWorthy: Boolean = ev.isReportWorthy(a) | ||
} | ||
|
||
object ReportWorthySyntax { | ||
implicit def reportWorthySyntax[A: ReportWorthy](a: A): ReportWorthyOps[A] = ReportWorthyOps(a) | ||
} |
39 changes: 39 additions & 0 deletions
39
...est/scala/org/broadinstitute/dsde/workbench/errorReporting/ErrorReportingManualTest.scala
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,39 @@ | ||
package org.broadinstitute.dsde.workbench.errorReporting | ||
|
||
import java.nio.file.Paths | ||
|
||
import cats.effect.IO | ||
import com.google.devtools.clouderrorreporting.v1beta1.{ProjectName, SourceLocation} | ||
|
||
import scala.concurrent.ExecutionContext.global | ||
import scala.util.control.NoStackTrace | ||
|
||
object ErrorReportingManualTest { | ||
implicit val cs = IO.contextShift(global) | ||
|
||
private def test(reporting: ErrorReporting[IO]): IO[Unit] = | ||
for { | ||
_ <- reporting.reportError(new Exception("eeee2")) | ||
_ <- reporting.reportError( | ||
"error2", | ||
SourceLocation | ||
.newBuilder() | ||
.setFunctionName("qi-function") | ||
.setFilePath(this.getClass.getName) | ||
// .setLineNumber(10) | ||
.build() | ||
) | ||
} yield () | ||
|
||
def run(): Unit = { | ||
val res = ErrorReporting | ||
.fromPath[IO](Paths.get("/Users/qi/.google/qi-billing-90828dd5e7b8.json"), | ||
"qi-test-app", | ||
ProjectName.of("qi-billing")) | ||
.use(c => test(c)) | ||
|
||
res.unsafeRunSync() | ||
} | ||
} | ||
|
||
final case class CustomException(msg: String) extends NoStackTrace |
10 changes: 10 additions & 0 deletions
10
.../src/test/scala/org/broadinstitute/dsde/workbench/errorReporting/FakeErrorReporting.scala
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,10 @@ | ||
package org.broadinstitute.dsde.workbench.errorReporting | ||
|
||
import cats.effect._ | ||
import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation | ||
|
||
object FakeErrorReporting extends ErrorReporting[IO] { | ||
override def reportError(msg: String, sourceLocation: SourceLocation): IO[Unit] = IO.unit | ||
|
||
override def reportError(t: Throwable): IO[Unit] = IO.unit | ||
} |
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