Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

telemetry.ts 1.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  1. import chalk from 'chalk';
  2. import packageJson from '../package.json';
  3. import logger from './logger';
  4. import { fetchWithTimeout } from './fetch';
  5. import { maybeRecordFirstRun } from './util';
  6. type EventValue = string | number | boolean | string[];
  7. type TelemetryEvent = {
  8. event: string;
  9. packageVersion: string;
  10. properties: Record<string, EventValue>;
  11. };
  12. type TelemetryEventTypes = 'eval_ran' | 'assertion_used' | 'command_used';
  13. const TELEMETRY_ENDPOINT = 'https://api.promptfoo.dev/telemetry';
  14. const TELEMETRY_TIMEOUT_MS = 1000;
  15. export class Telemetry {
  16. private events: TelemetryEvent[] = [];
  17. get disabled() {
  18. return process.env.PROMPTFOO_DISABLE_TELEMETRY === '1';
  19. }
  20. record(eventName: TelemetryEventTypes, properties: Record<string, EventValue>): void {
  21. if (!this.disabled) {
  22. this.events.push({
  23. event: eventName,
  24. packageVersion: packageJson.version,
  25. properties,
  26. });
  27. }
  28. }
  29. maybeShowNotice(): void {
  30. if (!this.disabled && maybeRecordFirstRun()) {
  31. logger.info(
  32. chalk.gray(
  33. 'Anonymous telemetry is enabled. For more info, see https://www.promptfoo.dev/docs/configuration/telemetry',
  34. ),
  35. );
  36. }
  37. }
  38. async send(): Promise<void> {
  39. if (!this.disabled && this.events.length > 0) {
  40. try {
  41. const response = await fetchWithTimeout(
  42. TELEMETRY_ENDPOINT,
  43. {
  44. method: 'POST',
  45. headers: {
  46. 'Content-Type': 'application/json',
  47. },
  48. body: JSON.stringify(this.events),
  49. },
  50. TELEMETRY_TIMEOUT_MS,
  51. );
  52. if (response.ok) {
  53. this.events = [];
  54. }
  55. } catch (err) {}
  56. }
  57. }
  58. }
  59. const telemetry = new Telemetry();
  60. export default telemetry;
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...