Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http gzip compression #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Alternatively you can write events directly to a S3 bucket:
"output": {
"type": "Http"
"endpoint": "https://my.collector.endpoint.com"
"gzip": true
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object Config {
case class File(path: URI) extends Output
case class PubSub(subscription: String) extends Output
case class Kafka(brokers: String, topic: String, producerConf: Map[String, String] = Map.empty) extends Output
case class Http(endpoint: org.http4s.Uri) extends Output
case class Http(endpoint: org.http4s.Uri, gzip: Option[Boolean]) extends Output
}

val configOpt = Opts.option[Path]("config", "Path to the configuration HOCON").orNone
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2021-2022 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.eventgen

import cats.effect._
import fs2.compression.Compression
import fs2.compression.DeflateParams
import fs2.compression.ZLibParams
import org.http4s.Header
import org.http4s.Request
import org.http4s.headers.`Content-Encoding`
import org.http4s.headers.`Content-Length`

object GZipMiddleware {
val DefaultBufferSize = 32 * 1024

def apply[F[_]: Sync](
retainUserEncoding: Boolean,
bufferSize: Int = DefaultBufferSize,
level: DeflateParams.Level = DeflateParams.Level.DEFAULT
)(request: Request[F]): Request[F] = {
val updateContentTypeEncoding =
(retainUserEncoding, request.headers.get[`Content-Encoding`]) match {
case (true, Some(`Content-Encoding`(cc))) =>
Header.Raw(
`Content-Encoding`.headerInstance.name,
s"${cc.coding}, gzip"
)

case _ =>
Header.Raw(
`Content-Encoding`.headerInstance.name,
"gzip"
)
}
val compressPipe =
Compression
.forSync[F]
.gzip(
fileName = None,
modificationTime = None,
comment = None,
DeflateParams(
bufferSize = bufferSize,
level = level,
header = ZLibParams.Header.GZIP
)
)
request
.removeHeader[`Content-Length`]
.putHeaders(updateContentTypeEncoding)
.withBodyStream(request.body.through(compressPipe))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object Http {

def sink[F[_]: Async](properties: Config.Output.Http): Pipe[F, Main.GenOutput, Unit] = {

def buildRequesst(
def buildRequest(
generatedRequest: HttpRequest
): Request[F] = {

Expand Down Expand Up @@ -97,7 +97,7 @@ object Http {
case TrackerMethod.Head(_) => throw new NotImplementedError ( "HEAD requests not implemented" )
}

return req
if (properties.gzip.getOrElse(false)) GZipMiddleware(false)(req) else req
}

val httpClient = EmberClientBuilder.default[F].build
Expand All @@ -106,7 +106,7 @@ object Http {
Stream
.resource(httpClient)
.flatMap(client =>
st.map(_._3).map(buildRequesst).evalMap(req => client.status(req)).void)
st.map(_._3).map(buildRequest).evalMap(req => client.status(req)).void)
}
}
}
Loading