Skip to content

Commit

Permalink
Rework compilation for Windows w/o CPP extension
Browse files Browse the repository at this point in the history
Extracts platform-dependent code to OpenTelemetry.Platform module
  • Loading branch information
develop7 committed Jun 20, 2024
1 parent 2baca5b commit 224e9a7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 18 deletions.
20 changes: 18 additions & 2 deletions sdk/hs-opentelemetry-sdk.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ library
, text
, transformers
, unagi-chan
, unix
, unix-compat >=0.7.1
, unordered-containers
, vector
, vector-builder
default-language: Haskell2010
if !os(windows)
build-depends:
unix
if os(windows)
other-modules:
OpenTelemetry.Platform
hs-source-dirs:
src/platform/windows
else
other-modules:
OpenTelemetry.Platform
hs-source-dirs:
src/platform/unix

test-suite hs-opentelemetry-sdk-test
type: exitcode-stdio-1.0
Expand Down Expand Up @@ -122,8 +135,11 @@ test-suite hs-opentelemetry-sdk-test
, text
, transformers
, unagi-chan
, unix
, unix-compat >=0.7.1
, unordered-containers
, vector
, vector-builder
default-language: Haskell2010
if !os(windows)
build-depends:
unix
12 changes: 11 additions & 1 deletion sdk/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ dependencies:
- vector-builder
- http-types
- network-bsd
- unix
- unix-compat >= 0.7.1 # for getProcessID
- transformers
- random >= 1.2.0

when:
condition: '!os(windows)'
dependencies: unix # for getEffectiveUserID

library:
ghc-options: -Wall
source-dirs: src
when:
- condition: os(windows)
then:
source-dirs: src/platform/windows
else:
source-dirs: src/platform/unix
reexported-modules:
- OpenTelemetry.Attributes
- OpenTelemetry.Baggage
Expand Down
17 changes: 2 additions & 15 deletions sdk/src/OpenTelemetry/Resource/Process/Detector.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
module OpenTelemetry.Resource.Process.Detector where

import Control.Exception (throwIO, try)
import qualified Data.Text as T
import Data.Version
import OpenTelemetry.Platform (tryGetUser)
import OpenTelemetry.Resource.Process
import System.Environment (
getArgs,
getExecutablePath,
getProgName,
)
import System.IO.Error
import System.Info
import System.Posix.Process (getProcessID)
import System.Posix.User (getEffectiveUserName)
import System.PosixCompat.Process (getProcessID)


{- | Create a 'Process' 'Resource' based off of the current process' knowledge
Expand All @@ -32,17 +30,6 @@ detectProcess = do
<*> tryGetUser


tryGetUser :: IO (Maybe T.Text)
tryGetUser = do
eResult <- try getEffectiveUserName
case eResult of
Left err ->
if isDoesNotExistError err
then pure Nothing
else throwIO err
Right ok -> pure $ Just $ T.pack ok


{- | A 'ProcessRuntime' 'Resource' populated with the current process' knoweldge
of itself.
Expand Down
17 changes: 17 additions & 0 deletions sdk/src/platform/unix/OpenTelemetry/Platform.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module OpenTelemetry.Platform where

import Control.Exception (throwIO, try)
import qualified Data.Text as T
import System.IO.Error (isDoesNotExistError)
import System.Posix.User (getEffectiveUserName)


tryGetUser :: IO (Maybe T.Text)
tryGetUser = do
eResult <- try getEffectiveUserName
case eResult of
Left err ->
if isDoesNotExistError err
then pure Nothing
else throwIO err
Right ok -> pure $ Just $ T.pack ok
7 changes: 7 additions & 0 deletions sdk/src/platform/windows/OpenTelemetry/Platform.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OpenTelemetry.Platform where

import qualified Data.Text as T


tryGetUser :: IO (Maybe T.Text)
tryGetUser = pure Nothing
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ packages:
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
extra-deps:
- unix-compat-0.7.1@sha256:bd5bb4e04b2ed707f3e3466470a452354310389506cf0a7a73bf10e4d533f6d1,3619
- thread-utils-context-0.3.0.4
- thread-utils-finalizers-0.1.1.0
- proto-lens-0.7.1.1
Expand Down
7 changes: 7 additions & 0 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# https://docs.haskellstack.org/en/stable/lock_files

packages:
- completed:
hackage: unix-compat-0.7.1@sha256:bd5bb4e04b2ed707f3e3466470a452354310389506cf0a7a73bf10e4d533f6d1,3619
pantry-tree:
sha256: c65e0937d1972e9a2824235082b6d8b70d4c3c60ce720ff11f5ab4e66fb6b85d
size: 1329
original:
hackage: unix-compat-0.7.1@sha256:bd5bb4e04b2ed707f3e3466470a452354310389506cf0a7a73bf10e4d533f6d1,3619
- completed:
hackage: thread-utils-context-0.3.0.4@sha256:e763da1c6cab3b6d378fb670ca74aa9bf03c9b61b6fcf7628c56363fb0e3e71e,1671
pantry-tree:
Expand Down

0 comments on commit 224e9a7

Please sign in to comment.