Skip to content
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
9 changes: 9 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ documentation: False

package *
documentation: False

-- Use latest hs-opentelemetry from GitHub for GHC 9.12 support
-- (Hackage version is outdated)
source-repository-package
type: git
location: https://github.com/iand675/hs-opentelemetry.git
tag: 841a8e191fba3f2c76f6e6fa10d26b644856b7ee
subdir: api
instrumentation/postgresql-simple
4 changes: 4 additions & 0 deletions effectful-postgresql/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Haskell Package Versioning Policy](https://pvp.hask

## [Unreleased]

### Added

- OpenTelemetry instrumentation support via `enable-otel` flag (transparent, no code changes required)

## [0.1.0.1] - 04.08.2025

### Changed
Expand Down
11 changes: 11 additions & 0 deletions effectful-postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ usingConnectionPool = do
pool <- P.newPool poolCfg
runEff . EP.runWithconnectionPool pool $ insertAndListCarefully
```

## OpenTelemetry Instrumentation

For applications using OpenTelemetry for distributed tracing, enable the `enable-otel` flag:

```cabal
dependencies:
- effectful-postgresql +enable-otel
```

All database operations will automatically create OpenTelemetry spans with database attributes. No code changes required - the same `Effectful.PostgreSQL` module transparently uses instrumented functions when the flag is enabled.
13 changes: 13 additions & 0 deletions effectful-postgresql/effectful-postgresql.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ flag enable-pool
default: True
manual: False

flag enable-otel
description: Enable OpenTelemetry instrumentation support using
hs-opentelemetry-instrumentation-postgresql-simple.
default: False
manual: True

common warnings
ghc-options: -Wall -Wno-unused-do-bind -Wunused-packages

if flag(enable-pool)
cpp-options: -DPOOL

if flag(enable-otel)
cpp-options: -DOTEL

common deps
build-depends:
, base >= 4 && < 5
Expand All @@ -53,6 +62,10 @@ common deps
build-depends:
, unliftio-pool >= 0.4.1 && < 0.5

if flag(enable-otel)
build-depends:
, hs-opentelemetry-instrumentation-postgresql-simple >= 0.2 && < 0.4

common extensions
default-extensions:
DataKinds
Expand Down
10 changes: 10 additions & 0 deletions effectful-postgresql/src/Effectful/PostgreSQL.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE PackageImports #-}

module Effectful.PostgreSQL
( -- * Effect
Expand Down Expand Up @@ -51,7 +52,11 @@ module Effectful.PostgreSQL
where

import Data.Int (Int64)
#if OTEL
import qualified "hs-opentelemetry-instrumentation-postgresql-simple" OpenTelemetry.Instrumentation.PostgresqlSimple as PSQL
#else
import qualified Database.PostgreSQL.Simple as PSQL
#endif
import qualified Database.PostgreSQL.Simple.FromRow as PSQL
import Effectful
import Effectful.PostgreSQL.Connection as Conn
Expand Down Expand Up @@ -212,7 +217,12 @@ forEach ::
Eff es ()
forEach q row forR =
unliftWithConn $ \conn unlift ->
#if OTEL
-- Workaround for bug in hs-opentelemetry-instrumentation-postgresql-simple
PSQL.forEachWith PSQL.fromRow conn q row (unlift . forR)
#else
PSQL.forEach conn q row (unlift . forR)
#endif

-- | Lifted 'PSQL.forEach_'.
forEach_ ::
Expand Down