mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* add user syslog writers to app users may specify a syslog url[s] on apps now and all functions under that app will spew their logs out to it. the docs have more information around details there, please review those (swagger and operating/logging.md), tried to implement to spec in some parts and improve others, open to feedback on format though, lots of liberty there. design decision wise, I am looking to the future and ignoring cold containers. the overhead of the connections there will not be worth it, so this feature only works for hot functions, since we're killing cold anyway (even if a user can just straight up exit a hot container). syslog connections will be opened against a container when it starts up, and then the call id that is logged gets swapped out for each call that goes through the container, this cuts down on the cost of opening/closing connections significantly. there are buffers to accumulate logs until we get a `\n` to actually write a syslog line, and a buffer to save some bytes when we're writing the syslog formatting as well. underneath writers re-use the line writer in certain scenarios (swapper). we could likely improve the ease of setting this up, but opening the syslog conns against a container seems worth it, and is a different path than the other func loggers that we create when we make a call object. the Close() stuff is a little tricky, not sure how to make it easier and have the ^ benefits, open to idears. this does add another vector of 'limits' to consider for more strict service operators. one being how many syslog urls can a user add to an app (infinite, atm) and the other being on the order of number of containers per host we could run out of connections in certain scenarios. there may be some utility in having multiple syslog sinks to send to, it could help with debugging at times to send to another destination or if a user is a client w/ someone and both want the function logs, e.g. (have used this for that in the past, specifically). this also doesn't work behind a proxy, which is something i'm open to fixing, but afaict will require a 3rd party dependency (we can pretty much steal what docker does). this is mostly of utility for those of us that work behind a proxy all the time, not really for end users. there are some unit tests. integration tests for this don't sound very fun to maintain. I did test against papertrail with each protocol and it works (and even times out if you're behind a proxy!). closes #337 * add trace to syslog dial
156 lines
3.6 KiB
Go
156 lines
3.6 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
|
|
"github.com/fnproject/fn/api/id"
|
|
"github.com/go-openapi/strfmt"
|
|
)
|
|
|
|
type App struct {
|
|
ID string `json:"id" db:"id"`
|
|
Name string `json:"name" db:"name"`
|
|
Config Config `json:"config,omitempty" db:"config"`
|
|
Annotations Annotations `json:"annotations,omitempty" db:"annotations"`
|
|
SyslogURL *string `json:"syslog_url,omitempty" db:"syslog_url"`
|
|
CreatedAt strfmt.DateTime `json:"created_at,omitempty" db:"created_at"`
|
|
UpdatedAt strfmt.DateTime `json:"updated_at,omitempty" db:"updated_at"`
|
|
}
|
|
|
|
func (a *App) SetDefaults() {
|
|
if time.Time(a.CreatedAt).IsZero() {
|
|
a.CreatedAt = strfmt.DateTime(time.Now())
|
|
}
|
|
if time.Time(a.UpdatedAt).IsZero() {
|
|
a.UpdatedAt = strfmt.DateTime(time.Now())
|
|
}
|
|
if a.Config == nil {
|
|
// keeps the json from being nil
|
|
a.Config = map[string]string{}
|
|
}
|
|
if a.ID == "" {
|
|
a.ID = id.New().String()
|
|
}
|
|
}
|
|
|
|
func (a *App) Validate() error {
|
|
if a.Name == "" {
|
|
return ErrAppsMissingName
|
|
}
|
|
if len(a.Name) > maxAppName {
|
|
return ErrAppsTooLongName
|
|
}
|
|
for _, c := range a.Name {
|
|
if !(unicode.IsLetter(c) || unicode.IsNumber(c) || c == '_' || c == '-') {
|
|
return ErrAppsInvalidName
|
|
}
|
|
}
|
|
err := a.Annotations.Validate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if a.SyslogURL != nil && *a.SyslogURL != "" {
|
|
sinks := strings.Split(*a.SyslogURL, ",")
|
|
for _, s := range sinks {
|
|
url, err := url.Parse(strings.TrimSpace(s))
|
|
fail := err != nil
|
|
if !fail {
|
|
switch url.Scheme {
|
|
case "udp", "tcp", "tls":
|
|
default:
|
|
fail = true
|
|
}
|
|
}
|
|
if fail {
|
|
return ErrInvalidSyslog(fmt.Sprintf(`invalid syslog url: "%v"`, s))
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) Clone() *App {
|
|
clone := new(App)
|
|
*clone = *a // shallow copy
|
|
|
|
// now deep copy the map
|
|
if a.Config != nil {
|
|
clone.Config = make(Config, len(a.Config))
|
|
for k, v := range a.Config {
|
|
clone.Config[k] = v
|
|
}
|
|
}
|
|
clone.ID = a.ID
|
|
return clone
|
|
}
|
|
|
|
func (a1 *App) Equals(a2 *App) bool {
|
|
// start off equal, check equivalence of each field.
|
|
// the RHS of && won't eval if eq==false so config checking is lazy
|
|
|
|
eq := true
|
|
eq = eq && a1.ID == a2.ID
|
|
eq = eq && a1.Name == a2.Name
|
|
eq = eq && a1.Config.Equals(a2.Config)
|
|
eq = eq && a1.Annotations.Equals(a2.Annotations)
|
|
// NOTE: datastore tests are not very fun to write with timestamp checks,
|
|
// and these are not values the user may set so we kind of don't care.
|
|
//eq = eq && time.Time(a1.CreatedAt).Equal(time.Time(a2.CreatedAt))
|
|
//eq = eq && time.Time(a1.UpdatedAt).Equal(time.Time(a2.UpdatedAt))
|
|
return eq
|
|
}
|
|
|
|
// Update adds entries from patch to a.Config and a.Annotations, and removes entries with empty values.
|
|
func (a *App) Update(patch *App) {
|
|
original := a.Clone()
|
|
|
|
if patch.Config != nil {
|
|
if a.Config == nil {
|
|
a.Config = make(Config)
|
|
}
|
|
for k, v := range patch.Config {
|
|
if v == "" {
|
|
delete(a.Config, k)
|
|
} else {
|
|
a.Config[k] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
if patch.SyslogURL != nil {
|
|
if *patch.SyslogURL == "" {
|
|
a.SyslogURL = nil // hides it from jason
|
|
} else {
|
|
a.SyslogURL = patch.SyslogURL
|
|
}
|
|
}
|
|
|
|
a.Annotations = a.Annotations.MergeChange(patch.Annotations)
|
|
|
|
if !a.Equals(original) {
|
|
a.UpdatedAt = strfmt.DateTime(time.Now())
|
|
}
|
|
}
|
|
|
|
var _ APIError = ErrInvalidSyslog("")
|
|
|
|
type ErrInvalidSyslog string
|
|
|
|
func (e ErrInvalidSyslog) Code() int { return http.StatusBadRequest }
|
|
func (e ErrInvalidSyslog) Error() string { return string(e) }
|
|
|
|
// AppFilter is the filter used for querying apps
|
|
type AppFilter struct {
|
|
Name string
|
|
// NameIn will filter by all names in the list (IN query)
|
|
NameIn []string
|
|
PerPage int
|
|
Cursor string
|
|
}
|