Skip to content

Commit

Permalink
Merge pull request #160 from nyaruka/slog-models
Browse files Browse the repository at this point in the history
Slog models
  • Loading branch information
rowanseymour authored Oct 27, 2023
2 parents d713d82 + 3c1e737 commit c9f004a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions core/models/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"log/slog"
"strings"
"sync"
"time"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/nyaruka/mailroom/runtime"
cache "github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Refresh is our type for the pieces of org assets we want fresh (not cached)
Expand Down Expand Up @@ -728,7 +728,7 @@ func loadAssetType[A any](ctx context.Context, db *sql.DB, orgID OrgID, name str

as, err := f(ctx, db, orgID)

logrus.WithField("elapsed", time.Since(start)).WithField("org_id", orgID).WithField("count", len(as)).Debugf("loaded %s", name)
slog.Debug(fmt.Sprintf("loaded %s", name), "elapsed", time.Since(start), "org_id", orgID, "count", len(as))

return as, err
}
4 changes: 2 additions & 2 deletions core/models/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"log/slog"
"time"

"github.com/jmoiron/sqlx"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/null/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// FireID is our id for our event fires
Expand Down Expand Up @@ -420,7 +420,7 @@ func LoadEventFires(ctx context.Context, db *sqlx.DB, ids []FireID) ([]*EventFir
fires = append(fires, fire)
}

logrus.WithField("elapsed", time.Since(start)).WithField("count", len(fires)).Debug("event fires loaded")
slog.Debug("event fires loaded", "elapsed", time.Since(start), "count", len(fires))

