fxhttpclient

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 12 Imported by: 0

README

Fx Http Client Module

ci go report codecov Deps PkgGoDev

Fx module for httpclient.

Installation

go get github.com/ankorstore/yokai/fxhttpclient

Features

This module provides the possibility to provide to your Fx application a http.Client with:

  • configurable transport
  • automatic and configurable request / response logging
  • configurable request / response tracing
  • configurable request metrics

Documentation

Dependencies

This module is intended to be used alongside:

Loading

To load the module in your Fx application:

package main

import (
	"net/http"

	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxhttpclient"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/fxmetrics"
	"github.com/ankorstore/yokai/fxtrace"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxconfig.FxConfigModule, // load the module dependencies
		fxlog.FxLogModule,
		fxmetrics.FxMetricsModule,
		fxtrace.FxTraceModule,
		fxhttpclient.FxHttpClientModule, // load the module
		fx.Invoke(func(httpClient *http.Client) { // invoke the client
			resp, err := httpClient.Get("https://example.com")
		}),
	).Run()
}
Configuration

Configuration reference:

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: true
modules:
  log:
    level: info
    output: stdout
  trace:
    processor:
      type: stdout
  http:
    client:
      timeout: 30                            # in seconds, 30 by default
      transport:
        max_idle_connections: 100            # 100 by default
        max_connections_per_host: 100        # 100 by default
        max_idle_connections_per_host: 100   # 100 by default
      log:
        request:
          enabled: true                      # to log request details, disabled by default
          body: true                         # to add request body to request details, disabled by default
          level: info                        # log level for request logging
        response:
          enabled: true                      # to log response details, disabled by default
          body: true                         # to add response body to request details, disabled by default
          level: info                        # log level for response logging
          level_from_response: true          # to use response code for response logging
      trace:
        enabled: true                        # to trace http calls, disabled by default
      metrics:
        collect:
          enabled: true                      # to collect http client metrics
          namespace: foo                     # http client metrics namespace (empty by default)
          subsystem: bar                     # http client metrics subsystem (empty by default)
        buckets: 0.1, 1, 10                  # to override default request duration buckets
        normalize:
          request_path: true                 # to normalize http request path, disabled by default
          request_path_masks:                # request path normalization masks (key: mask to apply, value: regex to match), empty by default
            /foo/{id}/bar?page={page}: /foo/(.+)/bar\?page=(.+)
          response_status: true              # to normalize http response status code (2xx, 3xx, ...), disabled by default

If modules.http.client.log.response.level_from_response=true, the response code will be used to determinate the log level:

  • code < 400: log level configured in modules.http.client.log.response.level
  • 400 <= code < 500: log level warn
  • code >= 500: log level error

If modules.http.client.metrics.normalize.request_path=true, the modules.http.client.metrics.normalize.request_path_masks map will be used to try to apply masks on the metrics path label for better cardinality.

For the given example, if the request path is /foo/1/bar?page=2, the metric path label will be masked with /foo/{id}/bar?page={page}.

Notes:

  • the http client logging will be based on the fxlog module configuration
  • the http client tracing will be based on the fxtrace module configuration
Override

By default, the http.Client is created by the DefaultHttpClientFactory.

If needed, you can provide your own factory and override the module:

package main

import (
	"net/http"

	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxhttpclient"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/fxmetrics"
	"github.com/ankorstore/yokai/fxtrace"
	"github.com/ankorstore/yokai/httpclient"
	"go.uber.org/fx"
)

type CustomHttpClientFactory struct{}

func NewCustomHttpClientFactory() httpclient.HttpClientFactory {
	return &CustomHttpClientFactory{}
}

func (f *CustomHttpClientFactory) Create(options ...httpclient.HttpClientOption) (*http.Client, error) {
	return http.DefaultClient, nil
}

func main() {
	fx.New(
		fxconfig.FxConfigModule, // load the module dependencies
		fxlog.FxLogModule,
		fxmetrics.FxMetricsModule,
		fxtrace.FxTraceModule,
		fxhttpclient.FxHttpClientModule,         // load the module
		fx.Decorate(NewCustomHttpClientFactory), // override the module with a custom factory
		fx.Invoke(func(httpClient *http.Client) { // invoke the custom client
			// ...
		}),
	).Run()
}

Documentation

Index

Constants

View Source
const (
	ModuleName                       = "httpclient"
	DefaultTimeout                   = 30
	DefaultMaxIdleConnections        = 100
	DefaultMaxConnectionsPerHost     = 100
	DefaultMaxIdleConnectionsPerHost = 100
)

Variables

FxHttpClientModule is the Fx http client module.

Functions

func Flip added in v1.2.0

func Flip(m map[string]string) map[string]string

Flip reverses a map in {value: key} format.

func NewFxHttpClient

func NewFxHttpClient(p FxHttpClientParam) (*http.Client, error)

NewFxHttpClient returns a new http.Client.

func NewFxHttpClientTransport added in v1.1.0

func NewFxHttpClientTransport(p FxHttpClientTransportParam) http.RoundTripper

NewFxHttpClientTransport returns a new http.RoundTripper.

func Sanitize added in v1.1.0

func Sanitize(str string) string

Sanitize transforms a given string to not contain spaces or dashes, and to be in lower case.

Types

type FxHttpClientParam

type FxHttpClientParam struct {
	fx.In
	Factory      httpclient.HttpClientFactory
	RoundTripper http.RoundTripper
	Config       *config.Config
}

FxHttpClientParam allows injection of the required dependencies in NewFxHttpClient.

type FxHttpClientTransportParam added in v1.1.0

type FxHttpClientTransportParam struct {
	fx.In
	TracerProvider  trace.TracerProvider
	Config          *config.Config
	Logger          *log.Logger
	MetricsRegistry *prometheus.Registry
}

FxHttpClientTransportParam allows injection of the required dependencies in NewFxHttpClientTransport.

Jump to

Keyboard shortcuts

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