packages feed

hs-opentelemetry-instrumentation-hspec (empty) → 0.0.1.0

raw patch · 7 files changed

+165/−0 lines, 7 filesdep +basedep +hs-opentelemetry-apidep +hs-opentelemetry-instrumentation-hspecsetup-changed

Dependencies added: base, hs-opentelemetry-api, hs-opentelemetry-instrumentation-hspec, hspec, hspec-core, mtl, resourcet, text, unliftio, vault

Files

+ ChangeLog.md view
@@ -0,0 +1,7 @@+# Changelog for hs-opentelemetry-instrumentation-hspec++## 0.0.1.0++- Initial release++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Ian Duncan (c) 2021++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Ian Duncan nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,10 @@+# hs-opentelemetry-instrumentation-hspec++Instrumentation for Hspec for OpenTelemetry. This creates a span for each test+case inside a test suite.++## Usage++See the example in [examples/hspec](../../examples/hspec) for an instrumented+test suite.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hs-opentelemetry-instrumentation-hspec.cabal view
@@ -0,0 +1,64 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.5.+--+-- see: https://github.com/sol/hpack++name:           hs-opentelemetry-instrumentation-hspec+version:        0.0.1.0+description:    Please see the README on GitHub at <https://github.com/iand675/hs-opentelemetry/tree/main/instrumentation/hspec#readme>+homepage:       https://github.com/iand675/hs-opentelemetry#readme+bug-reports:    https://github.com/iand675/hs-opentelemetry/issues+author:         Ian Duncan+maintainer:     ian@iankduncan.com+copyright:      2021 Ian Duncan+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md+    ChangeLog.md++source-repository head+  type: git+  location: https://github.com/iand675/hs-opentelemetry++library+  exposed-modules:+      OpenTelemetry.Instrumentation.Hspec+  other-modules:+      Paths_hs_opentelemetry_instrumentation_hspec+  hs-source-dirs:+      src+  build-depends:+      base >=4.7 && <5+    , hs-opentelemetry-api ==0.0.3.*+    , hspec >=2.9.4+    , hspec-core >=2.9.4+    , mtl+    , resourcet+    , text+    , unliftio+    , vault+  default-language: Haskell2010++test-suite hs-opentelemetry-hspec-test+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Paths_hs_opentelemetry_instrumentation_hspec+  hs-source-dirs:+      test+  ghc-options: -threaded -rtsopts -with-rtsopts=-N+  build-depends:+      base >=4.7 && <5+    , hs-opentelemetry-api ==0.0.3.*+    , hs-opentelemetry-instrumentation-hspec+    , hspec >=2.9.4+    , hspec-core >=2.9.4+    , mtl+    , resourcet+    , text+    , unliftio+    , vault+  default-language: Haskell2010
+ src/OpenTelemetry/Instrumentation/Hspec.hs view
@@ -0,0 +1,50 @@+-- | Instrumentation for Hspec test suites+{-# LANGUAGE OverloadedStrings #-}++module OpenTelemetry.Instrumentation.Hspec+  ( wrapSpec,+    wrapExampleInSpan,+  )+where++import Control.Monad.IO.Class+import Control.Monad.Reader+import Data.Text (Text)+import qualified Data.Text as T+import OpenTelemetry.Attributes (Attributes)+import OpenTelemetry.Context+import OpenTelemetry.Context.ThreadLocal (adjustContext, attachContext, getContext)+import OpenTelemetry.Trace.Core+import Test.Hspec.Core.Spec (ActionWith, Item (..), Spec, SpecWith, mapSpecItem_)++-- | Creates a wrapper function that you can pass a spec into.+--+--   This function will wrap each @it@ test case with a span with the name of+--   the test case.+--+--   The context in which this is called determines the parent span of all of+--   the spec items.+wrapSpec :: MonadIO m => m (SpecWith a -> SpecWith a)+wrapSpec = do+  tp <- getGlobalTracerProvider+  let tracer = makeTracer tp "hs-opentelemetry-instrumentation-hspec" tracerOptions+  context <- getContext++  -- FIXME: this kind of just dumps everything flat into one span per `it`. We+  -- could possibly do better, e.g. finding the `describe`s and making them into+  -- spans but I am not sure how that would be achieved.+  pure $ \spec -> mapSpecItem_ (wrapExampleInSpan tracer context) spec++-- | Wraps one example in a span parented by the specified context, and ensures+--   the thread running the spec item will have a context available.+wrapExampleInSpan :: Tracer -> Context -> Item a -> Item a+wrapExampleInSpan tp traceContext item@Item {itemExample = ex, itemRequirement = req} =+  item+    { itemExample = \params aroundAction pcb -> do+        let aroundAction' a = do+              -- we need to reattach the context, since we are on a forked thread+              void $ attachContext traceContext+              inSpan tp (T.pack req) defaultSpanArguments (aroundAction a)++        ex params aroundAction' pcb+    }
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"