Skip to content

Commit 2f24115

Browse files
committed
Merge branch 'compress-events'
2 parents e7001c9 + 7901cd4 commit 2f24115

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

core-telemetry/core-telemetry.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cabal-version: 1.18
55
-- see: https://github.com/sol/hpack
66

77
name: core-telemetry
8-
version: 0.2.3.3
8+
version: 0.2.3.4
99
synopsis: Advanced telemetry
1010
description: This is part of a library to help build command-line programs, both tools and
1111
longer-running daemons.
@@ -67,4 +67,5 @@ library
6767
, template-haskell >=2.14 && <3
6868
, text
6969
, unix
70+
, zlib
7071
default-language: Haskell2010

core-telemetry/lib/Core/Telemetry/Honeycomb.hs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ module Core.Telemetry.Honeycomb (
3535
honeycombExporter,
3636
) where
3737

38+
import Codec.Compression.GZip qualified as GZip (compress)
3839
import Control.Exception.Safe qualified as Safe (catch, finally, throw)
39-
import Core.Data.Clock (Time, unTime, getCurrentTimeNanoseconds)
40+
import Core.Data.Clock (Time, getCurrentTimeNanoseconds, unTime)
4041
import Core.Data.Structures (Map, fromMap, insertKeyValue, intoMap, lookupKeyValue)
4142
import Core.Encoding.Json
4243
import Core.Program.Arguments
@@ -49,6 +50,8 @@ import Core.Text.Rope
4950
import Core.Text.Utilities
5051
import Data.ByteString (ByteString)
5152
import Data.ByteString qualified as B (ByteString)
53+
import Data.ByteString.Builder (Builder)
54+
import Data.ByteString.Builder qualified as Builder (lazyByteString)
5255
import Data.ByteString.Char8 qualified as C (append, null, putStrLn)
5356
import Data.ByteString.Lazy qualified as L (ByteString)
5457
import Data.Fixed
@@ -57,7 +60,8 @@ import Data.List qualified as List
5760
import Network.Http.Client
5861
import System.Environment (lookupEnv)
5962
import System.Exit (ExitCode (..))
60-
import System.IO.Streams (InputStream)
63+
import System.IO.Streams (InputStream, OutputStream)
64+
import System.IO.Streams qualified as Streams (write)
6165
import System.Posix.Process qualified as Posix (exitImmediately)
6266

6367
{- |
@@ -233,6 +237,13 @@ cleanupConnection r = do
233237
writeIORef r Nothing
234238
)
235239

240+
compressBody :: Bytes -> OutputStream Builder -> IO ()
241+
compressBody bytes o = do
242+
let x = fromBytes bytes
243+
let x' = GZip.compress x
244+
let b = Builder.lazyByteString x'
245+
Streams.write (Just b) o
246+
236247
postEventToHoneycombAPI :: IORef (Maybe Connection) -> ApiKey -> Dataset -> JsonValue -> IO ()
237248
postEventToHoneycombAPI r apikey dataset json = attempt False
238249
where
@@ -242,7 +253,7 @@ postEventToHoneycombAPI r apikey dataset json = attempt False
242253
c <- acquireConnection r
243254

244255
-- actually transmit telemetry to Honeycomb
245-
sendRequest c q (simpleBody (fromBytes (encodeToUTF8 json)))
256+
sendRequest c q (compressBody (encodeToUTF8 json))
246257
receiveResponse c handler
247258
)
248259
( \(e :: SomeException) -> do
@@ -251,14 +262,17 @@ postEventToHoneycombAPI r apikey dataset json = attempt False
251262
cleanupConnection r
252263
case retrying of
253264
False -> do
254-
putStrLn "Reattempting"
265+
putStrLn "internal: Reconnecting to Honeycomb"
255266
attempt True
256-
True -> Safe.throw e
267+
True -> do
268+
putStrLn "internal: Failed to re-establish connection to Honeycomb"
269+
Safe.throw e
257270
)
258271

259272
q = buildRequest1 $ do
260273
http POST (fromRope ("/1/batch/" <> dataset))
261274
setContentType "application/json"
275+
setHeader "Content-Encoding" "gzip"
262276
setHeader "X-Honeycomb-Team" (fromRope (apikey))
263277

264278
{-
@@ -285,7 +299,7 @@ postEventToHoneycombAPI r apikey dataset json = attempt False
285299
pure ()
286300
_ -> do
287301
-- some other status!
288-
putStrLn "Unexpected status returned;"
302+
putStrLn "internal: Unexpected status returned;"
289303
C.putStrLn body
290304
_ -> putStrLn "internal: wtf?"
291305
_ -> do

core-telemetry/package.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: core-telemetry
2-
version: 0.2.3.3
2+
version: 0.2.3.4
33
synopsis: Advanced telemetry
44
description: |
55
This is part of a library to help build command-line programs, both tools and
@@ -46,6 +46,7 @@ library:
4646
- scientific
4747
- stm
4848
- unix
49+
- zlib
4950
source-dirs:
5051
- lib
5152
exposed-modules:

0 commit comments

Comments
 (0)