http

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 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
// Licensed to SkyAPM org under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. SkyAPM org licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package main

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

	"codehub-cn-east-2.devcloud.huaweicloud.com/jgz00001/go2sky.git"
	"codehub-cn-east-2.devcloud.huaweicloud.com/jgz00001/go2sky.git/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)
	}
}
Output:

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