Skip to content

Commit

Permalink
Merge pull request #15 from herp-inc/hedis-hook
Browse files Browse the repository at this point in the history
フック版 Hedis インスツルメンテーション
  • Loading branch information
kakkun61 authored Nov 26, 2024
2 parents 775e3dd + fb1c645 commit b119ae1
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 634 deletions.
7 changes: 7 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ packages:
, examples/aws-s3
, examples/grpc-echo
, examples/hdbc-mysql
, examples/hedis
, examples/herp-logger
, examples/hspec
, examples/http-server
Expand Down Expand Up @@ -71,6 +72,12 @@ source-repository-package
location: https://github.com/herp-inc/herp-logger
tag: 99ebe35339d973d6d47f1c307cb11da7ca42206e

source-repository-package
type: git
location: https://github.com/informatikr/hedis
tag: 6a5b1f2786f30c901b88ceed15ffe6b508f450e5
-- requires https://github.com/informatikr/hedis/pull/223 that not yet released

allow-newer:
http-api-data:base
, postgresql-simple:base
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ services:
ports:
- "3306:3306"

redis:
image: redis
ports:
- "6379:6379"

localstack:
image: localstack/localstack:s3-latest
ports:
Expand Down
30 changes: 30 additions & 0 deletions examples/hedis/client.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{-# LANGUAGE OverloadedStrings #-}

import Control.Exception (bracket)
import Control.Monad (void)
import Control.Monad.IO.Class (liftIO)
import Database.Redis (connect, defaultConnectInfo, get, runRedis, set)
import OpenTelemetry.Instrumentation.Hedis (appendHooksToConnectionInfo)
import OpenTelemetry.Trace (defaultSpanArguments, inSpan, initializeTracerProvider, makeTracer, shutdownTracerProvider, tracerOptions)
import System.IO (hFlush, stdout)


main :: IO ()
main =
bracket
initializeTracerProvider
shutdownTracerProvider
$ \tracerProvider -> do
let tracer = makeTracer tracerProvider "main" tracerOptions
inSpan tracer "main" defaultSpanArguments $ do
connInfo <- appendHooksToConnectionInfo tracerProvider defaultConnectInfo
connection <- connect connInfo
runRedis connection $ do
void $ set "hello" "hello"
void $ set "world" "world"
hello <- get "hello"
world <- get "world"
liftIO $ print (hello, world)
putStr "Press enter to exit after while..."
hFlush stdout
void $ getLine -- wait for transporting spans
48 changes: 48 additions & 0 deletions examples/hedis/hedis-example.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cabal-version: 3.4

name: hedis-example
version: 0.0.0
build-type: Simple

common common
ghc-options: -threaded
-with-rtsopts=-N
-Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-exported-signatures
-Wmissing-home-modules
-Wmissing-export-lists
-Wmonomorphism-restriction
-Wno-name-shadowing
-Wpartial-fields
-Wredundant-constraints
-Wunused-packages
if impl(ghc >= 9.0)
ghc-options: -Winvalid-haddock
if impl(ghc >= 9.2)
ghc-options: -Wmissing-kind-signatures
-Woperator-whitespace
-Wredundant-bang-patterns
default-language: Haskell2010

executable hedis-client
import: common
main-is: client.hs
hs-source-dirs: .
build-depends: base,
hedis,
hs-opentelemetry-sdk,
hs-opentelemetry-instrumentation-hedis

executable hedis-pubsub
import: common
main-is: pubsub.hs
hs-source-dirs: .
build-depends: base,
hedis,
hs-opentelemetry-sdk,
hs-opentelemetry-instrumentation-hedis
47 changes: 47 additions & 0 deletions examples/hedis/pubsub.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE OverloadedStrings #-}

import Control.Concurrent (forkIO)
import Control.Exception (bracket)
import Control.Monad (void)
import Control.Monad.IO.Class (liftIO)
import Database.Redis
import OpenTelemetry.Instrumentation.Hedis (appendHooksToConnectionInfo)
import qualified OpenTelemetry.Trace as Otel
import System.IO (hFlush, stdout)


main :: IO ()
main =
bracket
Otel.initializeTracerProvider
Otel.shutdownTracerProvider
$ \tracerProvider -> do
let tracer = Otel.makeTracer tracerProvider "hedis-pubsub" Otel.tracerOptions
connInfo <- appendHooksToConnectionInfo tracerProvider defaultConnectInfo
connection <- connect connInfo
void $ forkIO $ Otel.inSpan tracer "single-thread" Otel.defaultSpanArguments $ do
runRedis connection $
pubSub (subscribe ["hello"]) $ \message -> do
Otel.inSpan tracer "single-thread callback" Otel.defaultSpanArguments $ do
putStrLn $ "single-thread: received: " ++ show message
pure mempty
void $ forkIO $ Otel.inSpan tracer "multithread" Otel.defaultSpanArguments $ do
controller <- newPubSubController [("hello", helloMessageCallback tracer)] []
pubSubForever connection controller $ do
putStrLn "subscribed acknowledged"
liftIO $ do
putStrLn "Press enter to publish a message..."
hFlush stdout
void $ getLine -- wait for subscribing
Otel.inSpan tracer "publish" Otel.defaultSpanArguments $ do
runRedis connection $ do
void $ publish "hello" "world"
putStrLn "Press enter to exit after while..."
hFlush stdout
void $ getLine -- wait for transporting spans


helloMessageCallback :: Otel.Tracer -> MessageCallback
helloMessageCallback tracer message = do
Otel.inSpan tracer "multithread callback" Otel.defaultSpanArguments $ do
putStrLn $ "multithread: receive: " ++ show message
3 changes: 3 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ cradle:
- path: "examples/hdbc-mysql/main.hs"
component: "hdbc-mysql-example:exe:hdbc-mysql-example"

- path: "examples/hedis/client.hs"
component: "hedis-example:exe:hedis-client"

- path: "examples/herp-logger/main.hs"
component: "herp-logger-example:exe:herp-logger-example"

Expand Down
25 changes: 7 additions & 18 deletions instrumentation/hedis/hs-opentelemetry-instrumentation-hedis.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ name: hs-opentelemetry-instrumentation-hedis
version: 0.0.0.0
author: Kazuki Okamoto (岡本和樹)
maintainer: kazuki.okamoto@herp.co.jp
extra-source-files: functions.txt

common common
build-depends: base >= 4 && < 5
Expand All @@ -13,29 +12,19 @@ common common
ghc-options: -Wcompat
default-language: Haskell2010

custom-setup
setup-depends:
base,
Cabal,
directory,
filepath

library
import: common
hs-source-dirs: src, gen
hs-source-dirs: src
exposed-modules: OpenTelemetry.Instrumentation.Hedis
other-modules: OpenTelemetry.Instrumentation.Hedis.Internal.Action
OpenTelemetry.Instrumentation.Hedis.Internal.Wrapper
autogen-modules: OpenTelemetry.Instrumentation.Hedis.Internal.Action
other-modules:
Paths_hs_opentelemetry_instrumentation_hedis
autogen-modules:
Paths_hs_opentelemetry_instrumentation_hedis
build-depends: hs-opentelemetry-api,
hs-opentelemetry-semantic-conventions,
hedis >= 0.14,
bytestring,
iproute,
mtl,
safe-exceptions,
text,
unliftio-core,
unordered-containers
text
ghc-options: -Wcompat
-Wno-name-shadowing
if impl(ghc >= 6.4)
Expand Down
Loading

0 comments on commit b119ae1

Please sign in to comment.