Fixed slice index out of bounds error from fix-logs

This commit is contained in:
Travis Reeder
2017-06-21 08:24:06 -07:00
parent 6159deb22d
commit 47d78c3307
9 changed files with 38 additions and 101 deletions

View File

@@ -27,6 +27,7 @@ func NewFuncLogger(logDB models.FnLog) FuncLogger {
type writer struct { type writer struct {
bytes.Buffer bytes.Buffer
stderr bytes.Buffer // for logging to stderr
db models.FnLog db models.FnLog
ctx context.Context ctx context.Context
reqID string reqID string
@@ -36,28 +37,41 @@ type writer struct {
} }
func (w *writer) Close() error { func (w *writer) Close() error {
w.flush()
return w.db.InsertLog(context.TODO(), w.reqID, w.String()) return w.db.InsertLog(context.TODO(), w.reqID, w.String())
} }
func (w *writer) Write(b []byte) (int, error) { func (w *writer) Write(b []byte) (int, error) {
n, err := w.Buffer.Write(b) n, err := w.Buffer.Write(b)
// for now, also write to stderr so we can debug quick ;) // temp or should move to another FuncLogger implementation
// TODO this should be a separate FuncLogger but time is running short ! w.writeStdErr(b)
log := common.Logger(w.ctx)
log = log.WithFields(logrus.Fields{"user_log": true, "app_name": w.appName, "path": w.path, "image": w.image, "call_id": w.reqID})
for i := 0; i < len(b); i++ {
j := i
i = bytes.IndexByte(b[i:], '\n')
if i < 0 {
i = len(b)
}
log.Println(string(b[j:i]))
}
return n, err return n, err
} }
func (w *writer) writeStdErr(b []byte) {
// for now, also write to stderr so we can debug quick ;)
// TODO this should be a separate FuncLogger but time is running short !
endLine := bytes.IndexByte(b, '\n')
if endLine < 0 {
w.stderr.Write(b)
return
}
// we have a new line, so:
w.stderr.Write(b[0:endLine])
w.flush()
w.writeStdErr(b[endLine+1:])
}
func (w *writer) flush() {
log := common.Logger(w.ctx)
log = log.WithFields(logrus.Fields{"user_log": true, "app_name": w.appName, "path": w.path, "image": w.image, "call_id": w.reqID})
log.Println(w.stderr.String())
w.stderr.Reset()
}
// overrides Write, keeps Close // overrides Write, keeps Close
type limitWriter struct { type limitWriter struct {
n, max int n, max int

View File

@@ -1,2 +1,3 @@
bundle/ bundle/
.bundle/ .bundle/
func.yaml

View File

@@ -1,9 +0,0 @@
FROM funcy/ruby:dev
WORKDIR /worker
ADD Gemfile* /worker/
RUN bundle install
ADD . /worker/
ENTRYPOINT ["ruby", "error.rb"]

View File

@@ -1,77 +1,3 @@
# Error Function Image # Error Function Image
This images compares the payload info with the header. Raises an error.
## Requirements
- Oracle Functions API
## Development
### Building image locally
```
# SET BELOW TO YOUR DOCKER HUB USERNAME
USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
```
### Publishing to DockerHub
```
# tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-error:latest $USERNAME/func-error:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-error
```
### Testing image
```
./test.sh
```
## Running it on Oracle Functions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with Oracle Functions
With this command we are going to create an application with name `error`.
```
curl -X POST --data '{
"app": {
"name": "error",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-error",
"path": "/error",
}
}' http://$FUNCAPI/v1/apps/error/routes
```
#### Testing function
Now that we created our Oracle Functions route, let's test our new route
```
curl -X POST --data '{"input": "yoooo"}' http://$FUNCAPI/r/error/error
```

View File

@@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
set -ex set -ex
docker build -t username/func-error . docker build -t funcy/error .

View File

@@ -1,3 +1,5 @@
name: username/func-error name: funcy/error
build: version: 0.0.2
- ./build.sh runtime: ruby
entrypoint: ruby func.rb
path: /error

View File

@@ -17,4 +17,8 @@ func main() {
fmt.Printf("Hello %v!\n", p.Name) fmt.Printf("Hello %v!\n", p.Name)
log.Println("---> stderr goes to the server logs.") log.Println("---> stderr goes to the server logs.")
log.Println("---> LINE 2")
log.Println("---> LINE 3 with a break right here\nand LINE 4")
log.Println("---> LINE 5 with a double line break\n")
log.Println("---> LINE 6")
} }

View File

@@ -146,7 +146,6 @@ func (p *deploycmd) deploy(c *cli.Context, funcFilePath string) error {
func (p *deploycmd) route(c *cli.Context, ff *funcfile) error { func (p *deploycmd) route(c *cli.Context, ff *funcfile) error {
fmt.Printf("Updating route %s using image %s...\n", ff.Path, ff.FullName()) fmt.Printf("Updating route %s using image %s...\n", ff.Path, ff.FullName())
fmt.Printf("%+v\ntype: %v\n", ff, *ff.Type)
if err := resetBasePath(p.Configuration); err != nil { if err := resetBasePath(p.Configuration); err != nil {
return fmt.Errorf("error setting endpoint: %v", err) return fmt.Errorf("error setting endpoint: %v", err)
} }