-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
334 additions
and
262 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
59 changes: 59 additions & 0 deletions
59
modules/core/src/main/scala/com.snowplowanalytics.snowplow.snowflake/Monitoring.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,59 @@ | ||
package com.snowplowanalytics.snowplow.snowflake | ||
|
||
import cats.effect.Resource | ||
import cats.effect.kernel.Sync | ||
import cats.implicits._ | ||
import com.snowplowanalytics.snowplow.runtime.AppInfo | ||
import io.sentry.{Sentry, SentryOptions} | ||
|
||
trait Monitoring[F[_]] { | ||
def alert(message: Monitoring.Alert): F[Unit] | ||
def reportFatal(exception: Throwable): F[Unit] | ||
} | ||
|
||
object Monitoring { | ||
|
||
sealed trait Alert | ||
|
||
object Alert { | ||
final case class FailedToCreateEventsTable(cause: Throwable) extends Alert | ||
// TODO add more! | ||
|
||
} | ||
|
||
def create[F[_]: Sync](appInfo: AppInfo, config: Config.Monitoring): Resource[F, Monitoring[F]] = | ||
initSentry(appInfo, config.sentry).map { _ => | ||
new Monitoring[F] { | ||
override def alert(message: Alert): F[Unit] = | ||
Sync[F].unit // TODO | ||
|
||
override def reportFatal(exception: Throwable): F[Unit] = | ||
config.sentry.fold(Sync[F].unit) { _ => | ||
Sync[F].delay(Sentry.captureException(exception)).void | ||
} | ||
|
||
} | ||
} | ||
|
||
private def initSentry[F[_]: Sync](appInfo: AppInfo, config: Option[Config.Sentry]): Resource[F, Unit] = | ||
config match { | ||
case Some(sentryConfig) => | ||
Resource.make { | ||
Sync[F].delay(Sentry.init(createSentryOptions(appInfo, sentryConfig))) | ||
} { _ => | ||
Sync[F].delay(Sentry.close()) | ||
} | ||
case None => | ||
Resource.unit[F] | ||
} | ||
|
||
private def createSentryOptions(appInfo: AppInfo, sentryConfig: Config.Sentry): SentryOptions = { | ||
val options = new SentryOptions | ||
options.setDsn(sentryConfig.dsn) | ||
options.setRelease(appInfo.version) | ||
sentryConfig.tags.foreach { case (k, v) => | ||
options.setTag(k, v) | ||
} | ||
options | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...e/src/main/scala/com.snowplowanalytics.snowplow.snowflake/processing/JdbcTransactor.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,49 @@ | ||
/* | ||
* Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Snowplow Community License Version 1.0, | ||
* and you may not use this file except in compliance with the Snowplow Community License Version 1.0. | ||
* You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 | ||
*/ | ||
package com.snowplowanalytics.snowplow.snowflake | ||
|
||
import cats.effect.{Async, Sync} | ||
import cats.implicits._ | ||
import doobie.Transactor | ||
import net.snowflake.ingest.utils.{Utils => SnowflakeSdkUtils} | ||
|
||
import java.security.PrivateKey | ||
import java.util.Properties | ||
|
||
object JdbcTransactor { | ||
|
||
private val driver: String = "net.snowflake.client.jdbc.SnowflakeDriver" | ||
|
||
def make[F[_]: Async](config: Config.Snowflake): F[Transactor[F]] = | ||
for { | ||
privateKey <- parsePrivateKey[F](config) | ||
props = jdbcProperties(config, privateKey) | ||
} yield Transactor.fromDriverManager[F](driver, config.url.getJdbcUrl, props, None) | ||
|
||
private def parsePrivateKey[F[_]: Sync](config: Config.Snowflake): F[PrivateKey] = | ||
Sync[F].delay { // Wrap in Sync because these can raise exceptions | ||
config.privateKeyPassphrase match { | ||
case Some(passphrase) => | ||
SnowflakeSdkUtils.parseEncryptedPrivateKey(config.privateKey, passphrase) | ||
case None => | ||
SnowflakeSdkUtils.parsePrivateKey(config.privateKey) | ||
} | ||
} | ||
|
||
private def jdbcProperties(config: Config.Snowflake, privateKey: PrivateKey): Properties = { | ||
val props = new Properties() | ||
props.setProperty("user", config.user) | ||
props.put("privateKey", privateKey) | ||
props.setProperty("timezone", "UTC") | ||
config.role.foreach(props.setProperty("role", _)) | ||
props.put("loginTimeout", config.jdbcLoginTimeout.toSeconds.toInt) | ||
props.put("networkTimeout", config.jdbcNetworkTimeout.toMillis.toInt) | ||
props.put("queryTimeout", config.jdbcQueryTimeout.toSeconds.toInt) | ||
props | ||
} | ||
} |
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
Oops, something went wrong.