Skip to content

Commit 6e01042

Browse files
committed
example: running instrumentation for logging
1 parent 2322e76 commit 6e01042

10 files changed

Lines changed: 119 additions & 69 deletions

File tree

frontend/src/store/index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createStore } from 'vuex'
2+
import * as Sentry from '@sentry/vue'
23
import api from '@/api'
34
import helper from '@/helper'
45

@@ -46,7 +47,10 @@ const store = createStore({
4647
const response = await api.get('user')
4748
commit('SET_USER', response.data)
4849
} catch (error) {
49-
console.error(error)
50+
Sentry.logger.error('Failed to load authenticated user', {
51+
'gibpotato.api.endpoint': 'user',
52+
'gibpotato.api.status_code': error.response?.status,
53+
})
5054
}
5155
},
5256
async getUsers({ commit }) {
@@ -84,25 +88,34 @@ const store = createStore({
8488
async toggleSentNotifications({ commit, getters }) {
8589
commit('TOGGLE_SENT_NOTIFICATIONS')
8690
try {
87-
const response = await api.patch('user', getters.user)
91+
await api.patch('user', getters.user)
8892
} catch (error) {
89-
console.error(error)
93+
Sentry.logger.warn('Notification preference may not have persisted', {
94+
'gibpotato.settings.notification_type': 'sent',
95+
'gibpotato.api.status_code': error.response?.status,
96+
})
9097
}
9198
},
9299
async toggleReceivedNotifications({ commit, getters }) {
93100
commit('TOGGLE_RECEIVED_NOTIFICATIONS')
94101
try {
95-
const response = await api.patch('user', getters.user)
102+
await api.patch('user', getters.user)
96103
} catch (error) {
97-
console.error(error)
104+
Sentry.logger.warn('Notification preference may not have persisted', {
105+
'gibpotato.settings.notification_type': 'received',
106+
'gibpotato.api.status_code': error.response?.status,
107+
})
98108
}
99109
},
100110
async toggleTooGoodToGoNotifications({ commit, getters }) {
101111
commit('TOGGLE_TOO_GOOD_TO_GO_NOTIFICATIONS')
102112
try {
103-
const response = await api.patch('user', getters.user)
113+
await api.patch('user', getters.user)
104114
} catch (error) {
105-
console.error(error)
115+
Sentry.logger.warn('Notification preference may not have persisted', {
116+
'gibpotato.settings.notification_type': 'too_good_to_go',
117+
'gibpotato.api.status_code': error.response?.status,
118+
})
106119
}
107120
},
108121
setRangeFilter({ commit }, range) {

frontend/src/views/Shop.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@
204204
import { computed } from 'vue'
205205
import { useStore } from 'vuex'
206206
207+
import * as Sentry from '@sentry/vue'
208+
207209
import api from '@/api'
208210
209211
import 'vue-select/dist/vue-select.css';
@@ -273,11 +275,21 @@ export default {
273275
this.purchaseSuccess = true
274276
this.code = response.data.code
275277
278+
Sentry.logger.info('Shop purchase completed', {
279+
'gibpotato.shop.product_id': this.product.id,
280+
'gibpotato.shop.purchase_mode': this.purchaseMode,
281+
'gibpotato.shop.is_gift': this.purchaseMode !== 'myself',
282+
})
283+
276284
await this.$store.dispatch('getUser')
277285
await this.$store.dispatch('getProducts')
278286
await this.$store.dispatch('getCollection')
279287
} catch (error) {
280-
console.log(error)
288+
Sentry.logger.warn('Shop purchase failed', {
289+
'gibpotato.shop.product_id': this.product?.id,
290+
'gibpotato.shop.purchase_mode': this.purchaseMode,
291+
'gibpotato.shop.status_code': error.response?.status,
292+
})
281293
this.modalError = error.response.data.error
282294
} finally {
283295
this.loading = false

potal/handler.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ func (h *Handler) EventsHandler(w http.ResponseWriter, r *http.Request, _ httpro
166166
}
167167

168168
h.emitEventMetric(txn.Context(), "potal.event.forwarded", "message")
169+
slog.InfoContext(txn.Context(), "Potato message forwarded to API",
170+
"gibpotato.event.type", "message",
171+
"gibpotato.message.sender", processedEvent.Sender,
172+
"gibpotato.message.receivers", len(processedEvent.Receivers),
173+
"gibpotato.potatoes.amount", processedEvent.Amount,
174+
"gibpotato.slack.channel", processedEvent.Channel,
175+
)
169176
txn.Status = sentry.SpanStatusOK
170177
}()
171178
}
@@ -200,6 +207,13 @@ func (h *Handler) EventsHandler(w http.ResponseWriter, r *http.Request, _ httpro
200207
}
201208

202209
h.emitEventMetric(txn.Context(), "potal.event.forwarded", "reaction_added")
210+
slog.InfoContext(txn.Context(), "Potato reaction forwarded to API",
211+
"gibpotato.event.type", "reaction_added",
212+
"gibpotato.message.sender", processedEvent.Sender,
213+
"gibpotato.message.receivers", len(processedEvent.Receivers),
214+
"gibpotato.potatoes.amount", processedEvent.Amount,
215+
"gibpotato.slack.channel", processedEvent.Channel,
216+
)
203217
txn.Status = sentry.SpanStatusOK
204218
}()
205219
case *slackevents.AppMentionEvent:

shopato/app/controllers/concerns/secure_potato_concern.rb

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,19 @@ module SecurePotatoConcern
44
private
55

66
def authenticate_🥔!
7-
if valid_authentication?
8-
Sentry.logger.debug("Authentication successful",
9-
request_path: request.path,
10-
request_method: request.method)
11-
else
12-
Sentry.logger.warn("Authentication failed - invalid or missing token",
13-
request_path: request.path,
14-
request_method: request.method,
15-
remote_ip: request.remote_ip)
16-
head :unauthorized
17-
end
7+
return if valid_authentication?
8+
9+
Sentry.logger.warn("Gift card request authentication failed",
10+
"gibpotato.auth.request_path": request.path,
11+
"gibpotato.auth.request_method": request.method,
12+
"gibpotato.auth.remote_ip": request.remote_ip)
13+
head :unauthorized
1814
end
1915

2016
def valid_authentication?
2117
token = request.headers["Authorization"]
2218
return false unless token
2319

24-
is_valid = token == Rails.application.config.gib_potato_token
25-
26-
unless is_valid
27-
Sentry.logger.debug("Token validation failed",
28-
token_present: token.present?,
29-
token_length: token&.length)
30-
end
31-
32-
is_valid
20+
token == Rails.application.config.gib_potato_token
3321
end
3422
end

shopato/app/controllers/gift_card_controller.rb

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ class GiftCardController < ActionController::Base
66
skip_forgery_protection
77

88
def create
9-
log_info("Gift card creation request received", name: params[:name], amount: params[:amount])
10-
119
gift_card = build_gift_card
1210

1311
if gift_card.valid?
@@ -29,38 +27,19 @@ def build_gift_card
2927
end
3028

3129
def handle_valid_gift_card(gift_card)
32-
log_debug("Gift card validation passed", gift_card_id: gift_card.id, amount: gift_card.amount)
33-
3430
service_result = gift_card_service.create_gift_card(gift_card.amount, gift_card.name)
3531

3632
if service_result[:success]
37-
handle_service_success(service_result, gift_card)
33+
render json: service_result[:gift_card], status: :created
3834
else
39-
handle_service_failure(service_result, gift_card)
35+
render json: {errors: service_result[:message]}, status: :unprocessable_content
4036
end
4137
end
4238

43-
def handle_service_success(result, gift_card)
44-
log_info("Gift card created successfully",
45-
gift_card_id: result[:gift_card]["id"],
46-
amount: result[:gift_card]["amount"],
47-
name: gift_card.name)
48-
render json: result[:gift_card], status: :created
49-
end
50-
51-
def handle_service_failure(result, gift_card)
52-
log_error("Gift card creation failed",
53-
error_message: result[:message],
54-
name: gift_card.name,
55-
amount: gift_card.amount)
56-
render json: {errors: result[:message]}, status: :unprocessable_content
57-
end
58-
5939
def handle_validation_errors(gift_card)
60-
log_warn("Gift card validation failed",
61-
errors: gift_card.errors.full_messages,
62-
name: gift_card.name,
63-
amount: gift_card.amount)
40+
log_warn("Gift card request failed validation",
41+
"gibpotato.giftcard.amount": gift_card.amount,
42+
"gibpotato.giftcard.validation_errors": gift_card.errors.full_messages)
6443
render json: {errors: gift_card.errors.full_messages}, status: :unprocessable_content
6544
end
6645

shopato/app/services/shopify_gift_card_service.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class ShopifyGiftCardService
1818
def initialize(shop: Rails.application.config.shopify_shop_domain, token: Rails.application.config.shopify_admin_access_token)
1919
@session = ShopifyAPI::Auth::Session.new(shop: shop, access_token: token)
2020
@client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)
21-
log_debug("ShopifyGiftCardService initialized", shop: shop)
2221
end
2322

2423
def create_gift_card(amount, name = nil, note: nil)
25-
log_info("Creating gift card via Shopify API", amount: amount, name: name)
24+
log_info("Creating gift card via Shopify",
25+
"gibpotato.giftcard.amount": amount)
2626

2727
begin
2828
variables = build_mutation_variables(amount, name, note)
@@ -45,7 +45,6 @@ def build_mutation_variables(amount, name, note)
4545
end
4646

4747
def execute_graphql_mutation(variables)
48-
log_trace("Executing Shopify GraphQL mutation", variables: variables)
4948
@client.query(query: CREATE_MUTATION, variables: variables)
5049
end
5150

@@ -68,10 +67,9 @@ def extract_user_errors(payload)
6867
end
6968

7069
def handle_user_errors(errors, amount, name)
71-
log_error("Shopify API returned user errors",
72-
errors: errors,
73-
amount: amount,
74-
name: name)
70+
log_error("Shopify returned user errors creating gift card",
71+
"gibpotato.giftcard.amount": amount,
72+
"gibpotato.giftcard.user_errors": errors)
7573
error!(errors.join(", "))
7674
end
7775

@@ -82,19 +80,18 @@ def build_success_response(payload)
8280
"code" => payload["giftCardCode"]
8381
}
8482

