Merge pull request #1128 from harshavardhana/s3v2-s3v4
Move http trace to a common code, implement trace
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/minio/mc/pkg/client"
|
||||
"github.com/minio/mc/pkg/httptracer"
|
||||
"github.com/minio/minio-go-legacy"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
@@ -38,12 +39,9 @@ type s3Client struct {
|
||||
// New returns an initialized s3Client structure. if debug use a internal trace transport
|
||||
func New(config *client.Config) (client.Client, *probe.Error) {
|
||||
u := client.NewURL(config.HostURL)
|
||||
var transport http.RoundTripper
|
||||
switch {
|
||||
case config.Debug == true:
|
||||
transport = GetNewTraceTransport(NewTrace(), http.DefaultTransport)
|
||||
default:
|
||||
transport = http.DefaultTransport
|
||||
transport := http.DefaultTransport
|
||||
if config.Debug == true {
|
||||
transport = httptracer.GetNewTraceTransport(NewTrace(), http.DefaultTransport)
|
||||
}
|
||||
s3Conf := minio.Config{
|
||||
AccessKeyID: config.AccessKeyID,
|
||||
@@ -22,14 +22,14 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/mc/pkg/httptracer"
|
||||
)
|
||||
|
||||
// Trace - tracing structure
|
||||
type Trace struct {
|
||||
}
|
||||
type Trace struct{}
|
||||
|
||||
// NewTrace - initialize Trace structure
|
||||
func NewTrace() HTTPTracer {
|
||||
func NewTrace() httptracer.HTTPTracer {
|
||||
return Trace{}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Minio Client (C) 2015 Minio, Inc.
|
||||
*
|
||||
* Licensed 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 s3v4
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/minio/mc/pkg/client"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
)
|
||||
|
||||
// HTTPTracer provides callback hook mechanism for HTTP transport.
|
||||
type HTTPTracer interface {
|
||||
Request(req *http.Request) error
|
||||
Response(res *http.Response) error
|
||||
}
|
||||
|
||||
// RoundTripTrace interposes HTTP transport requests and respsonses using HTTPTracer hooks
|
||||
type RoundTripTrace struct {
|
||||
Trace HTTPTracer // User provides callback methods
|
||||
Transport http.RoundTripper // HTTP transport that needs to be intercepted
|
||||
}
|
||||
|
||||
// RoundTrip executes user provided request and response hooks for each HTTP call.
|
||||
func (t RoundTripTrace) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
||||
timeStamp := time.Now()
|
||||
|
||||
if t.Transport == nil {
|
||||
return nil, client.InvalidArgument{}
|
||||
}
|
||||
|
||||
res, err = t.Transport.RoundTrip(req)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if t.Trace != nil {
|
||||
err = t.Trace.Request(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = t.Trace.Response(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
console.Debugln("Response Time: ", time.Since(timeStamp).String()+"\n")
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// GetNewTraceTransport returns a traceable transport
|
||||
func GetNewTraceTransport(trace HTTPTracer, transport http.RoundTripper) RoundTripTrace {
|
||||
return RoundTripTrace{Trace: trace,
|
||||
Transport: transport}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/minio/mc/pkg/client"
|
||||
"github.com/minio/mc/pkg/httptracer"
|
||||
"github.com/minio/minio-go"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
@@ -38,12 +39,9 @@ type s3Client struct {
|
||||
// New returns an initialized s3Client structure. if debug use a internal trace transport
|
||||
func New(config *client.Config) (client.Client, *probe.Error) {
|
||||
u := client.NewURL(config.HostURL)
|
||||
var transport http.RoundTripper
|
||||
switch {
|
||||
case config.Debug == true:
|
||||
transport = GetNewTraceTransport(NewTrace(), http.DefaultTransport)
|
||||
default:
|
||||
transport = http.DefaultTransport
|
||||
transport := http.DefaultTransport
|
||||
if config.Debug == true {
|
||||
transport = httptracer.GetNewTraceTransport(NewTrace(), http.DefaultTransport)
|
||||
}
|
||||
s3Conf := minio.Config{
|
||||
AccessKeyID: config.AccessKeyID,
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/mc/pkg/httptracer"
|
||||
)
|
||||
|
||||
// Trace - tracing structure
|
||||
@@ -30,7 +31,7 @@ type Trace struct {
|
||||
}
|
||||
|
||||
// NewTrace - initialize Trace structure
|
||||
func NewTrace() HTTPTracer {
|
||||
func NewTrace() httptracer.HTTPTracer {
|
||||
return Trace{}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,13 +14,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package s3v2
|
||||
package httptracer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/minio/mc/pkg/client"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
)
|
||||
|
||||
@@ -41,7 +41,7 @@ func (t RoundTripTrace) RoundTrip(req *http.Request) (res *http.Response, err er
|
||||
timeStamp := time.Now()
|
||||
|
||||
if t.Transport == nil {
|
||||
return nil, client.InvalidArgument{}
|
||||
return nil, errors.New("Invalid Argument")
|
||||
}
|
||||
|
||||
res, err = t.Transport.RoundTrip(req)
|
||||
@@ -65,6 +65,9 @@ func (t RoundTripTrace) RoundTrip(req *http.Request) (res *http.Response, err er
|
||||
}
|
||||
|
||||
// GetNewTraceTransport returns a traceable transport
|
||||
//
|
||||
// Takes first argument a custom tracer which implements Response, Request() conforming to HTTPTracer interface.
|
||||
// Another argument can be a default transport or a custom http.RoundTripper implementation
|
||||
func GetNewTraceTransport(trace HTTPTracer, transport http.RoundTripper) RoundTripTrace {
|
||||
return RoundTripTrace{Trace: trace,
|
||||
Transport: transport}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Minio Client (C) 2015 Minio, Inc.
|
||||
*
|
||||
* Licensed 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 httptracer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type MySuite struct{}
|
||||
|
||||
var _ = Suite(&MySuite{})
|
||||
|
||||
func (s *MySuite) TestHTTPTracer(c *C) {
|
||||
//
|
||||
}
|
||||
Reference in New Issue
Block a user