http

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

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/gorilla/mux"

	"github.com/marlonfan/go2sky"
	"github.com/marlonfan/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)
	}
	tracer.WaitUntilRegister()

	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)
	}

	router := mux.NewRouter()

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

	// add handlers
	router.Methods("GET").Path("/middle").HandlerFunc(middleFunc(client, ts.URL))
	router.Methods("POST").Path("/end").HandlerFunc(endFunc())

	// call end service
	request, err := http.NewRequest("GET", fmt.Sprintf("%s/middle", 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 middleFunc(client *http.Client, url string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		log.Printf("middle func called with method: %s\n", r.Method)

		// do some operation
		time.Sleep(100 * time.Millisecond)

		newRequest, err := http.NewRequest("POST", url+"/end", nil)
		if err != nil {
			log.Printf("unable to create client: %+v\n", err)
			http.Error(w, err.Error(), 500)
			return
		}

		//Link the context of entry and exit spans
		newRequest = newRequest.WithContext(r.Context())

		res, err := client.Do(newRequest)
		if err != nil {
			log.Printf("call to end fund returned error: %+v\n", err)
			http.Error(w, err.Error(), 500)
			return
		}
		_ = res.Body.Close()
	}
}

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.

Jump to

Keyboard shortcuts

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