Skip to content

Commit

Permalink
feat: added logging, error page
Browse files Browse the repository at this point in the history
feat: added logging, error page
  • Loading branch information
telpirion authored Oct 18, 2024
2 parents 785b3db + e7cad5c commit cb4628a
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 24 deletions.
34 changes: 28 additions & 6 deletions DevelopmentLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Friction Log

## Week 1

For this week's activities, we need to accomplish the following:

- [x] Deploy a Gemma2 model to an endpoint.
Expand All @@ -14,23 +16,27 @@ For this week's activities, we need to accomplish the following:

_The app has been deployed [here](https://myherodotus-1025771077852.us-west1.run.app/)._

- [] Instrument the application to log to Cloud Observability.
- [x] Instrument the application for Cloud Observability (Logging).

_I have instrumented the application for Cloud Logging._

- [ ] Instrument the application for Cloud Observability (Monitoring).

- [x] Persist model interactions into a Database.

_I have integrated Firestore into the app._

- [] Identify data that needs to be persisted to make response history useful.
- [ ] Identify data that needs to be persisted to make response history useful.

_I have integrated Firebase auth into the app. This asks users to sign in so that their interactions are
stored keyed into the user's email. I may want to separately store query & responses from the models to
track their accuracy._

## Tracking future upgrades to app

- [] Provide feedback mechanism for users to rate responses.
- [] Track user feedback across sessions and users.
- [] Tag user feedback with model type, endpoint ID, prompt
- [ ] Provide feedback mechanism for users to rate responses.
- [ ] Track user feedback across sessions and users.
- [ ] Tag user feedback with model type, endpoint ID, prompt

## Learning how to create a templated web server with Go

Expand Down Expand Up @@ -167,10 +173,26 @@ us-west1-docker.pkg.dev/${PROJECT_ID}/my-herodotus/base-image:v1
docker push us-west1-docker.pkg.dev/${PROJECT_ID}/my-herodotus/base-image:v1
```

<<Happy>> Deploying a new version of my web app from Artifact Registry was shockingly intuitive.

Sources:

+ https://github.com/telpirion/telpirion_com/blob/main/README.md
+ https://medium.com/@manzurulhoque/deploying-a-golang-web-app-to-google-cloud-run-a-step-by-step-guide-619e6bb1836e
+ https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-go-service
+ https://phoenixnap.com/kb/docker-environment-variables
+ https://phoenixnap.com/kb/docker-environment-variables

## Integrating Cloud Observability (Logging)

The basic quickstart is in Python only :/.

<<Anxious>> The version of the tutorial in Go says "use standard logging," but it shows how to use the
cloud.google.com/go/logging library (not the standard `log` package).

<<Curious>> It seems like reinitializing the LoggingClient each time I need to log a message is
a bit repetitive. I wonder if there is a better pattern for this?

Sources:

+ https://cloud.google.com/logging/docs/setup/go
+ https://cloud.google.com/logging/docs/write-query-log-entries-python
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FROM golang:1.23 AS build
ENV CGO_ENABLED=0
ENV ENDPOINT_ID=3122353538139684864
ENV COLLECTION_NAME=HerodotusStaging
ENV LOGGER_NAME=HerodotusStaging

# TODO(telpirion): Delete this before checking in
#ENV PROJECT_ID=definitely-not-my-project
Expand Down
6 changes: 6 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package main

import (
"context"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -69,6 +70,7 @@ func saveConversation(convo ConversationBit, userEmail, projectID string) error

client, err := firestore.NewClientWithDatabase(ctx, projectID, DBName)
if err != nil {
LogError(fmt.Sprintf("firestore.Client: %v\n", err))
return err
}
defer client.Close()
Expand All @@ -85,6 +87,7 @@ func getConversation(userEmail, projectID string) ([]ConversationBit, error) {
conversations := []ConversationBit{}
client, err := firestore.NewClientWithDatabase(ctx, projectID, DBName)
if err != nil {
LogError(fmt.Sprintf("firestore.Client: %v\n", err))
return conversations, err
}
defer client.Close()
Expand All @@ -96,6 +99,7 @@ func getConversation(userEmail, projectID string) ([]ConversationBit, error) {
return conversations, nil
}
if err != nil {
LogError(fmt.Sprintf("firestore.DocumentRef: %v\n", err))
return conversations, err
}

Expand All @@ -106,11 +110,13 @@ func getConversation(userEmail, projectID string) ([]ConversationBit, error) {
break
}
if err != nil {
LogError(fmt.Sprintf("Firestore Iterator: %v\n", err))
return conversations, err
}
var convo ConversationBit
err = doc.DataTo(&convo)
if err != nil {
LogError(fmt.Sprintf("Firestore document unmarshaling: %v\n", err))
continue
}
conversations = append(conversations, convo)
Expand Down
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.4
)

require cloud.google.com/go/longrunning v0.6.0 // indirect
require (
cloud.google.com/go/logging v1.12.0 // indirect
cloud.google.com/go/longrunning v0.6.1 // indirect
)

require (
cloud.google.com/go v0.115.1 // indirect
cloud.google.com/go/aiplatform v1.68.0
cloud.google.com/go/auth v0.9.3 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/iam v1.2.0 // indirect
cloud.google.com/go/compute/metadata v0.5.1 // indirect
cloud.google.com/go/iam v1.2.1 // indirect
github.com/bytedance/sonic v1.12.2 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand All @@ -35,7 +38,7 @@ require (
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.3 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -56,16 +59,16 @@ require (
golang.org/x/arch v0.10.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/api v0.196.0
google.golang.org/api v0.197.0
google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.0
google.golang.org/grpc v1.66.2
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ cloud.google.com/go/auth/oauth2adapt v0.2.4 h1:0GWE/FUsXhf6C+jAkWgYm7X9tK8cuEIfy
cloud.google.com/go/auth/oauth2adapt v0.2.4/go.mod h1:jC/jOpwFP6JBxhB3P5Rr0a9HLMC/Pe3eaL4NmdvqPtc=
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
cloud.google.com/go/compute/metadata v0.5.1 h1:NM6oZeZNlYjiwYje+sYFjEpP0Q0zCan1bmQW/KmIrGs=
cloud.google.com/go/compute/metadata v0.5.1/go.mod h1:C66sj2AluDcIqakBq/M8lw8/ybHgOZqin2obFxa/E5k=
cloud.google.com/go/firestore v1.17.0 h1:iEd1LBbkDZTFsLw3sTH50eyg4qe8eoG6CjocmEXO9aQ=
cloud.google.com/go/firestore v1.17.0/go.mod h1:69uPx1papBsY8ZETooc71fOhoKkD70Q1DwMrtKuOT/Y=
cloud.google.com/go/iam v1.2.0 h1:kZKMKVNk/IsSSc/udOb83K0hL/Yh/Gcqpz+oAkoIFN8=
cloud.google.com/go/iam v1.2.0/go.mod h1:zITGuWgsLZxd8OwAlX+eMFgZDXzBm7icj1PVTYG766Q=
cloud.google.com/go/iam v1.2.1 h1:QFct02HRb7H12J/3utj0qf5tobFh9V4vR6h9eX5EBRU=
cloud.google.com/go/iam v1.2.1/go.mod h1:3VUIJDPpwT6p/amXRC5GY8fCCh70lxPygguVtI0Z4/g=
cloud.google.com/go/logging v1.12.0 h1:ex1igYcGFd4S/RZWOCU51StlIEuey5bjqwH9ZYjHibk=
cloud.google.com/go/logging v1.12.0/go.mod h1:wwYBt5HlYP1InnrtYI0wtwttpVU1rifnMT7RejksUAM=
cloud.google.com/go/longrunning v0.6.0 h1:mM1ZmaNsQsnb+5n1DNPeL0KwQd9jQRqSqSDEkBZr+aI=
cloud.google.com/go/longrunning v0.6.0/go.mod h1:uHzSZqW89h7/pasCWNYdUpwGz3PcVWhrWupreVPYLts=
cloud.google.com/go/longrunning v0.6.1 h1:lOLTFxYpr8hcRtcwWir5ITh1PAKUD/sG2lKrTSYjyMc=
cloud.google.com/go/longrunning v0.6.1/go.mod h1:nHISoOZpBcmlwbJmiVk5oDRz0qG/ZxPynEGs1iZ79s0=
cloud.google.com/go/vertexai v0.13.1 h1:E6I+eA6vNQxz7/rb0wdILdKg4hFmMNWZLp+dSy9DnEo=
cloud.google.com/go/vertexai v0.13.1/go.mod h1:25DzKFzP9JByYxcNjJefu/px2dRjcRpCDSdULYL2avI=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down Expand Up @@ -92,6 +100,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.3.3 h1:QRje2j5GZimBzlbhGA2V2QlGNgL8G6e+wGo/+/2bWI0=
github.com/googleapis/enterprise-certificate-proxy v0.3.3/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA=
github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gTgghdIA6Stxb52D5RnLI1SLyw=
github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA=
github.com/googleapis/gax-go/v2 v2.13.0 h1:yitjD5f7jQHhyDsnhKEBU52NdvvdSeGzlAnDPT0hH1s=
github.com/googleapis/gax-go/v2 v2.13.0/go.mod h1:Z/fvTZXF8/uw7Xu5GuslPw+bplx6SS338j1Is2S+B7A=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
Expand Down Expand Up @@ -170,6 +180,8 @@ golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -197,6 +209,8 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.196.0 h1:k/RafYqebaIJBO3+SMnfEGtFVlvp5vSgqTUF54UN/zg=
google.golang.org/api v0.196.0/go.mod h1:g9IL21uGkYgvQ5BZg6BAtoGJQIm8r6EgaAbpNey5wBE=
google.golang.org/api v0.197.0 h1:x6CwqQLsFiA5JKAiGyGBjc2bNtHtLddhJCE2IKuhhcQ=
google.golang.org/api v0.197.0/go.mod h1:AuOuo20GoQ331nq7DquGHlU6d+2wN2fZ8O0ta60nRNw=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand All @@ -215,6 +229,8 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c=
google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo=
google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
20 changes: 16 additions & 4 deletions js/signIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ const signIn = () => {

window.location = `/home?user=${user.email}`;
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
const code = error.code;
const message = error.message;
const email = error.customData.email;
const credential = GoogleAuthProvider.credentialFromError(error);

console.log(`
Error code: ${errorCode}
Error message: ${errorMessage}
Error code: ${code}
Error message: ${message}
Email: ${email}
`)
const xhr = new XMLHttpRequest();
xhr.open("POST", "/logClientError", true)
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(
{
code,
email,
message,
credential,
}
));
window.location = "/error"
});
}

Expand Down
53 changes: 53 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"fmt"
"log"
"os"

"cloud.google.com/go/logging"
)

var loggerName string = "HerodotusDev"

// For now, print logs both to stdout and to monitoring
func _logInternal(fullMessage string, sev logging.Severity) {
projectID := os.Getenv("PROJECT_ID")

ctx := context.Background()
loggingClient, err := logging.NewClient(ctx, projectID)
if err != nil {
log.Fatal(err)
}
defer loggingClient.Close()

if _loggerName, ok := os.LookupEnv("LOGGER_NAME"); ok {
loggerName = _loggerName
}

logger := loggingClient.Logger(loggerName).StandardLogger(sev)

log.Println(fullMessage)
logger.Println(fullMessage)
}

func LogError(message string) {
fullMessage := fmt.Sprintf("error: \n%v\n", message)
_logInternal(fullMessage, logging.Error)
}

func LogInfo(message string) {
fullMessage := fmt.Sprintf("info: \n%v\n", message)
_logInternal(fullMessage, logging.Info)
}

func LogDebug(message string) {
fullMessage := fmt.Sprintf("debug: \n%v\n", message)
_logInternal(fullMessage, logging.Debug)
}

func LogWarning(message string) {
fullMessage := fmt.Sprintf("warning: \n%v\n", message)
_logInternal(fullMessage, logging.Warning)
}
Loading

0 comments on commit cb4628a

Please sign in to comment.