Finish snapshots the recorded events once and sends their payload after releasing the lock. Sampling and telemetry eligibility apply to immediate and pending events alike.
()
| 319 | // Finish snapshots the recorded events once and sends their payload after releasing the lock. |
| 320 | // Sampling and telemetry eligibility apply to immediate and pending events alike. |
| 321 | func (s *service) Finish() { |
| 322 | s.mu.Lock() |
| 323 | |
| 324 | if s.finished { |
| 325 | s.mu.Unlock() |
| 326 | return |
| 327 | } |
| 328 | s.finished = true |
| 329 | |
| 330 | if s.sampleRate > 0 && s.sampleRate < 100 && int(s.sampleBucket) >= s.sampleRate { |
| 331 | s.mu.Unlock() |
| 332 | return |
| 333 | } |
| 334 | |
| 335 | // When the service has been disabled mid-invocation (e.g. an enterprise host |
| 336 | // was contacted), discard any recorded events. We still call the flusher |
| 337 | // with an empty payload so that the log-mode flusher can surface the |
| 338 | // absence of telemetry rather than leaving the user staring at silence. |
| 339 | events := s.events |
| 340 | if s.disabled { |
| 341 | events = nil |
| 342 | } |
| 343 | |
| 344 | payload := SendTelemetryPayload{ |
| 345 | Events: make([]PayloadEvent, len(events)), |
| 346 | } |
| 347 | |
| 348 | for i, recorded := range events { |
| 349 | event := recorded.event |
| 350 | |
| 351 | dimensions := map[string]string{ |
| 352 | "timestamp": recorded.recordedAt.UTC().Format("2006-01-02T15:04:05.000Z"), |
| 353 | } |
| 354 | maps.Copy(dimensions, s.commonDimensions) |
| 355 | maps.Copy(dimensions, event.Dimensions) |
| 356 | |
| 357 | payload.Events[i] = PayloadEvent{ |
| 358 | Type: event.Type, |
| 359 | Dimensions: dimensions, |
| 360 | Measures: maps.Clone(event.Measures), |
| 361 | } |
| 362 | } |
| 363 | s.mu.Unlock() |
| 364 | |
| 365 | s.flush(payload) |
| 366 | } |
| 367 | |
| 368 | type pendingEvent struct { |
| 369 | service *service |