aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/opentelemetry/metric-helpers/stats-observers-builder.ts
blob: 90b58f33dff3328d61922311d2a1f381c5a71234 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import memoizee from 'memoizee'
import { Meter } from '@opentelemetry/api-metrics'
import { MEMOIZE_TTL } from '@server/initializers/constants'
import { buildAvailableActivities } from '@server/lib/activitypub/activity'
import { StatsManager } from '@server/lib/stat-manager'

export class StatsObserverBuilder {

  private readonly getInstanceStats = memoizee(() => {
    return StatsManager.Instance.getStats()
  }, { maxAge: MEMOIZE_TTL.GET_STATS_FOR_OPEN_TELEMETRY_METRICS })

  constructor (private readonly meter: Meter) {

  }

  buildObservers () {
    this.buildUserStatsObserver()
    this.buildVideoStatsObserver()
    this.buildCommentStatsObserver()
    this.buildPlaylistStatsObserver()
    this.buildChannelStatsObserver()
    this.buildInstanceFollowsStatsObserver()
    this.buildRedundancyStatsObserver()
    this.buildActivityPubStatsObserver()
  }

  private buildUserStatsObserver () {
    this.meter.createObservableGauge('peertube_users_total', {
      description: 'Total users on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalUsers)
    })

    this.meter.createObservableGauge('peertube_active_users_total', {
      description: 'Total active users on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalDailyActiveUsers, { activeInterval: 'daily' })
      observableResult.observe(stats.totalWeeklyActiveUsers, { activeInterval: 'weekly' })
      observableResult.observe(stats.totalMonthlyActiveUsers, { activeInterval: 'monthly' })
    })
  }

  private buildChannelStatsObserver () {
    this.meter.createObservableGauge('peertube_channels_total', {
      description: 'Total channels on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalVideoChannels, { channelOrigin: 'local' })
    })

    this.meter.createObservableGauge('peertube_active_channels_total', {
      description: 'Total active channels on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalDailyActiveVideoChannels, { channelOrigin: 'local', activeInterval: 'daily' })
      observableResult.observe(stats.totalLocalWeeklyActiveVideoChannels, { channelOrigin: 'local', activeInterval: 'weekly' })
      observableResult.observe(stats.totalLocalMonthlyActiveVideoChannels, { channelOrigin: 'local', activeInterval: 'monthly' })
    })
  }

  private buildVideoStatsObserver () {
    this.meter.createObservableGauge('peertube_videos_total', {
      description: 'Total videos on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalVideos, { videoOrigin: 'local' })
      observableResult.observe(stats.totalVideos - stats.totalLocalVideos, { videoOrigin: 'remote' })
    })

    this.meter.createObservableGauge('peertube_video_views_total', {
      description: 'Total video views made on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalVideoViews, { viewOrigin: 'local' })
    })

    this.meter.createObservableGauge('peertube_video_bytes_total', {
      description: 'Total bytes of videos'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalVideoFilesSize, { videoOrigin: 'local' })
    })
  }

  private buildCommentStatsObserver () {
    this.meter.createObservableGauge('peertube_comments_total', {
      description: 'Total comments on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalVideoComments, { accountOrigin: 'local' })
    })
  }

  private buildPlaylistStatsObserver () {
    this.meter.createObservableGauge('peertube_playlists_total', {
      description: 'Total playlists on the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalLocalPlaylists, { playlistOrigin: 'local' })
    })
  }

  private buildInstanceFollowsStatsObserver () {
    this.meter.createObservableGauge('peertube_instance_followers_total', {
      description: 'Total followers of the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalInstanceFollowers)
    })

    this.meter.createObservableGauge('peertube_instance_following_total', {
      description: 'Total following of the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalInstanceFollowing)
    })
  }

  private buildRedundancyStatsObserver () {
    this.meter.createObservableGauge('peertube_redundancy_used_bytes_total', {
      description: 'Total redundancy used of the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      for (const r of stats.videosRedundancy) {
        observableResult.observe(r.totalUsed, { strategy: r.strategy })
      }
    })

    this.meter.createObservableGauge('peertube_redundancy_available_bytes_total', {
      description: 'Total redundancy available of the instance'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      for (const r of stats.videosRedundancy) {
        observableResult.observe(r.totalSize, { strategy: r.strategy })
      }
    })
  }

  private buildActivityPubStatsObserver () {
    const availableActivities = buildAvailableActivities()

    this.meter.createObservableGauge('peertube_ap_inbox_success_total', {
      description: 'Total inbox messages processed with success'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      for (const type of availableActivities) {
        observableResult.observe(stats[`totalActivityPub${type}MessagesSuccesses`], { activityType: type })
      }
    })

    this.meter.createObservableGauge('peertube_ap_inbox_error_total', {
      description: 'Total inbox messages processed with error'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      for (const type of availableActivities) {
        observableResult.observe(stats[`totalActivityPub${type}MessagesErrors`], { activityType: type })
      }
    })

    this.meter.createObservableGauge('peertube_ap_inbox_waiting_total', {
      description: 'Total inbox messages waiting for being processed'
    }).addCallback(async observableResult => {
      const stats = await this.getInstanceStats()

      observableResult.observe(stats.totalActivityPubMessagesWaiting)
    })
  }
}