Giter Site home page Giter Site logo

stremovskyy / recorder Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 14 KB

A Go library for recording and retrieving requests, responses, errors, and metrics. This library provides both synchronous and asynchronous methods for recording data to Redis or file storage.

License: MIT License

Go 100.00%
go golang http library package redis

recorder's Introduction

Recorder

A Go library for recording and retrieving requests, responses, errors, and metrics. This library provides both synchronous and asynchronous methods for recording data to Redis or file storage.

Features

  • Record and retrieve requests, responses, errors, and metrics.
  • Support for both Redis and file-based storage backends.
  • Asynchronous methods for non-blocking operations.
  • Easy to extend with custom storage backends.

Installation

To install the recorder library, use go get:

go get github.com/stremovskyy/recorder

Usage

Interface

The recorder package defines the Recorder interface, which includes methods for recording and retrieving data:

// Recorder is the public interface for the recorder.
type Recorder interface {
	RecordRequest(ctx context.Context, primaryID *string, requestID string, request []byte, tags map[string]string) error
	RecordResponse(ctx context.Context, primaryID *string, requestID string, response []byte, tags map[string]string) error
	RecordError(ctx context.Context, id *string, requestID string, err error, tags map[string]string) error
	RecordMetrics(ctx context.Context, primaryID *string, requestID string, metrics map[string]string, tags map[string]string) error
	GetRequest(ctx context.Context, requestID string) ([]byte, error)
	GetResponse(ctx context.Context, requestID string) ([]byte, error)
	FindByTag(ctx context.Context, tag string) ([]string, error)
	Async() AsyncRecorder
}

// AsyncRecorder defines the asynchronous methods for the recorder.
type AsyncRecorder interface {
	RecordRequest(ctx context.Context, primaryID *string, requestID string, request []byte, tags map[string]string) <-chan error
	RecordResponse(ctx context.Context, primaryID *string, requestID string, response []byte, tags map[string]string) <-chan error
	RecordError(ctx context.Context, id *string, requestID string, err error, tags map[string]string) <-chan error
	RecordMetrics(ctx context.Context, primaryID *string, requestID string, metrics map[string]string, tags map[string]string) <-chan error
	GetRequest(ctx context.Context, requestID string) <-chan Result
	GetResponse(ctx context.Context, requestID string) <-chan Result
	FindByTag(ctx context.Context, tag string) <-chan FindByTagResult
}

Redis Implementation

Usage

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/stremovskyy/recorder/redis_recorder"
)

func main() {
	options := &redis_recorder.Options{
		Addr:          "localhost:6379",
		Password:      "",
		DB:            0,
		Prefix:        "myapp",
		DefaultTTL:    24 * time.Hour,
		CompressionLvl: 5,
		Debug:         true,
	}

	rec := redis_recorder.NewRedisRecorder(options)

	// Record a request
	err := rec.RecordRequest(context.Background(), nil, "req1", []byte("request data"), nil)
	if err != nil {
		log.Fatalf("Failed to record request: %v", err)
	}

	// Retrieve a request
	data, err := rec.GetRequest(context.Background(), "req1")
	if err != nil {
		log.Fatalf("Failed to get request: %v", err)
	}
	fmt.Println("Request data:", string(data))
}

File-based Implementation

Usage

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/stremovskyy/recorder/file_recorder"
)

func main() {
	rec := file_recorder.NewFileRecorder("/path/to/store/files")

	// Record a request
	err := rec.RecordRequest(context.Background(), nil, "req1", []byte("request data"), nil)
	if err != nil {
		log.Fatalf("Failed to record request: %v", err)
	}

	// Retrieve a request
	data, err := rec.GetRequest(context.Background(), "req1")
	if err != nil {
		log.Fatalf("Failed to get request: %v", err)
	}
	fmt.Println("Request data:", string(data))
}

Asynchronous Methods

Both implementations support asynchronous methods via the Async() method:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/stremovskyy/recorder/file_recorder"
)

func main() {
	rec := file_recorder.NewFileRecorder("/path/to/store/files").Async()

	// Record a request asynchronously
	resultChan := rec.RecordRequest(context.Background(), nil, "req1", []byte("request data"), nil)
	if err := <-resultChan; err != nil {
		log.Fatalf("Failed to record request: %v", err)
	}

	// Retrieve a request asynchronously
	dataChan := rec.GetRequest(context.Background(), "req1")
	result := <-dataChan
	if result.Err != nil {
		log.Fatalf("Failed to get request: %v", result.Err)
	}
	fmt.Println("Request data:", string(result.Data))
}

Extending the Library

You can extend the recorder library by implementing the Recorder interface for other storage backends. Create a new package for your implementation and follow the patterns shown in the Redis and file-based implementations.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch with a descriptive name.
  3. Make your changes.
  4. Commit your changes with clear commit messages.
  5. Push to your fork and submit a pull request.

Please ensure your code adheres to the standard Go formatting and includes tests for any new functionality.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any questions or suggestions, please open an issue on GitHub or contact the repository owner.

Acknowledgments

Special thanks to all contributors who have helped improve this project.

recorder's People

Contributors

stremovskyy avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.