85-
log_info("Gift card created successfully via Shopify",
86-
gift_card_id: gift_card_data["id"],
87-
amount: gift_card_data["amount"])
83+
log_info("Gift card created via Shopify",
84+
"gibpotato.giftcard.id": gift_card_data["id"],
85+
"gibpotato.giftcard.amount": gift_card_data["amount"])
8886

8987
success!(gift_card: gift_card_data)
9088
end
9189

9290
def handle_api_error(error, amount, name)
93-
log_error("Shopify API error occurred",
94-
error_message: error.message,
95-
error_class: error.class.name,
96-
amount: amount,
97-
name: name)
91+
log_error("Shopify API request failed",
92+
"gibpotato.giftcard.amount": amount,
93+
"gibpotato.giftcard.error_class": error.class.name,
94+
"gibpotato.giftcard.error_message": error.message)
9895
error!("Shopify error: #{error.message}")
9996
end
10097

src/Controller/Api/ShopController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function Cake\Core\env;
1212
use function Sentry\getBaggage;
1313
use function Sentry\getTraceparent;
14+
use function Sentry\logger;
1415

1516
/**
1617
* @property \Authentication\Controller\Component\AuthenticationComponent $Authentication
@@ -147,6 +148,15 @@ public function purchase(): Response
147148
]);
148149
$purchase = $purchasesTable->saveOrFail($purchase);
149150
} else {
151+
logger()->warn(
152+
message: 'Gift card fulfilment failed',
153+
attributes: [
154+
'gibpotato.purchase.id' => $purchase->id,
155+
'gibpotato.shop.product_id' => $product->id,
156+
'gibpotato.api.status_code' => $response->getStatusCode(),
157+
],
158+
);
159+
150160
return $this->response
151161
->withStatus(500)
152162
->withType('json')
@@ -209,6 +219,17 @@ public function purchase(): Response
209219
);
210220
}
211221

222+
logger()->info(
223+
message: 'Shop purchase completed',
224+
attributes: [
225+
'gibpotato.purchase.id' => $purchase->id,
226+
'gibpotato.shop.product_id' => $product->id,
227+
'gibpotato.purchase.price' => $product->price,
228+
'gibpotato.purchase.is_gift' => $presentee !== null,
229+
'gibpotato.purchase.is_gift_card' => $product->type === Product::TYPE_GIFT_CARD,
230+
],
231+
);
232+
212233
return $this->response
213234
->withStatus(200)
214235
->withType('json')

src/Service/AwardService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ private function gibToUser(
8585
$toUser->slack_name,
8686
$event->amount,
8787
],
88+
attributes: [
89+
'gibpotato.event.type' => $event->type,
90+
'gibpotato.potatoes.amount' => $event->amount,
91+
'gibpotato.message.sender_id' => $fromUser->id,
92+
'gibpotato.message.receiver_id' => $toUser->id,
93+
],
8894
);
8995
}
9096
}

src/Service/ProgressionService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Model\Entity\Progression;
88
use App\Model\Entity\User;
99
use Cake\ORM\Locator\LocatorAwareTrait;
10+
use function Sentry\logger;
1011

1112
class ProgressionService
1213
{
@@ -44,6 +45,15 @@ public function progress(User $user): void
4445
]);
4546
$usersTable->saveOrFail($user);
4647

48+
logger()->info(
49+
message: 'User progression unlocked',
50+
attributes: [
51+
'gibpotato.user.id' => $user->id,
52+
'gibpotato.progression.id' => $progression->id,
53+
'gibpotato.progression.name' => $progression->name,
54+
],
55+
);
56+
4757
$this->sendProgressionNotification($user, $progression);
4858
}
4959

src/Service/UserService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Cake\ORM\Locator\LocatorAwareTrait;
1111
use Exception;
1212
use function Cake\Core\env;
13+
use function Sentry\logger;
1314

1415
class UserService
1516
{
@@ -70,6 +71,15 @@ public function getOrCreateUser(string $slackUserId): ?User
7071

7172
$user = $this->Users->saveOrFail($user);
7273

74+
logger()->info(
75+
message: 'New user created',
76+
attributes: [
77+
'gibpotato.user.id' => $user->id,
78+
'gibpotato.user.slack_user_id' => $user->slack_user_id,
79+
'gibpotato.user.is_bot' => $user->slack_is_bot,
80+
],
81+
);
82+
7383
$this->ApiTokens->generateApiToken($user);
7484

7585
$this->sendWelcomeNotification($user);

0 commit comments

Comments
 (0)