datacounter

package
v0.0.0-...-663eb47 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseWriters

func CloseWriters(writers []io.Closer) error

CloseWriters closes all provided io.Closer instances and returns the first encountered error, if any.

Types

type HTTPReaderCounter

type HTTPReaderCounter struct {
	Reader io.ReadCloser
	// contains filtered or unexported fields
}

HTTPReaderCounter monitors the amount of data read from an io.Reader and accumulates it safely across multiple goroutines. It wraps an io.ReadCloser, providing additional functionality to track and update metrics related to read operations. The type also offers a method to retrieve the total byte count in a thread-safe manner, ensuring data integrity when accessed concurrently.

func NewHTTPReaderCounter

func NewHTTPReaderCounter(ctx context.Context, component string, r io.ReadCloser) *HTTPReaderCounter

NewHTTPReaderCounter wraps an io.ReadCloser to monitor the amount of data being read, updating metrics for each read operation and providing a concurrent-safe total byte count. It returns a new instance of HTTPReaderCounter.

func (*HTTPReaderCounter) Close

func (counter *HTTPReaderCounter) Close() error

Close terminates the underlying io.Reader of the *HTTPReaderCounter, ends the tracking of metrics, and returns any error that occurred during the closing operation. If the underlying io.Reader does not implement the io.Closer interface, Close performs no operation and returns nil.

func (*HTTPReaderCounter) Count

func (counter *HTTPReaderCounter) Count() uint64

Count returns the total number of bytes read from the underlying io.Reader in a thread-safe manner.

func (*HTTPReaderCounter) Read

func (counter *HTTPReaderCounter) Read(buf []byte) (int, error)

Read fetches data from the underlying io.Reader into a provided buffer and updates the count of bytes read. It ensures that only non-negative byte counts are recorded in the common byte counter and updates associated metrics for HTTPBytesRead. The method returns the number of bytes read and any error that may have occurred during the reading operation.

type HTTPWriterCounter

type HTTPWriterCounter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

HTTPWriterCounter wraps an http.ResponseWriter to measure and record the volume of data written in HTTP responses, the response status codes, and the duration of response writing. It also enables hijacking the connection for protocol upgrades and modifying response headers. This type is crucial for monitoring the performance and efficiency of HTTP response handling in web applications.

func NewHTTPWriterCounter

func NewHTTPWriterCounter(ctx context.Context, component string, rw http.ResponseWriter) *HTTPWriterCounter

NewHTTPWriterCounter initializes a new HTTPWriterCounter that wraps an existing http.ResponseWriter. It starts tracking the amount of data sent to the client, the response write latency, and sets the initial time for these measurements. It returns a pointer to the newly created HTTPWriterCounter.

func (*HTTPWriterCounter) Count

func (counter *HTTPWriterCounter) Count() uint64

Count retrieves the total number of bytes that have been written to the HTTP response, ensuring access is safe across multiple goroutines.

func (*HTTPWriterCounter) Header

func (counter *HTTPWriterCounter) Header() http.Header

Header returns the headers of the HTTP response that will be sent. This allows for modification of the header map before writing the response body.

func (*HTTPWriterCounter) Hijack

