http

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2021 License: Apache-2.0 Imports: 7 Imported by: 24 modules, 34 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package http contains several client/server http plugin which can be used for integration with net/http.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(tracer *go2sky.Tracer, options ...ClientOption) (*http.Client, error)

NewClient returns an HTTP Client with tracer

func NewServerMiddleware

func NewServerMiddleware(tracer *go2sky.Tracer, options ...ServerOption) (func(http.Handler) http.Handler, error)

NewServerMiddleware returns a http.Handler middleware with tracing.

Example
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
)

func main() {
	// Use gRPC reporter for production
	r, err := reporter.NewLogReporter()
	if err != nil {
		log.Fatalf("new reporter error %v \n", err)
	}
	defer r.Close()

	tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r))
	if err != nil {
		log.Fatalf("create tracer error %v \n", err)
	}

	sm, err := NewServerMiddleware(tracer)
	if err != nil {
		log.Fatalf("create server middleware error %v \n", err)
	}

	client, err := NewClient(tracer)
	if err != nil {
		log.Fatalf("create client error %v \n", err)
	}

	// create test server
	ts := httptest.NewServer(sm(endFunc()))
	defer ts.Close()

	// call end service
	request, err := http.NewRequest("POST", fmt.Sprintf("%s/end", ts.URL), nil)
	if err != nil {
		log.Fatalf("unable to create http request: %+v\n", err)
	}
	res, err := client.Do(request)
	if err != nil {
		log.Fatalf("unable to do http request: %+v\n", err)
	}
	_ = res.Body.Close()
	time.Sleep(time.Second)

}

func endFunc() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		log.Printf("end func called with method: %s\n", r.Method)
		time.Sleep(50 * time.Millisecond)
	}
}

Types

type ClientConfig

type ClientConfig struct {
	// contains filtered or unexported fields
}

type ClientOption

type ClientOption func(*ClientConfig)

ClientOption allows optional configuration of Client.

func WithClient

func WithClient(client *http.Client) ClientOption

WithClient set customer http client.

func WithClientOperationName

func WithClientOperationName(name string) ClientOption

WithOperationName override default operation name.

func WithClientTag

func WithClientTag(key string, value string) ClientOption

WithClientTag adds extra tag to client spans.

type ServerOption

type ServerOption func(*handler)

ServerOption allows Middleware to be optionally configured.

func WithServerOperationName

func WithServerOperationName(name string) ServerOption

WithOperationName override default operation name.

func WithServerTag

func WithServerTag(key string, value string) ServerOption

Tag adds extra tag to server spans.

Directories

Path Synopsis
test
client command
server command

Jump to

Keyboard shortcuts

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