Skip to content

use structured logs (STR-415) #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/internal/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"math"
"os"
"reflect"
Expand All @@ -17,6 +16,7 @@ import (
ethcomm "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rs/zerolog/log"
)

func ToSha256(v string) string {
Expand All @@ -40,7 +40,7 @@ func RecoverAddress(message string, signature string) (ethcomm.Address, error) {
func BigNumberToFloat(bigNumber string, decimals uint64) (floatReturn float64, err error) {
floatReturn, err = strconv.ParseFloat(bigNumber, 64)
if err != nil {
log.Printf("Failed to convert bigNumber to float: %s", err)
log.Err(err).Msg("Failed to convert bigNumber to float")
err = StringError(err)
return
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func IsLocalEnv() bool {
func BetterStringify(jsonBody any) (betterString string, err error) {
bodyBytes, err := json.Marshal(jsonBody)
if err != nil {
log.Printf("Could not encode %+v to bytes: %s", jsonBody, err)
log.Err(err).Interface("body", jsonBody).Msg("Could not encode to bytes")
return betterString, StringError(err)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/internal/unit21/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package unit21

import (
"encoding/json"
"log"
"os"

"github.com/String-xyz/string-api/pkg/internal/common"
"github.com/String-xyz/string-api/pkg/model"
"github.com/String-xyz/string-api/pkg/repository"
"github.com/rs/zerolog/log"
)

type Action interface {
Expand Down Expand Up @@ -48,18 +48,18 @@ func (a action) Create(
url := "https://" + os.Getenv("UNIT21_ENV") + ".unit21.com/v1/events/create"
body, err := u21Post(url, mapToUnit21ActionEvent(instrument, actionData, unit21InstrumentId, eventSubtype))
if err != nil {
log.Printf("Unit21 Action create failed: %s", err)
log.Err(err).Msg("Unit21 Action create failed")
return "", common.StringError(err)
}

var u21Response *createEventResponse
err = json.Unmarshal(body, &u21Response)
if err != nil {
log.Printf("Reading body failed: %s", err)
log.Err(err).Msg("Reading body failed")
return "", common.StringError(err)
}

log.Printf("Create Action Unit21Id: %s", u21Response.Unit21Id)
log.Info().Str("unit21Id", u21Response.Unit21Id).Msg("Create Action")

return u21Response.Unit21Id, nil
}
Expand Down Expand Up @@ -90,10 +90,10 @@ func mapToUnit21ActionEvent(instrument model.Instrument, actionData actionData,

actionBody, err := common.BetterStringify(jsonBody)
if err != nil {
log.Printf("\nError creating action body\n")
log.Err(err).Msg("Error creating action body")
return jsonBody
}
log.Printf("\nCreate Action action body: %+v\n", actionBody)
log.Info().Str("body", actionBody).Msg("Create Action action body")

return jsonBody
}
27 changes: 13 additions & 14 deletions pkg/internal/unit21/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"

"github.com/String-xyz/string-api/pkg/internal/common"
"github.com/rs/zerolog/log"
)

func u21Put(url string, jsonBody any) (body []byte, err error) {
apiKey := os.Getenv("UNIT21_API_KEY")

reqBodyBytes, err := json.Marshal(jsonBody)
if err != nil {
log.Printf("Could not encode %+v to bytes: %s", jsonBody, err)
log.Err(err).Msg("Could not encode into bytes")
return nil, common.StringError(err)
}
log.Printf("reqBodyBytes: %s", reqBodyBytes)

log.Info().Str("body", string(reqBodyBytes)).Send()
bodyReader := bytes.NewReader(reqBodyBytes)

req, err := http.NewRequest(http.MethodPut, url, bodyReader)
if err != nil {
log.Printf("Could not create request for %s: %s", url, err)
log.Err(err).Str("url", url).Msg("Could not create request")
return nil, common.StringError(err)
}

Expand All @@ -39,20 +38,20 @@ func u21Put(url string, jsonBody any) (body []byte, err error) {

res, err := client.Do(req)
if err != nil {
log.Printf("Request failed to update %s: %s", url, err)
log.Err(err).Str("url", url).Msg("Request failed to update")
return nil, common.StringError(err)
}

defer res.Body.Close()

body, err = io.ReadAll(res.Body)
if err != nil {
log.Printf("Error extracting body from %s update request: %s", url, err)
log.Err(err).Str("url", url).Msg("Error extracting body")
return nil, common.StringError(err)
}

if res.StatusCode != 200 {
log.Printf("Request failed to update %s: %s", url, fmt.Sprint(res.StatusCode))
log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update")
err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body)))
return
}
Expand All @@ -66,15 +65,15 @@ func u21Post(url string, jsonBody any) (body []byte, err error) {
reqBodyBytes, err := json.Marshal(jsonBody)

if err != nil {
log.Printf("Could not encode %+v to bytes: %s", jsonBody, err)
log.Err(err).Msg("Could not encode into bytes")
return nil, common.StringError(err)
}

bodyReader := bytes.NewReader(reqBodyBytes)

req, err := http.NewRequest(http.MethodPost, url, bodyReader)
if err != nil {
log.Printf("Could not create request for %s: %s", url, err)
log.Err(err).Str("url", url).Msg("Could not create request")
return nil, common.StringError(err)
}

Expand All @@ -86,22 +85,22 @@ func u21Post(url string, jsonBody any) (body []byte, err error) {

res, err := client.Do(req)
if err != nil {
log.Printf("Request failed to update %s: %s", url, err)
log.Err(err).Str("url", url).Msg("Request failed to update")
return nil, common.StringError(err)
}

defer res.Body.Close()

body, err = io.ReadAll(res.Body)
if err != nil {
log.Printf("Error extracting body from %s update response: %s", url, err)
log.Err(err).Str("url", url).Msg("Error extracting body from")
return nil, common.StringError(err)
}

log.Printf("String of body from response: %s", string(body))
log.Info().Str("body", string(body)).Msgf("Strinb of body grom response")

if res.StatusCode != 200 {
log.Printf("Request failed to update %s: %s", url, fmt.Sprint(res.StatusCode))
log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update")
err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body)))
return
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/internal/unit21/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package unit21

import (
"encoding/json"
"log"
"os"

"github.com/String-xyz/string-api/pkg/internal/common"
"github.com/String-xyz/string-api/pkg/model"
"github.com/String-xyz/string-api/pkg/repository"
"github.com/rs/zerolog/log"
)

type Entity interface {
Expand Down Expand Up @@ -37,37 +37,37 @@ func (e entity) Create(user model.User) (unit21Id string, err error) {

communications, err := e.getCommunications(user.ID)
if err != nil {
log.Printf("Failed to gather Unit21 entity communications: %s", err)
log.Err(err).Msg("Failed to gather Unit21 entity communications")
return "", common.StringError(err)
}

digitalData, err := e.getEntityDigitalData(user.ID)
if err != nil {
log.Printf("Failed to gather Unit21 entity digitalData: %s", err)
log.Err(err).Msg("Failed to gather Unit21 entity digitalData")
return "", common.StringError(err)
}

customData, err := e.getCustomData(user.ID)
if err != nil {
log.Printf("Failed to gather Unit21 entity customData: %s", err)
log.Err(err).Msg("Failed to gather Unit21 entity customData")
return "", common.StringError(err)
}

url := "https://" + os.Getenv("UNIT21_ENV") + ".unit21.com/v1/entities/create"
body, err := u21Post(url, mapUserToEntity(user, communications, digitalData, customData))
if err != nil {
log.Printf("Unit21 Entity create failed: %s", err)
log.Err(err).Msg("Unit21 Entity create failed")
return "", common.StringError(err)
}

var entity *createEntityResponse
err = json.Unmarshal(body, &entity)
if err != nil {
log.Printf("Reading body failed: %s", err)
log.Err(err).Msg("Reading body failed")
return "", common.StringError(err)
}

log.Printf("Unit21Id: %s", entity.Unit21Id)
log.Info().Str("Unit21Id", entity.Unit21Id).Send()

return entity.Unit21Id, nil
}
Expand All @@ -79,21 +79,21 @@ func (e entity) Update(user model.User) (unit21Id string, err error) {

communications, err := e.getCommunications(user.ID)
if err != nil {
log.Printf("Failed to gather Unit21 entity communications: %s", err)
log.Err(err).Msg("Failed to gather Unit21 entity communications")
err = common.StringError(err)
return
}

digitalData, err := e.getEntityDigitalData(user.ID)
if err != nil {
log.Printf("Failed to gather Unit21 entity digitalData: %s", err)
log.Err(err).Msg("Failed to gather Unit21 entity digitalData")
err = common.StringError(err)
return
}

customData, err := e.getCustomData(user.ID)
if err != nil {
log.Printf("Failed to gather Unit21 entity customData: %s", err)
log.Err(err).Msg("Failed to gather Unit21 entity customData")
err = common.StringError(err)
return
}
Expand All @@ -103,20 +103,20 @@ func (e entity) Update(user model.User) (unit21Id string, err error) {
body, err := u21Put(url, mapUserToEntity(user, communications, digitalData, customData))

if err != nil {
log.Printf("Unit21 Entity create failed: %s", err)
log.Err(err).Msg("Unit21 Entity create failed")
err = common.StringError(err)
return
}

var entity *updateEntityResponse
err = json.Unmarshal(body, &entity)
if err != nil {
log.Printf("Reading body failed: %s", err)
log.Err(err).Msg("Reading body failed")
err = common.StringError(err)
return
}

log.Printf("Unit21Id: %s", entity.Unit21Id)
log.Info().Str("Unit21Id", entity.Unit21Id).Send()
return entity.Unit21Id, nil
}

Expand All @@ -130,7 +130,7 @@ func (e entity) AddInstruments(entityId string, instrumentIds []string) (err err

_, err = u21Put(url, instruments)
if err != nil {
log.Printf("Unit21 Entity Add Instruments failed: %s", err)
log.Err(err).Msg("Unit21 Entity Add Instruments failed")
err = common.StringError(err)
return
}
Expand All @@ -142,7 +142,7 @@ func (e entity) getCommunications(userId string) (communications entityCommunica
// Get user contacts
contacts, err := e.repo.Contact.ListByUserId(userId, 100, 0)
if err != nil {
log.Printf("Failed to get user contacts: %s", err)
log.Err(err).Msg("Failed to get user contacts")
err = common.StringError(err)
return
}
Expand All @@ -161,7 +161,7 @@ func (e entity) getCommunications(userId string) (communications entityCommunica
func (e entity) getEntityDigitalData(userId string) (deviceData entityDigitalData, err error) {
devices, err := e.repo.Device.ListByUserId(userId, 100, 0)
if err != nil {
log.Printf("Failed to get user devices: %s", err)
log.Err(err).Msg("Failed to get user devices")
err = common.StringError(err)
return
}
Expand All @@ -176,7 +176,7 @@ func (e entity) getEntityDigitalData(userId string) (deviceData entityDigitalDat
func (e entity) getCustomData(userId string) (customData entityCustomData, err error) {
devices, err := e.repo.UserToPlatform.ListByUserId(userId, 100, 0)
if err != nil {
log.Printf("Failed to get user platforms: %s", err)
log.Err(err).Msg("Failed to get user platforms")
err = common.StringError(err)
return
}
Expand Down
Loading