mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-03 12:37:56 +00:00
chore: Add Telemetry (#2631)
ref: https://www.chatwoot.com/docs/self-hosted/telemetry
This commit is contained in:
@@ -27,7 +27,13 @@ class Installation::OnboardingController < ApplicationController
|
|||||||
|
|
||||||
def finish_onboarding
|
def finish_onboarding
|
||||||
::Redis::Alfred.delete(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING)
|
::Redis::Alfred.delete(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING)
|
||||||
ChatwootHub.register_instance(onboarding_params) if onboarding_params[:subscribe_to_updates]
|
return if onboarding_params[:subscribe_to_updates].blank?
|
||||||
|
|
||||||
|
ChatwootHub.register_instance(
|
||||||
|
onboarding_params.dig(:user, :company),
|
||||||
|
onboarding_params.dig(:user, :name),
|
||||||
|
onboarding_params.dig(:user, :email)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_installation_onboarding
|
def ensure_installation_onboarding
|
||||||
|
|||||||
@@ -1,16 +1,40 @@
|
|||||||
class ChatwootHub
|
class ChatwootHub
|
||||||
BASE_URL = ENV['CHATWOOT_HUB_URL'] || 'https://hub.chatwoot.com'
|
BASE_URL = ENV['CHATWOOT_HUB_URL'] || 'https://hub.2.chatwoot.com'
|
||||||
|
PING_URL = "#{BASE_URL}/ping".freeze
|
||||||
|
REGISTRATION_URL = "#{BASE_URL}/instances".freeze
|
||||||
|
EVENTS_URL = "#{BASE_URL}/events".freeze
|
||||||
|
|
||||||
|
def self.installation_identifier
|
||||||
|
identifier = InstallationConfig.find_by(name: 'INSTALLATION_IDENTIFIER')&.value
|
||||||
|
identifier ||= InstallationConfig.create(name: 'INSTALLATION_IDENTIFIER', value: SecureRandom.uuid).value
|
||||||
|
identifier
|
||||||
|
end
|
||||||
|
|
||||||
def self.instance_config
|
def self.instance_config
|
||||||
{
|
{
|
||||||
installationVersion: Chatwoot.config[:version],
|
installation_identifier: installation_identifier,
|
||||||
installationHost: URI.parse(ENV.fetch('FRONTEND_URL', '')).host
|
installation_version: Chatwoot.config[:version],
|
||||||
|
installation_host: URI.parse(ENV.fetch('FRONTEND_URL', '')).host
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.instance_metrics
|
||||||
|
{
|
||||||
|
accounts_count: Account.count,
|
||||||
|
users_count: User.count,
|
||||||
|
inboxes_count: Inbox.count,
|
||||||
|
conversations_count: Conversation.count,
|
||||||
|
incoming_messages_count: Message.incoming.count,
|
||||||
|
outgoing_messages_count: Message.outgoing.count,
|
||||||
|
additional_information: {}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.latest_version
|
def self.latest_version
|
||||||
begin
|
begin
|
||||||
response = RestClient.get(BASE_URL, { params: instance_config })
|
info = instance_config
|
||||||
|
info = info.merge(instance_metrics) unless ENV['DISABLE_TELEMETRY']
|
||||||
|
response = RestClient.post(PING_URL, info.to_json, { content_type: :json, accept: :json })
|
||||||
version = JSON.parse(response)['version']
|
version = JSON.parse(response)['version']
|
||||||
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e
|
||||||
Rails.logger.info "Exception: #{e.message}"
|
Rails.logger.info "Exception: #{e.message}"
|
||||||
@@ -20,8 +44,20 @@ class ChatwootHub
|
|||||||
version
|
version
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.register_instance(info)
|
def self.register_instance(company_name, owner_name, owner_email)
|
||||||
RestClient.post("#{BASE_URL}/register_instance", info.merge(instance_config).to_json, { content_type: :json, accept: :json })
|
info = { company_name: company_name, owner_name: owner_name, owner_email: owner_email, subscribed_to_mailers: true }
|
||||||
|
RestClient.post(REGISTRATION_URL, info.merge(instance_config).to_json, { content_type: :json, accept: :json })
|
||||||
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e
|
||||||
|
Rails.logger.info "Exception: #{e.message}"
|
||||||
|
rescue StandardError => e
|
||||||
|
Raven.capture_exception(e)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.emit_event(event_name, event_data)
|
||||||
|
return if ENV['DISABLE_TELEMETRY']
|
||||||
|
|
||||||
|
info = { event_name: event_name, event_data: event_data }
|
||||||
|
RestClient.post(EVENTS_URL, info.merge(instance_config).to_json, { content_type: :json, accept: :json })
|
||||||
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e
|
||||||
Rails.logger.info "Exception: #{e.message}"
|
Rails.logger.info "Exception: #{e.message}"
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
|
|||||||
@@ -1,15 +1,71 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe ChatwootHub do
|
describe ChatwootHub do
|
||||||
|
it 'generates installation identifier' do
|
||||||
|
installation_identifier = described_class.installation_identifier
|
||||||
|
expect(installation_identifier).not_to eq nil
|
||||||
|
expect(described_class.installation_identifier).to eq installation_identifier
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when fetching latest_version' do
|
||||||
it 'get latest version from chatwoot hub' do
|
it 'get latest version from chatwoot hub' do
|
||||||
version = '1.1.1'
|
version = '1.1.1'
|
||||||
allow(RestClient).to receive(:get).and_return({ version: version }.to_json)
|
allow(RestClient).to receive(:post).and_return({ version: version }.to_json)
|
||||||
expect(described_class.latest_version).to eq version
|
expect(described_class.latest_version).to eq version
|
||||||
expect(RestClient).to have_received(:get).with(described_class::BASE_URL, { params: described_class.instance_config })
|
expect(RestClient).to have_received(:post).with(described_class::PING_URL, described_class.instance_config
|
||||||
|
.merge(described_class.instance_metrics).to_json, { content_type: :json, accept: :json })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not send instance metrics when telemetry is disabled' do
|
||||||
|
version = '1.1.1'
|
||||||
|
ENV['DISABLE_TELEMETRY'] = 'true'
|
||||||
|
allow(RestClient).to receive(:post).and_return({ version: version }.to_json)
|
||||||
|
expect(described_class.latest_version).to eq version
|
||||||
|
expect(RestClient).to have_received(:post).with(described_class::PING_URL,
|
||||||
|
described_class.instance_config.to_json, { content_type: :json, accept: :json })
|
||||||
|
ENV['DISABLE_TELEMETRY'] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns nil when chatwoot hub is down' do
|
it 'returns nil when chatwoot hub is down' do
|
||||||
allow(RestClient).to receive(:get).and_raise(ExceptionList::REST_CLIENT_EXCEPTIONS.sample)
|
allow(RestClient).to receive(:post).and_raise(ExceptionList::REST_CLIENT_EXCEPTIONS.sample)
|
||||||
expect(described_class.latest_version).to eq nil
|
expect(described_class.latest_version).to eq nil
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when register instance' do
|
||||||
|
let(:company_name) { 'test' }
|
||||||
|
let(:owner_name) { 'test' }
|
||||||
|
let(:owner_email) { 'test@test.com' }
|
||||||
|
|
||||||
|
it 'sends info of registration' do
|
||||||
|
info = { company_name: company_name, owner_name: owner_name, owner_email: owner_email, subscribed_to_mailers: true }
|
||||||
|
allow(RestClient).to receive(:post)
|
||||||
|
described_class.register_instance(company_name, owner_name, owner_email)
|
||||||
|
expect(RestClient).to have_received(:post).with(described_class::REGISTRATION_URL,
|
||||||
|
info.merge(described_class.instance_config).to_json, { content_type: :json, accept: :json })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when sending events' do
|
||||||
|
let(:event_name) { 'sample_event' }
|
||||||
|
let(:event_data) { { 'sample_data' => 'sample_data' } }
|
||||||
|
|
||||||
|
it 'will send instance events' do
|
||||||
|
info = { event_name: event_name, event_data: event_data }
|
||||||
|
allow(RestClient).to receive(:post)
|
||||||
|
described_class.emit_event(event_name, event_data)
|
||||||
|
expect(RestClient).to have_received(:post).with(described_class::EVENTS_URL,
|
||||||
|
info.merge(described_class.instance_config).to_json, { content_type: :json, accept: :json })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not send instance events when telemetry is disabled' do
|
||||||
|
ENV['DISABLE_TELEMETRY'] = 'true'
|
||||||
|
info = { event_name: event_name, event_data: event_data }
|
||||||
|
allow(RestClient).to receive(:post)
|
||||||
|
described_class.emit_event(event_name, event_data)
|
||||||
|
expect(RestClient).not_to have_received(:post).with(described_class::EVENTS_URL,
|
||||||
|
info.merge(described_class.instance_config).to_json, { content_type: :json, accept: :json })
|
||||||
|
ENV['DISABLE_TELEMETRY'] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user