Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

Sentry フロントエンド監視の実践 2020:パフォーマンストラッキングと Release 管理

Sentry を使ったフロントエンド監視の導入はフロントエンド開発でますます広く使われるようになっています。本記事では実際のプロジェクトに基づき、その本質的な原理とベストプラクティスを掘り下げます。

基本的な使い方 ​

この上にさらに最適化を重ねることができます:

javascript
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T

async function fetchUser(id: string) {
  const res = await fetch(`/api/users/${id}`)
  return res.json() as Promise<{ id: string; name: string; email: string }>
}

type User = UnwrapPromise<ReturnType<typeof fetchUser>>

// 类型安全的事件系统
interface EventMap {
  login: { userId: string; timestamp: number }
  logout: { userId: string }
}

class TypedEmitter<T extends Record<string, any>> {
  private handlers = new Map<keyof T, Set<Function>>()
  on<K extends keyof T>(event: K, handler: (payload: T[K]) => void) {
    if (!this.handlers.has(event)) this.handlers.set(event, new Set())
    this.handlers.get(event)!.add(handler)
  }
  emit<K extends keyof T>(event: K, payload: T[K]) {
    this.handlers.get(event)?.forEach(h => h(payload))
  }
}

このパターンは大規模プロジェクトで非常に有用で、保守コストを大きく下げられます。

応用的な使い方 ​

実際のプロジェクトでの使い方はもう少し複雑です:

javascript
const express = require('express')
const app = express()

app.use(express.json())

class AppError extends Error {
  constructor(status, message) {
    super(message); this.statusCode = status
  }
}

const asyncHandler = (fn) => (req, res, next) =>
  Promise.resolve(fn(req, res, next)).catch(next)

app.get('/api/users/:id', asyncHandler(async (req, res) => {
  const user = await User.findById(req.params.id)
  if (!user) throw new AppError(404, '用户不存在')
  res.json({ data: user })
}))

この方法で、コードのテスト容易性と拡張性の両方が向上します。

実践事例 ​

以下は完全なサンプルです:

javascript
import { useReducer, useCallback } from 'react'

const initialState = { items: [], filter: '', sort: 'date' }

function reducer(state, action) {
  switch (action.type) {
    case 'SET_ITEMS': return { ...state, items: action.payload }
    case 'SET_FILTER': return { ...state, filter: action.payload }
    case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
    case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
    default: throw new Error(`Unknown: ${action.type}`)
  }
}

境界条件の処理に注意してください。これは本番環境で非常に重要です。

パフォーマンス最適化 ​

重要なのは本質的なロジックを理解することです:

javascript
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T

async function fetchUser(id: string) {
  const res = await fetch(`/api/users/${id}`)
  return res.json() as Promise<{ id: string; name: string; email: string }>
}

type User = UnwrapPromise<ReturnType<typeof fetchUser>>

// 类型安全的事件系统
interface EventMap {
  login: { userId: string; timestamp: number }
  logout: { userId: string }
}

class TypedEmitter<T extends Record<string, any>> {
  private handlers = new Map<keyof T, Set<Function>>()
  on<K extends keyof T>(event: K, handler: (payload: T[K]) => void) {
    if (!this.handlers.has(event)) this.handlers.set(event, new Set())
    this.handlers.get(event)!.add(handler)
  }
  emit<K extends keyof T>(event: K, payload: T[K]) {
    this.handlers.get(event)?.forEach(h => h(payload))
  }
}

パフォーマンスの最適化は具体的な場面に合わせる必要があり、すべてのケースで過度な最適化が必要なわけではありません。

よくある落とし穴 ​

以下の方法で改善できます:

javascript
const express = require('express')
const app = express()

app.use(express.json())

class AppError extends Error {
  constructor(status, message) {
    super(message); this.statusCode = status
  }
}

const asyncHandler = (fn) => (req, res, next) =>
  Promise.resolve(fn(req, res, next)).catch(next)

app.get('/api/users/:id', asyncHandler(async (req, res) => {
  const user = await User.findById(req.params.id)
  if (!user) throw new AppError(404, '用户不存在')
  res.json({ data: user })
}))

このアプローチは本番環境で半年以上安定して稼働しており、実際に検証されています。

まとめ ​

  • 本番環境で使う前には必ず互換性の検証を行ってください
  • チームでの協力においては、技術そのものより取り決めとドキュメントが重要です
  • コミュニティの動向に注目し、技術戦略は継続的に改善する必要があります
  • 新しい技術を使うために使うのではありません

MIT Licensed