func (counter *HTTPWriterCounter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack allows a client to take over the underlying TCP connection from the HTTP server. This is useful for switching protocols or performing operations at a lower level than HTTP. It returns the net.Conn, which represents the raw network connection, along with bufio.ReadWriter objects that facilitate buffered I/O on that connection. If hijacking isn't supported by the underlying ResponseWriter or another error occurs, an error will be returned.

func (*HTTPWriterCounter) Started

func (counter *HTTPWriterCounter) Started() time.Time

Started returns the timestamp marking when the tracking of HTTP response write times and data counts commenced for the HTTPWriterCounter.

func (*HTTPWriterCounter) StatusCode

func (counter *HTTPWriterCounter) StatusCode() int

StatusCode retrieves the HTTP status code that has been recorded for the response. If no status code has been explicitly set via WriteHeader, this method may return the zero value for an integer.

func (*HTTPWriterCounter) Unwrap

func (counter *HTTPWriterCounter) Unwrap() http.ResponseWriter

Unwrap provides access to the encapsulated http.ResponseWriter used by the HTTPWriterCounter. It allows for interactions with the original response writer without any of the additional functionality provided by the counter, such as byte counting and latency tracking.

func (*HTTPWriterCounter) Write

func (counter *HTTPWriterCounter) Write(buf []byte) (int, error)

Write forwards the provided data to the wrapped http.ResponseWriter while tracking the number of bytes sent and updating related metrics. It returns the number of bytes written and any error that occurred during the write operation.

func (*HTTPWriterCounter) WriteHeader

func (counter *HTTPWriterCounter) WriteHeader(statusCode int)

WriteHeader sets the HTTP status code for the current response, records it internally for future reference, and delegates the actual header writing to the wrapped http.ResponseWriter.

type ObjectStorageReaderCounter

type ObjectStorageReaderCounter struct {
	Reader *storage.Reader
	// contains filtered or unexported fields
}

ObjectStorageReaderCounter tracks the amount of data read from an object storage service, ensuring thread-safe tallying of bytes transferred during read operations. It encapsulates a reader for monitoring and recording the volume of data accessed, with optional support for random-access reading capabilities. The counter integrates with a metrics system to log read latencies and byte counts, providing valuable insights for monitoring object storage interactions.

func NewObjectStorageReaderCounter

func NewObjectStorageReaderCounter(ctx context.Context, component string, r *storage.Reader, client *storage.Client, objHandler *storage.ObjectHandle, seeker bool) *ObjectStorageReaderCounter

NewObjectStorageReaderCounter creates a new instance of ObjectStorageReaderCounter that wraps an existing storage.Reader to monitor and count the bytes read from an object in cloud storage. It also enables optional random-access read capabilities and integrates with a metrics system for monitoring read operations. The function accepts a storage.Reader, a storage.Client, a storage.ObjectHandle, and a boolean indicating whether random-access reads should be enabled. It returns the newly created ObjectStorageReaderCounter.

func (*ObjectStorageReaderCounter) Close

func (counter *ObjectStorageReaderCounter) Close() error

Close terminates the underlying io.Reader and finalizes the tracking of read latency metrics. If the io.Reader implements the io.Closer interface and an error occurs during its closure, that error is returned. If the underlying io.Reader does not implement the io.Closer interface, Close performs no operation and returns nil.

func (*ObjectStorageReaderCounter) Count

func (counter *ObjectStorageReaderCounter) Count() uint64

Count returns the cumulative number of bytes read from the object storage by this instance. It ensures thread-safety, allowing for accurate byte count retrieval at any point during the object's read operations.

func (*ObjectStorageReaderCounter) Read

func (counter *ObjectStorageReaderCounter) Read(buf []byte) (int, error)

Read retrieves data from the underlying io.Reader into the provided buffer and updates the read byte count. It returns the number of bytes read along with any error encountered, ensuring that only non-negative byte counts are considered for updating the metrics. The function is concurrency-safe and integrates with metric tracking for object storage reads.

func (*ObjectStorageReaderCounter) ReadAt

func (counter *ObjectStorageReaderCounter) ReadAt(buf []byte, off int64) (int, error)

ReadAt reads data from the storage object starting at a specified offset into the provided buffer. It updates the byte count and integrates with metric tracking, returning the number of bytes read and any error encountered. This method is concurrency-safe and supports concurrent access by multiple goroutines. If the instance is configured for random-access reads, it performs the operation using an internal reader; otherwise, it creates a new range reader for each call.

func (*ObjectStorageReaderCounter) Size

func (counter *ObjectStorageReaderCounter) Size() int64

Size retrieves the size of the underlying object in the storage, expressed in bytes. This information is accessed through the attributes of the underlying storage.Reader.

type ObjectStorageWriterCounter

type ObjectStorageWriterCounter struct {
	Writer *storage.Writer
	// contains filtered or unexported fields
}

ObjectStorageWriterCounter aggregates and reports the amount of data sent to an object storage service, while also monitoring the performance of these write operations. It encapsulates the functionality to handle concurrent writes effectively, ensuring that the byte count is precise and consistent. Additionally, it offers mechanisms to conclude the writing session and finalize the collection of related metrics. Use this type when you need a reliable way to track both the volume of data written to object storage and the associated latency of these operations.

func NewObjectStorageWriterCounter

func NewObjectStorageWriterCounter(ctx context.Context, component string, w *storage.Writer, client *storage.Client) *ObjectStorageWriterCounter

NewObjectStorageWriterCounter returns a new instance of ObjectStorageWriterCounter, which is responsible for monitoring and aggregating the volume of data written to an object storage service, as well as tracking the latency of write operations. It utilizes a provided storage.Writer and storage.Client to interface with the object storage system.

func (*ObjectStorageWriterCounter) Close

func (counter *ObjectStorageWriterCounter) Close() error

Close finalizes the operation of the *ObjectStorageWriterCounter, completing the metric tracking and closing the underlying writer if applicable. It returns any error that occurs during the closure of the underlying writer.

func (*ObjectStorageWriterCounter) Count

func (counter *ObjectStorageWriterCounter) Count() uint64

Count retrieves the total number of bytes that have been successfully written to object storage using the *ObjectStorageWriterCounter. It ensures thread-safe access to the byte count and is suitable for use in concurrent operations. The method returns the cumulative byte count as a [uint64].

func (*ObjectStorageWriterCounter) Write

func (counter *ObjectStorageWriterCounter) Write(buf []byte) (int, error)

Write writes a slice of bytes to the object storage, updates the count of successfully written bytes, and records the corresponding metrics. It returns the number of bytes written and any error that may have occurred during the write operation. Only positive byte counts are considered valid and included in the total count.

type ReaderCounter

type ReaderCounter struct {
	Reader io.ReadCloser
	// contains filtered or unexported fields
}

ReaderCounter wraps an io.Reader to monitor the number of bytes read during its lifetime. It provides functionality to read data, query the accumulated byte count, and close the underlying reader when applicable. The count is maintained in a thread-safe manner, ensuring accurate reporting even in concurrent environments.

func NewReaderCounter

func NewReaderCounter(r io.ReadCloser) *ReaderCounter

NewReaderCounter returns a new ReaderCounter that wraps an io.ReadCloser, allowing for the monitoring and recording of the total number of bytes read.

func (*ReaderCounter) Close

func (counter *ReaderCounter) Close() error

Close terminates the reading process from the underlying io.Reader of the ReaderCounter. It closes the reader if it implements the io.Closer interface. If the reader does not implement io.Closer, Close has no effect and returns nil. Any error encountered during the close operation is returned to the caller.

func (*ReaderCounter) Count

func (counter *ReaderCounter) Count() uint64

Count retrieves the current cumulative number of bytes that have been read using the ReaderCounter.

func (*ReaderCounter) Read

func (counter *ReaderCounter) Read(buf []byte) (int, error)

Read retrieves data from the wrapped io.Reader, increments the byte count of the ReaderCounter by the number of bytes read, and returns the amount read along with any error encountered during the operation. If the read byte count is zero or positive, it is added to the total; negative counts are ignored.

type UpstreamPCMReaderCounter

type UpstreamPCMReaderCounter struct {
	Reader io.ReadCloser
	// contains filtered or unexported fields
}

UpstreamPCMReaderCounter wraps an io.Reader to monitor and record the volume of data read and the duration of read operations from an upstream source. It provides safe concurrent access for multiple goroutines to track cumulative bytes read, supports closing of the underlying reader if it is also an io.Closer, and ensures precise latency measurements are captured. This type is essential for observing and analyzing reading performance and overall data throughput in real-time data processing scenarios.

func NewUpstreamPCMReaderCounter

func NewUpstreamPCMReaderCounter(ctx context.Context, component string, r io.ReadCloser) *UpstreamPCMReaderCounter

NewUpstreamPCMReaderCounter creates and returns a new UpstreamPCMReaderCounter that wraps an io.ReadCloser for the purpose of monitoring and recording the amount of data read as well as the time taken to read from an upstream source. It facilitates both the accurate collection of metrics and the provision of concurrent access without race conditions.

func (*UpstreamPCMReaderCounter) Close

func (counter *UpstreamPCMReaderCounter) Close() error

Close finalizes the operations of an UpstreamPCMReaderCounter. It terminates any ongoing read tracking and closes the underlying data source if it implements io.Closer. If the data source does not implement io.Closer, Close will have no effect on it. Close returns an error if one occurs during the closing of the data source.

func (*UpstreamPCMReaderCounter) Count

func (counter *UpstreamPCMReaderCounter) Count() uint64

Count retrieves the total number of bytes that have been read from the upstream data source. It is safe for concurrent use and returns the byte count as a [uint64].

func (*UpstreamPCMReaderCounter) Read

func (counter *UpstreamPCMReaderCounter) Read(buf []byte) (int, error)

Read populates the provided buffer with data from the upstream source, concurrently updates the count of total bytes read, and records the read latency metrics. It returns the number of bytes read into the buffer and any error encountered during the reading process. Negative byte counts, if returned by the upstream source, are not counted towards the total.

type UpstreamPCMWriterCounter

type UpstreamPCMWriterCounter struct {
	Writer io.ReadCloser
	// contains filtered or unexported fields
}

UpstreamPCMWriterCounter wraps an io.Writer to monitor and record the volume of data read and the duration of read operations from an upstream source. It provides safe concurrent access for multiple goroutines to track cumulative bytes read, supports closing of the underlying reader if it is also an io.Closer, and ensures precise latency measurements are captured. This type is essential for observing and analyzing reading performance and overall data throughput in real-time data processing scenarios.

func NewUpstreamPCMWriterCounter

func NewUpstreamPCMWriterCounter(ctx context.Context, component string, r io.ReadCloser) *UpstreamPCMWriterCounter

NewUpstreamPCMWriterCounter creates and returns a new UpstreamPCMWriterCounter that wraps an io.ReadCloser for the purpose of monitoring and recording the amount of data read as well as the time taken to read from an upstream source. It facilitates both the accurate collection of metrics and the provision of concurrent access without race conditions.

func (*UpstreamPCMWriterCounter) Close

func (counter *UpstreamPCMWriterCounter) Close() error

Close finalizes the operations of an UpstreamPCMWriterCounter. It terminates any ongoing read tracking and closes the underlying data source if it implements io.Closer. If the data source does not implement io.Closer, Close will have no effect on it. Close returns an error if one occurs during the closing of the data source.

func (*UpstreamPCMWriterCounter) Count

func (counter *UpstreamPCMWriterCounter) Count() uint64

Count retrieves the total number of bytes that have been read from the upstream data source. It is safe for concurrent use and returns the byte count as a [uint64].

func (*UpstreamPCMWriterCounter) Read

func (counter *UpstreamPCMWriterCounter) Read(buf []byte) (int, error)

Read populates the provided buffer with data from the upstream source, concurrently updates the count of total bytes read, and records the read latency metrics. It returns the number of bytes read into the buffer and any error encountered during the reading process. Negative byte counts, if returned by the upstream source, are not counted towards the total.

type WriterCounter

type WriterCounter struct {
	Writer io.WriteCloser
	// contains filtered or unexported fields
}

WriterCounter tracks the cumulative number of bytes successfully written to an underlying io.Writer. It provides a thread-safe way to monitor write operations and retrieve the total byte count at any moment. WriterCounter also supports closing the underlying writer if it implements the io.Closer interface, allowing for proper resource management.

func NewWriterCounter

func NewWriterCounter(w io.WriteCloser) *WriterCounter

NewWriterCounter returns a new instance of WriterCounter that wraps an io.WriteCloser for tracking the cumulative number of bytes written. The count is accessible at any time and is thread-safe. If the underlying writer supports closing, the WriterCounter will also be closable.

func (*WriterCounter) Close

func (counter *WriterCounter) Close() error

Close finalizes the WriterCounter by closing the underlying io.Writer. If the io.Writer does not support the io.Closer interface, Close returns an error (if any) from closing the underlying writer.

func (*WriterCounter) Count

func (counter *WriterCounter) Count() uint64

Count retrieves the total number of bytes that have been successfully written to the wrapped io.Writer. It ensures thread safety and returns the byte count as a uint64 value.

func (*WriterCounter) Write

func (counter *WriterCounter) Write(buf []byte) (int, error)

Write sends a slice of bytes to the wrapped io.Writer, increments the count of total bytes written, and returns the number of bytes written along with any error that occurred during the write. It ensures thread-safe updating of the byte count and excludes negative values from the count.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL