|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/getsentry/sentry-go" |
| 10 | + "github.com/getsentry/sentry-go/attribute" |
| 11 | + sentryhttp "github.com/getsentry/sentry-go/http" |
| 12 | +) |
| 13 | + |
| 14 | +var meter sentry.Meter |
| 15 | + |
| 16 | +func main() { |
| 17 | + err := sentry.Init(sentry.ClientOptions{ |
| 18 | + Dsn: "https://53eb618df7695aaea90bcc816ff537e0@o447951.ingest.us.sentry.io/5774600", |
| 19 | + Debug: true, |
| 20 | + EnableTracing: true, |
| 21 | + TracesSampleRate: 1.0, |
| 22 | + BeforeSendMetric: func(metric *sentry.Metric) *sentry.Metric { |
| 23 | + // Filter metrics based on metric type and value |
| 24 | + switch metric.Type { |
| 25 | + case sentry.MetricTypeCounter: |
| 26 | + if v, ok := metric.Value.Int64(); ok && v > 5 { |
| 27 | + return nil // drop high-value counters |
| 28 | + } |
| 29 | + case sentry.MetricTypeGauge: |
| 30 | + if v, ok := metric.Value.Float64(); ok && v < 10.0 { |
| 31 | + return nil // drop low gauge readings |
| 32 | + } |
| 33 | + case sentry.MetricTypeDistribution: |
| 34 | + // keep all distributions |
| 35 | + } |
| 36 | + |
| 37 | + // Alternative: handle value types directly |
| 38 | + if v, ok := metric.Value.Int64(); ok && v > 1 { |
| 39 | + // handle all int64 values (counters) |
| 40 | + } |
| 41 | + |
| 42 | + return metric |
| 43 | + }, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + defer sentry.Flush(2 * time.Second) |
| 49 | + |
| 50 | + meter = sentry.NewMeter(context.Background()) |
| 51 | + // Attaching permanent attributes on the meter |
| 52 | + meter.SetAttributes( |
| 53 | + attribute.String("version", "1.0.0"), |
| 54 | + ) |
| 55 | + |
| 56 | + // Count metrics to measure occurrences of an event. |
| 57 | + meter.Count("sent_emails", 6, |
| 58 | + sentry.WithAttributes( |
| 59 | + attribute.String("email.provider", "sendgrid"), |
| 60 | + attribute.Int("email.number_of_recipients", 3), |
| 61 | + ), |
| 62 | + ) |
| 63 | + |
| 64 | + // Distribution metrics to measure the statistical distribution of a set of values. |
| 65 | + // Useful for measuring things and keeping track of the patterns, e.g. file sizes, response times, etc. |
| 66 | + meter.Distribution("file_upload_size", 3.14, |
| 67 | + sentry.WithUnit(sentry.UnitMegabyte), // Using standard unit constants |
| 68 | + sentry.WithAttributes( |
| 69 | + attribute.String("file.type", "image/png"), |
| 70 | + attribute.String("bucket.region", "us-west-2"), |
| 71 | + attribute.String("bucket.name", "user-uploads"), |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + // Distribution metric with duration unit |
| 76 | + meter.Distribution("response_time", 123.5, |
| 77 | + sentry.WithUnit(sentry.UnitMillisecond), |
| 78 | + sentry.WithAttributes( |
| 79 | + attribute.String("endpoint", "/api/users"), |
| 80 | + attribute.String("method", "GET"), |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + // Gauge metrics to measure a value at a specific point in time. |
| 85 | + // Useful for measuring values that can go up and down, e.g. temperature, memory usage, etc. |
| 86 | + meter.Gauge("memory_usage", 512.0, |
| 87 | + sentry.WithUnit(sentry.UnitMebibyte), // Using binary unit (MiB) |
| 88 | + sentry.WithAttributes( |
| 89 | + attribute.String("process", "worker"), |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + // Gauge metric with percentage |
| 94 | + meter.Gauge("cpu_usage", 0.75, |
| 95 | + sentry.WithUnit(sentry.UnitRatio), // Value from 0.0 to 1.0 |
| 96 | + sentry.WithAttributes( |
| 97 | + attribute.String("core", "0"), |
| 98 | + ), |
| 99 | + ) |
| 100 | + |
| 101 | + // Example using a custom scope for isolating metrics |
| 102 | + // This is useful when you want to capture metrics with a specific scope |
| 103 | + // that has different context data (user, tags, etc.) than the current scope |
| 104 | + customScope := sentry.NewScope() |
| 105 | + customScope.SetUser(sentry.User{ |
| 106 | + ID: "user-123", |
| 107 | + Email: "user@example.com", |
| 108 | + }) |
| 109 | + customScope.SetTag("environment", "staging") |
| 110 | + |
| 111 | + meter.Distribution("api_latency", 250.0, |
| 112 | + sentry.WithUnit(sentry.UnitMillisecond), |
| 113 | + sentry.WithScopeOverride(customScope), // Use a custom scope for this metric |
| 114 | + sentry.WithAttributes( |
| 115 | + attribute.String("endpoint", "/api/orders"), |
| 116 | + ), |
| 117 | + ) |
| 118 | + |
| 119 | + sentryHandler := sentryhttp.New(sentryhttp.Options{}) |
| 120 | + http.HandleFunc("/", sentryHandler.HandleFunc(handler)) |
| 121 | + |
| 122 | + fmt.Println("Listening on http://localhost:8080") |
| 123 | + if err := http.ListenAndServe(":8080", nil); err != nil { |
| 124 | + panic(err) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// handler is an example of using `WithCtx` to link the metric with the correct request trace. |
| 129 | +func handler(w http.ResponseWriter, r *http.Request) { |
| 130 | + // Use r.Context() and `WithCtx` to link the metric to the current request's span. |
| 131 | + // The sentryhttp middleware adds the span to the request context. |
| 132 | + meter.WithCtx(r.Context()).Count("page_views", 1, |
| 133 | + sentry.WithAttributes( |
| 134 | + attribute.String("path", r.URL.Path), |
| 135 | + attribute.String("method", r.Method), |
| 136 | + ), |
| 137 | + ) |
| 138 | + |
| 139 | + w.WriteHeader(http.StatusOK) |
| 140 | + fmt.Fprintln(w, "Hello, World!") |
| 141 | +} |
0 commit comments