-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Simple LogRecordProcessor and refactored shutdown and forceFlush #136
base: add-log-record-exporters
Are you sure you want to change the base?
Changes from all commits
e404a2f
77a9b64
a3fbc19
fa40778
b9df7ce
9b6b234
6a12572
7c35830
58f8451
9250ec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,20 @@ module OpenTelemetry.Internal.Common.Types ( | |
AnyValue (..), | ||
ToValue (..), | ||
ShutdownResult (..), | ||
shutdownErrorHandler, | ||
takeWorstShutdownResult, | ||
flushResultToShutdownResult, | ||
FlushResult (..), | ||
flushErrorHandler, | ||
takeWorstFlushResult, | ||
exportResultToFlushResult, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Helper functions to convert from |
||
ExportResult (..), | ||
) where | ||
|
||
import Control.Exception (SomeException) | ||
import Control.Exception (Exception, SomeException) | ||
import Data.ByteString (ByteString) | ||
import Data.Data (Data) | ||
import Data.Foldable (fold, toList) | ||
import qualified Data.HashMap.Strict as H | ||
import Data.Hashable (Hashable) | ||
import Data.Int (Int64) | ||
|
@@ -159,22 +166,91 @@ instance ToValue AnyValue where | |
toValue = id | ||
|
||
|
||
data ShutdownResult = ShutdownSuccess | ShutdownFailure | ShutdownTimeout | ||
data ShutdownResult | ||
= ShutdownSuccess | ||
| ShutdownTimeout | ||
| ShutdownError [SomeException] | ||
|
||
|
||
{- | (<>) concatenates the error lists if both arguments are @ShutdownError@, otherwise it returns the worst of the two in this order | ||
- @ShutdownError@ | ||
- @ShutdownTimeout@ | ||
- @ShutdownSuccess@ | ||
-} | ||
instance Semigroup ShutdownResult where | ||
ShutdownError l <> ShutdownError r = ShutdownError (l <> r) | ||
ShutdownError es <> _ = ShutdownError es | ||
_ <> ShutdownError es = ShutdownError es | ||
ShutdownTimeout <> _ = ShutdownTimeout | ||
_ <> ShutdownTimeout = ShutdownTimeout | ||
ShutdownSuccess <> ShutdownSuccess = ShutdownSuccess | ||
|
||
|
||
-- | mempty is ShutdownSuccess | ||
instance Monoid ShutdownResult where | ||
mempty = ShutdownSuccess | ||
|
||
|
||
shutdownErrorHandler :: SomeException -> IO ShutdownResult | ||
shutdownErrorHandler = pure . ShutdownError . pure | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used with |
||
|
||
|
||
flushResultToShutdownResult :: FlushResult -> ShutdownResult | ||
flushResultToShutdownResult FlushSuccess = ShutdownSuccess | ||
flushResultToShutdownResult FlushTimeout = ShutdownTimeout | ||
flushResultToShutdownResult (FlushError es) = ShutdownError es | ||
|
||
|
||
takeWorstShutdownResult :: (Foldable t) => t ShutdownResult -> ShutdownResult | ||
takeWorstShutdownResult = fold | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Developers may be able to use |
||
|
||
|
||
-- | The outcome of a call to @OpenTelemetry.Trace.forceFlush@ or @OpenTelemetry.Logs.forceFlush@ | ||
data FlushResult | ||
= -- | One or more spans or @LogRecord@s did not export from all associated exporters | ||
= -- | Flushing spans or @LogRecord@s to all associated exporters succeeded. | ||
FlushSuccess | ||
| -- | One or more spans or @LogRecord@s did not export from all associated exporters | ||
-- within the alotted timeframe. | ||
FlushTimeout | ||
| -- | Flushing spans or @LogRecord@s to all associated exporters succeeded. | ||
FlushSuccess | ||
| -- | One or more exporters failed to successfully export one or more | ||
-- unexported spans or @LogRecord@s. | ||
FlushError | ||
FlushError [SomeException] | ||
deriving (Show) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend tweaking | FlushError [SomeException] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
|
||
|
||
{- | (<>) concatenates the error lists if both arguments are @FlushError@, otherwise it returns the worst of the two in this order | ||
- @FlushError@ | ||
- @FlushTimeout@ | ||
- @FlushSuccess@ | ||
-} | ||
instance Semigroup FlushResult where | ||
FlushError l <> FlushError r = FlushError (l <> r) | ||
FlushError es <> _ = FlushError es | ||
_ <> FlushError es = FlushError es | ||
FlushTimeout <> _ = FlushTimeout | ||
_ <> FlushTimeout = FlushTimeout | ||
FlushSuccess <> FlushSuccess = FlushSuccess | ||
|
||
|
||
-- | mempty is FlushSuccess | ||
instance Monoid FlushResult where | ||
mempty = FlushSuccess | ||
|
||
|
||
flushErrorHandler :: SomeException -> IO FlushResult | ||
flushErrorHandler = pure . FlushError . pure | ||
|
||
|
||
takeWorstFlushResult :: (Foldable t) => t FlushResult -> FlushResult | ||
takeWorstFlushResult = fold | ||
|
||
|
||
exportResultToFlushResult :: ExportResult -> FlushResult | ||
exportResultToFlushResult Success = FlushSuccess | ||
exportResultToFlushResult (Failure mErr) = FlushError $ toList mErr | ||
|
||
|
||
data ExportResult | ||
= Success | ||
| Failure (Maybe SomeException) | ||
deriving (Show) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ module OpenTelemetry.Internal.Logs.Types ( | |
) where | ||
|
||
import Control.Concurrent (MVar, newMVar, withMVar) | ||
import Control.Concurrent.Async | ||
import Data.Function (on) | ||
import Data.HashMap.Strict (HashMap) | ||
import qualified Data.HashMap.Strict as H | ||
|
@@ -33,7 +32,7 @@ import Data.Text (Text) | |
import Data.Vector (Vector) | ||
import OpenTelemetry.Common (Timestamp, TraceFlags) | ||
import OpenTelemetry.Context.Types (Context) | ||
import OpenTelemetry.Internal.Common.Types (ExportResult, InstrumentationLibrary, ShutdownResult) | ||
import OpenTelemetry.Internal.Common.Types | ||
import OpenTelemetry.Internal.Trace.Id (SpanId, TraceId) | ||
import OpenTelemetry.LogAttributes | ||
import OpenTelemetry.Resource (MaterializedResources) | ||
|
@@ -43,9 +42,9 @@ import OpenTelemetry.Resource (MaterializedResources) | |
data LogRecordExporterArguments = LogRecordExporterArguments | ||
{ logRecordExporterArgumentsExport :: Vector ReadableLogRecord -> IO ExportResult | ||
-- ^ See @logRecordExporterExport@ for documentation | ||
, logRecordExporterArgumentsForceFlush :: IO () | ||
, logRecordExporterArgumentsForceFlush :: IO FlushResult | ||
-- ^ See @logRecordExporterArgumentsForceFlush@ for documentation | ||
, logRecordExporterArgumentsShutdown :: IO () | ||
, logRecordExporterArgumentsShutdown :: IO ShutdownResult | ||
-- ^ See @logRecordExporterArgumentsShutdown@ for documentation | ||
} | ||
|
||
|
@@ -97,7 +96,7 @@ the process after an invocation, but before the exporter exports the ReadlableLo | |
ForceFlush SHOULD complete or abort within some timeout. ForceFlush can be implemented as a blocking API or an asynchronous API which | ||
notifies the caller via a callback or an event. OpenTelemetry SDK authors MAY decide if they want to make the flush timeout configurable. | ||
-} | ||
logRecordExporterForceFlush :: LogRecordExporter -> IO () | ||
logRecordExporterForceFlush :: LogRecordExporter -> IO FlushResult | ||
logRecordExporterForceFlush = flip withMVar logRecordExporterArgumentsForceFlush . unExporter | ||
|
||
|
||
|
@@ -109,7 +108,7 @@ allowed and SHOULD return a Failure result. | |
Shutdown SHOULD NOT block indefinitely (e.g. if it attempts to flush the data and the destination is unavailable). | ||
OpenTelemetry SDK authors MAY decide if they want to make the shutdown timeout configurable. | ||
-} | ||
logRecordExporterShutdown :: LogRecordExporter -> IO () | ||
logRecordExporterShutdown :: LogRecordExporter -> IO ShutdownResult | ||
logRecordExporterShutdown = flip withMVar logRecordExporterArgumentsShutdown . unExporter | ||
|
||
|
||
|
@@ -143,7 +142,7 @@ data LogRecordProcessor = LogRecordProcessor | |
-- ^ Called when a LogRecord is emitted. This method is called synchronously on the thread that emitted the LogRecord, therefore it SHOULD NOT block or throw exceptions. | ||
-- | ||
-- A LogRecordProcessor may freely modify logRecord for the duration of the OnEmit call. If logRecord is needed after OnEmit returns (i.e. for asynchronous processing) only reads are permitted. | ||
, logRecordProcessorShutdown :: IO (Async ShutdownResult) | ||
, logRecordProcessorShutdown :: IO ShutdownResult | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes more sense to me for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least for tracing, the docs say this:
I can appreciate the argument in the other direction, but would prefer to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another reason I made the change is that having Also, the way I rewrote Let me know your thoughts. |
||
-- ^ Shuts down the processor. Called when SDK is shut down. This is an opportunity for processor to do any cleanup required. | ||
-- | ||
-- Shutdown SHOULD be called only once for each LogRecordProcessor instance. After the call to Shutdown, subsequent calls to OnEmit are not allowed. SDKs SHOULD ignore these calls gracefully, if possible. | ||
|
@@ -154,7 +153,7 @@ data LogRecordProcessor = LogRecordProcessor | |
-- | ||
-- Shutdown SHOULD complete or abort within some timeout. Shutdown can be implemented as a blocking API or an asynchronous API which notifies the caller via a callback or an event. | ||
-- OpenTelemetry SDK authors can decide if they want to make the shutdown timeout configurable. | ||
, logRecordProcessorForceFlush :: IO () | ||
, logRecordProcessorForceFlush :: IO FlushResult | ||
-- ^ This is a hint to ensure that any tasks associated with LogRecords for which the LogRecordProcessor had already received events prior to the call to ForceFlush SHOULD be completed | ||
-- as soon as possible, preferably before returning from this method. | ||
-- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a driveby request on my part, but it would be nice to reexport
FlushResult
so that callers of forceFlushTracerProvider can actually use the type.