return fires, nil
}
Expand Down
6 changes: 3 additions & 3 deletions core/models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"log/slog"
"net/url"
"strconv"
"time"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/nyaruka/null/v3"
"github.com/nyaruka/redisx"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// URNID is our type for urn ids, which can be null
Expand Down Expand Up @@ -321,7 +321,7 @@ func LoadContacts(ctx context.Context, db Queryer, oa *OrgAssets, ids []ContactI
for _, u := range e.URNs {
urn, err := u.AsURN(oa)
if err != nil {
logrus.WithField("urn", u).WithField("org_id", oa.OrgID()).WithField("contact_id", contact.id).Warn("invalid URN, ignoring")
slog.Warn("invalid URN, ignoring", "urn", u, "org_id", oa.OrgID(), "contact_id", contact.id)
continue
}
contactURNs = append(contactURNs, urn)
Expand All @@ -340,7 +340,7 @@ func LoadContacts(ctx context.Context, db Queryer, oa *OrgAssets, ids []ContactI
contacts = append(contacts, contact)
}

logrus.WithField("elapsed", time.Since(start)).WithField("count", len(contacts)).Debug("loaded contacts")
slog.Debug("loaded contacts", "elapsed", time.Since(start), "count", len(contacts))

return contacts, nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/models/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"log/slog"
"time"

"github.com/nyaruka/gocommon/dbutil"
Expand All @@ -15,7 +16,6 @@ import (

"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// FlowID is the type for flow IDs
Expand Down Expand Up @@ -166,7 +166,7 @@ func loadFlow(ctx context.Context, db *sql.DB, sql string, orgID OrgID, arg any)
return nil, errors.Wrapf(err, "error reading flow definition by: %s", arg)
}

logrus.WithField("elapsed", time.Since(start)).WithField("org_id", orgID).WithField("flow", arg).Debug("loaded flow")
slog.Debug("loaded flow", "elapsed", time.Since(start), "org_id", orgID, "flow", arg)

return flow, nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/models/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"database/sql"
"encoding/json"
"log/slog"
"time"

"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"

"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Location is our mailroom type for administrative locations
Expand Down Expand Up @@ -108,7 +108,7 @@ func loadLocations(ctx context.Context, db *sql.DB, oa *OrgAssets) ([]assets.Loc
return nil, errors.Wrapf(err, "error unmarshalling hierarchy: %s", string(locationJSON))
}

logrus.WithField("elapsed", time.Since(start)).WithField("org_id", oa.orgID).Debug("loaded locations")
slog.Debug("loaded locations", "elapsed", time.Since(start), "org_id", oa.orgID)

return []assets.LocationHierarchy{hierarchy}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"log/slog"
"strings"
"time"

Expand All @@ -22,7 +23,6 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/null/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// maximum number of repeated messages to same contact allowed in 5 minute window
Expand Down Expand Up @@ -361,7 +361,7 @@ func newOutgoingTextMsg(rt *runtime.Runtime, org *Org, channel *Channel, contact
m.Status = MsgStatusFailed
m.FailedReason = MsgFailedLooping

logrus.WithFields(logrus.Fields{"contact_id": contact.ID(), "text": out.Text(), "repetitions": repetitions}).Error("too many repetitions, failing message")
slog.Error("too many repetitions, failing message", "contact_id", contact.ID(), "text", out.Text(), "repetitions", repetitions)
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/models/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"mime"
"net/http"
"path/filepath"
Expand All @@ -25,7 +26,6 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/null/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Register a airtime service factory with the engine
Expand Down Expand Up @@ -199,7 +199,7 @@ func LoadOrg(ctx context.Context, cfg *runtime.Config, db *sql.DB, orgID OrgID)
return nil, errors.Wrapf(err, "error unmarshalling org")
}

logrus.WithField("elapsed", time.Since(start)).WithField("org_id", orgID).Debug("loaded org environment")
slog.Debug("loaded org environment", "elapsed", time.Since(start), "org_id", orgID)

return org, nil
}
Expand Down
16 changes: 8 additions & 8 deletions core/models/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"log/slog"
"net/url"
"path"
"time"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/null/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type SessionID int64
Expand Down Expand Up @@ -373,7 +373,7 @@ func (s *Session) Update(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
if rt.Config.SessionStorage == "s3" {
err := WriteSessionOutputsToStorage(ctx, rt, []*Session{s})
if err != nil {
logrus.WithError(err).Error("error writing session to s3")
slog.Error("error writing session to s3", "error", err)
}

// don't write output in our SQL
Expand Down Expand Up @@ -423,7 +423,7 @@ func (s *Session) Update(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
// update all modified runs at once
err = BulkQuery(ctx, "update runs", tx, sqlUpdateRun, updatedRuns)
if err != nil {
logrus.WithError(err).WithField("session", string(output)).Error("error while updating runs for session")
slog.Error("error while updating runs for session", "error", err, "session", string(output))
return errors.Wrapf(err, "error updating runs")
}

Expand Down Expand Up @@ -787,7 +787,7 @@ func FindWaitingSessionForContact(ctx context.Context, db *sqlx.DB, st storage.S
return nil, errors.Wrapf(err, "error reading session from storage: %s", session.OutputURL())
}

logrus.WithField("elapsed", time.Since(start)).WithField("output_url", session.OutputURL()).Debug("loaded session from storage")
slog.Debug("loaded session from storage", "elapsed", time.Since(start), "output_url", session.OutputURL())
session.s.Output = null.String(output)
}

Expand Down Expand Up @@ -817,7 +817,7 @@ func WriteSessionOutputsToStorage(ctx context.Context, rt *runtime.Runtime, sess
s.s.OutputURL = null.String(uploads[i].URL)
}

logrus.WithField("elapsed", time.Since(start)).WithField("count", len(sessions)).Debug("wrote sessions to s3")
slog.Debug("wrote sessions to s3", "elapsed", time.Since(start), "count", len(sessions))
return nil
}

Expand Down Expand Up @@ -896,7 +896,7 @@ func exitSessionBatch(ctx context.Context, tx *sqlx.Tx, sessionIDs []SessionID,
return errors.Wrapf(err, "error exiting sessions")
}

logrus.WithField("count", len(contactIDs)).WithField("elapsed", time.Since(start)).Debug("exited session batch")
slog.Debug("exited session batch", "count", len(contactIDs), "elapsed", time.Since(start))

// then the runs that belong to these sessions
start = time.Now()
Expand All @@ -907,7 +907,7 @@ func exitSessionBatch(ctx context.Context, tx *sqlx.Tx, sessionIDs []SessionID,
}

rows, _ := res.RowsAffected()
logrus.WithField("count", rows).WithField("elapsed", time.Since(start)).Debug("exited session batch runs")
slog.Debug("exited session batch runs", "count", rows, "elapsed", time.Since(start))

// and finally the contacts from each session
start = time.Now()
Expand All @@ -918,7 +918,7 @@ func exitSessionBatch(ctx context.Context, tx *sqlx.Tx, sessionIDs []SessionID,
}

rows, _ = res.RowsAffected()
logrus.WithField("count", rows).WithField("elapsed", time.Since(start)).Debug("exited session batch contacts")
slog.Debug("exited session batch contacts", "count", rows, "elapsed", time.Since(start))

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions core/models/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"log/slog"
"time"

"github.com/jmoiron/sqlx"
"github.com/nyaruka/gocommon/dbutil"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Queryer lets us pass anything that supports QueryContext to a function (sql.DB, sql.Tx, sqlx.DB, sqlx.Tx)
Expand Down Expand Up @@ -51,7 +51,7 @@ func BulkQuery[T any](ctx context.Context, label string, tx DBorTx, sql string,
return errors.Wrap(err, "error making bulk query")
}

logrus.WithField("elapsed", time.Since(start)).WithField("rows", len(structs)).Infof("%s bulk sql complete", label)
slog.Info(fmt.Sprintf("%s bulk sql complete", label), "elapsed", time.Since(start), "rows", len(structs))

return nil
}
Expand All @@ -67,7 +67,7 @@ func BulkQueryBatches(ctx context.Context, label string, tx DBorTx, sql string,
return errors.Wrap(err, "error making bulk batch query")
}

logrus.WithField("elapsed", time.Since(start)).WithField("rows", len(batch)).WithField("batch", i+1).Infof("%s bulk sql batch complete", label)
slog.Info(fmt.Sprintf("%s bulk sql batch complete", label), "elapsed", time.Since(start), "rows", len(batch), "batch", i+1)
}

return nil
Expand Down

0 comments on commit c9f004a

Please sign in to comment.