OpenTracing for Fission (#1079)
Added Opentracing integration using opencensus libraries for all Fission components.
This commit is contained in:
+15
-11
@@ -18,6 +18,7 @@ package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -29,12 +30,16 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"golang.org/x/net/context/ctxhttp"
|
||||
|
||||
"github.com/fission/fission/storagesvc"
|
||||
)
|
||||
|
||||
type (
|
||||
Client struct {
|
||||
url string
|
||||
url string
|
||||
httpClient *http.Client
|
||||
}
|
||||
)
|
||||
|
||||
@@ -42,13 +47,16 @@ type (
|
||||
func MakeClient(url string) *Client {
|
||||
return &Client{
|
||||
url: strings.TrimSuffix(url, "/") + "/v1",
|
||||
httpClient: &http.Client{
|
||||
Transport: &ochttp.Transport{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Upload sends the local file pointed to by filePath to the storage
|
||||
// service, along with the metadata. It returns a file ID that can be
|
||||
// used to retrieve the file.
|
||||
func (c *Client) Upload(filePath string, metadata *map[string]string) (string, error) {
|
||||
func (c *Client) Upload(ctx context.Context, filePath string, metadata *map[string]string) (string, error) {
|
||||
fi, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -82,9 +90,7 @@ func (c *Client) Upload(filePath string, metadata *map[string]string) (string, e
|
||||
req.Header["X-File-Size"] = []string{fmt.Sprintf("%v", fileSize)}
|
||||
req.Header["Content-Type"] = []string{contentType}
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := ctxhttp.Do(ctx, c.httpClient, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -114,7 +120,7 @@ func (c *Client) GetUrl(id string) string {
|
||||
|
||||
// Download fetches the file identified by ID to the local file path.
|
||||
// filePath must not exist.
|
||||
func (c *Client) Download(id string, filePath string) error {
|
||||
func (c *Client) Download(ctx context.Context, id string, filePath string) error {
|
||||
// url for id
|
||||
url := c.GetUrl(id)
|
||||
|
||||
@@ -132,7 +138,7 @@ func (c *Client) Download(id string, filePath string) error {
|
||||
defer f.Close()
|
||||
|
||||
// make request
|
||||
resp, err := http.Get(url)
|
||||
resp, err := ctxhttp.Get(ctx, c.httpClient, url)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Remove(filePath)
|
||||
@@ -154,7 +160,7 @@ func (c *Client) Download(id string, filePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Delete(id string) error {
|
||||
func (c *Client) Delete(ctx context.Context, id string) error {
|
||||
url := c.GetUrl(id)
|
||||
|
||||
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
||||
@@ -162,9 +168,7 @@ func (c *Client) Delete(id string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := ctxhttp.Do(ctx, c.httpClient, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@@ -64,7 +65,8 @@ func TestStorageService(t *testing.T) {
|
||||
|
||||
// store it
|
||||
metadata := make(map[string]string)
|
||||
fileId, err := client.Upload(tmpfile.Name(), &metadata)
|
||||
ctx := context.Background()
|
||||
fileId, err := client.Upload(ctx, tmpfile.Name(), &metadata)
|
||||
panicIf(err)
|
||||
|
||||
// make a temp file for verification
|
||||
@@ -73,7 +75,7 @@ func TestStorageService(t *testing.T) {
|
||||
os.Remove(retrievedfile.Name())
|
||||
|
||||
// retrieve uploaded file
|
||||
err = client.Download(fileId, retrievedfile.Name())
|
||||
err = client.Download(ctx, fileId, retrievedfile.Name())
|
||||
panicIf(err)
|
||||
defer os.Remove(retrievedfile.Name())
|
||||
|
||||
@@ -87,11 +89,11 @@ func TestStorageService(t *testing.T) {
|
||||
}
|
||||
|
||||
// delete uploaded file
|
||||
err = client.Delete(fileId)
|
||||
err = client.Delete(ctx, fileId)
|
||||
panicIf(err)
|
||||
|
||||
// make sure download fails
|
||||
err = client.Download(fileId, "xxx")
|
||||
err = client.Download(ctx, fileId, "xxx")
|
||||
if err == nil {
|
||||
log.Panic("Download succeeded but file isn't supposed to exist")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user