ginhttp

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ginhttp provides a custom HTTP client with tracing and logging capabilities for Gin web applications. It wraps the standard net/http client and integrates with Gin web framework for context propagation and trace ID handling.

The package implements the following main features:

  • Automatic trace ID propagation across service boundaries
  • Request/Response logging with structured attributes
  • Integration with slog for structured logging
  • Compatible with Gin web framework contexts

Basic usage:

client := ginhttp.NewClient(nil)
resp, err := client.OutgoingRequest(
	ginCtx,
	"GET",
	"https://api.example.com",
	nil,
	map[string]string{"Authorization": "Bearer token"},
)

Trace ID Propagation: The package automatically extracts trace IDs from incoming requests and propagates them to outgoing requests through the X-Trace-ID header. The trace ID is extracted in the following order:

  1. From Gin context stored value
  2. From parent context
  3. From request headers

Logging: All outgoing requests are automatically logged with the following attributes:

  • trace_id: The propagated trace ID
  • http.request.method: The HTTP method
  • http.route: The request path
  • server.address: The target host
  • http.response.latency: Request duration
  • http.response.status_code: Response status code
  • error: Error message (if request failed)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*http.Client
}

Client wraps http.Client with tracing and logging capabilities

func NewClient

func NewClient(baseClient *http.Client) *Client

NewClient creates a new HTTP client with tracing and logging middleware

func (*Client) OutgoingRequest

func (c *Client) OutgoingRequest(ctx *gin.Context, method, url string, body io.Reader, headers map[string]string) (*http.Response, error)

OutgoingRequest performs an outgoing HTTP request with "outgoing" attributes to be logged.

Directories

Path Synopsis
gin
statuses
Package ginhttp from http/gin/statuses provides a set of utility functions for handling HTTP responses in a Gin web application.
Package ginhttp from http/gin/statuses provides a set of utility functions for handling HTTP responses in a Gin web application.

Jump to

Keyboard shortcuts

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