forked from TrueCloudLab/distribution
Merge pull request #343 from stevvooe/tracing-driver
context, storagedriver: trace function calls to Base storage driver
This commit is contained in:
commit
e5eddbc762
2 changed files with 34 additions and 22 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.google.com/p/go-uuid/uuid"
|
|
||||||
"github.com/docker/distribution"
|
"github.com/docker/distribution"
|
||||||
"github.com/docker/distribution/configuration"
|
"github.com/docker/distribution/configuration"
|
||||||
ctxu "github.com/docker/distribution/context"
|
ctxu "github.com/docker/distribution/context"
|
||||||
|
@ -32,11 +31,8 @@ import (
|
||||||
// fields should be protected.
|
// fields should be protected.
|
||||||
type App struct {
|
type App struct {
|
||||||
context.Context
|
context.Context
|
||||||
Config configuration.Configuration
|
|
||||||
|
|
||||||
// InstanceID is a unique id assigned to the application on each creation.
|
Config configuration.Configuration
|
||||||
// Provides information in the logs and context to identify restarts.
|
|
||||||
InstanceID string
|
|
||||||
|
|
||||||
router *mux.Router // main application router, configured with dispatchers
|
router *mux.Router // main application router, configured with dispatchers
|
||||||
driver storagedriver.StorageDriver // driver maintains the app global storage driver instance.
|
driver storagedriver.StorageDriver // driver maintains the app global storage driver instance.
|
||||||
|
@ -52,17 +48,6 @@ type App struct {
|
||||||
redis *redis.Pool
|
redis *redis.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value intercepts calls context.Context.Value, returning the current app id,
|
|
||||||
// if requested.
|
|
||||||
func (app *App) Value(key interface{}) interface{} {
|
|
||||||
switch key {
|
|
||||||
case "app.id":
|
|
||||||
return app.InstanceID
|
|
||||||
}
|
|
||||||
|
|
||||||
return app.Context.Value(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewApp takes a configuration and returns a configured app, ready to serve
|
// NewApp takes a configuration and returns a configured app, ready to serve
|
||||||
// requests. The app only implements ServeHTTP and can be wrapped in other
|
// requests. The app only implements ServeHTTP and can be wrapped in other
|
||||||
// handlers accordingly.
|
// handlers accordingly.
|
||||||
|
@ -70,11 +55,10 @@ func NewApp(ctx context.Context, configuration configuration.Configuration) *App
|
||||||
app := &App{
|
app := &App{
|
||||||
Config: configuration,
|
Config: configuration,
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
InstanceID: uuid.New(),
|
|
||||||
router: v2.RouterWithPrefix(configuration.HTTP.Prefix),
|
router: v2.RouterWithPrefix(configuration.HTTP.Prefix),
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Context = ctxu.WithLogger(app.Context, ctxu.GetLogger(app, "app.id"))
|
app.Context = ctxu.WithLogger(app.Context, ctxu.GetLogger(app, "instance.id"))
|
||||||
|
|
||||||
// Register the handler dispatchers.
|
// Register the handler dispatchers.
|
||||||
app.register(v2.RouteNameBase, func(ctx *Context, r *http.Request) http.Handler {
|
app.register(v2.RouteNameBase, func(ctx *Context, r *http.Request) http.Handler {
|
||||||
|
@ -200,7 +184,7 @@ func (app *App) configureEvents(configuration *configuration.Configuration) {
|
||||||
|
|
||||||
app.events.source = notifications.SourceRecord{
|
app.events.source = notifications.SourceRecord{
|
||||||
Addr: hostname,
|
Addr: hostname,
|
||||||
InstanceID: app.InstanceID,
|
InstanceID: ctxu.GetStringValue(app, "instance.id"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ package base
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/context"
|
||||||
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,6 +52,9 @@ type Base struct {
|
||||||
|
|
||||||
// GetContent wraps GetContent of underlying storage driver.
|
// GetContent wraps GetContent of underlying storage driver.
|
||||||
func (base *Base) GetContent(path string) ([]byte, error) {
|
func (base *Base) GetContent(path string) ([]byte, error) {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.GetContent")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
if !storagedriver.PathRegexp.MatchString(path) {
|
||||||
return nil, storagedriver.InvalidPathError{Path: path}
|
return nil, storagedriver.InvalidPathError{Path: path}
|
||||||
}
|
}
|
||||||
|
@ -60,6 +64,9 @@ func (base *Base) GetContent(path string) ([]byte, error) {
|
||||||
|
|
||||||
// PutContent wraps PutContent of underlying storage driver.
|
// PutContent wraps PutContent of underlying storage driver.
|
||||||
func (base *Base) PutContent(path string, content []byte) error {
|
func (base *Base) PutContent(path string, content []byte) error {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.PutContent")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
if !storagedriver.PathRegexp.MatchString(path) {
|
||||||
return storagedriver.InvalidPathError{Path: path}
|
return storagedriver.InvalidPathError{Path: path}
|
||||||
}
|
}
|
||||||
|
@ -69,6 +76,9 @@ func (base *Base) PutContent(path string, content []byte) error {
|
||||||
|
|
||||||
// ReadStream wraps ReadStream of underlying storage driver.
|
// ReadStream wraps ReadStream of underlying storage driver.
|
||||||
func (base *Base) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
func (base *Base) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.ReadStream")
|
||||||
|
|
||||||
if offset < 0 {
|
if offset < 0 {
|
||||||
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
||||||
}
|
}
|
||||||
|
@ -82,6 +92,9 @@ func (base *Base) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
||||||
|
|
||||||
// WriteStream wraps WriteStream of underlying storage driver.
|
// WriteStream wraps WriteStream of underlying storage driver.
|
||||||
func (base *Base) WriteStream(path string, offset int64, reader io.Reader) (nn int64, err error) {
|
func (base *Base) WriteStream(path string, offset int64, reader io.Reader) (nn int64, err error) {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.WriteStream")
|
||||||
|
|
||||||
if offset < 0 {
|
if offset < 0 {
|
||||||
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
||||||
}
|
}
|
||||||
|
@ -95,6 +108,9 @@ func (base *Base) WriteStream(path string, offset int64, reader io.Reader) (nn i
|
||||||
|
|
||||||
// Stat wraps Stat of underlying storage driver.
|
// Stat wraps Stat of underlying storage driver.
|
||||||
func (base *Base) Stat(path string) (storagedriver.FileInfo, error) {
|
func (base *Base) Stat(path string) (storagedriver.FileInfo, error) {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.Stat")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
if !storagedriver.PathRegexp.MatchString(path) {
|
||||||
return nil, storagedriver.InvalidPathError{Path: path}
|
return nil, storagedriver.InvalidPathError{Path: path}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +120,9 @@ func (base *Base) Stat(path string) (storagedriver.FileInfo, error) {
|
||||||
|
|
||||||
// List wraps List of underlying storage driver.
|
// List wraps List of underlying storage driver.
|
||||||
func (base *Base) List(path string) ([]string, error) {
|
func (base *Base) List(path string) ([]string, error) {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.List")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
||||||
return nil, storagedriver.InvalidPathError{Path: path}
|
return nil, storagedriver.InvalidPathError{Path: path}
|
||||||
}
|
}
|
||||||
|
@ -113,6 +132,9 @@ func (base *Base) List(path string) ([]string, error) {
|
||||||
|
|
||||||
// Move wraps Move of underlying storage driver.
|
// Move wraps Move of underlying storage driver.
|
||||||
func (base *Base) Move(sourcePath string, destPath string) error {
|
func (base *Base) Move(sourcePath string, destPath string) error {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.Move")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(sourcePath) {
|
if !storagedriver.PathRegexp.MatchString(sourcePath) {
|
||||||
return storagedriver.InvalidPathError{Path: sourcePath}
|
return storagedriver.InvalidPathError{Path: sourcePath}
|
||||||
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
||||||
|
@ -124,6 +146,9 @@ func (base *Base) Move(sourcePath string, destPath string) error {
|
||||||
|
|
||||||
// Delete wraps Delete of underlying storage driver.
|
// Delete wraps Delete of underlying storage driver.
|
||||||
func (base *Base) Delete(path string) error {
|
func (base *Base) Delete(path string) error {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.Move")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
if !storagedriver.PathRegexp.MatchString(path) {
|
||||||
return storagedriver.InvalidPathError{Path: path}
|
return storagedriver.InvalidPathError{Path: path}
|
||||||
}
|
}
|
||||||
|
@ -133,6 +158,9 @@ func (base *Base) Delete(path string) error {
|
||||||
|
|
||||||
// URLFor wraps URLFor of underlying storage driver.
|
// URLFor wraps URLFor of underlying storage driver.
|
||||||
func (base *Base) URLFor(path string, options map[string]interface{}) (string, error) {
|
func (base *Base) URLFor(path string, options map[string]interface{}) (string, error) {
|
||||||
|
_, done := context.WithTrace(context.Background())
|
||||||
|
defer done("Base.URLFor")
|
||||||
|
|
||||||
if !storagedriver.PathRegexp.MatchString(path) {
|
if !storagedriver.PathRegexp.MatchString(path) {
|
||||||
return "", storagedriver.InvalidPathError{Path: path}
|
return "", storagedriver.InvalidPathError{Path: path}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue