Commit 020fc91b authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Add dsn_url method to client key

Generate URL for sentry clients based on public key and project id from
client key
Signed-off-by: default avatarDmytro Zaporozhets <dzaporozhets@gitlab.com>
parent 33a83066
......@@ -14,6 +14,10 @@ class ErrorTracking::ClientKey < ApplicationRecord
find_by(public_key: key)
end
def sentry_dsn
@sentry_dsn ||= ErrorTracking::Collector::Dsn.build_url(public_key, project_id)
end
private
def generate_key
......
# frozen_string_literal: true
module ErrorTracking
module Collector
class Dsn
# Build a sentry compatible DSN URL for GitLab collector.
#
# The expected URL looks like that:
# https://PUBLIC_KEY@gitlab.example.com/api/v4/error_tracking/collector/PROJECT_ID
#
def self.build_url(public_key, project_id)
gitlab = Settings.gitlab
custom_port = Settings.gitlab_on_standard_port? ? nil : ":#{gitlab.port}"
base_url = [
gitlab.protocol,
"://",
public_key,
'@',
gitlab.host,
custom_port,
gitlab.relative_url_root
].join('')
"#{base_url}/api/v4/error_tracking/collector/#{project_id}"
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ErrorTracking::Collector::Dsn do
describe '.build__url' do
let(:gitlab) do
double(
protocol: 'https',
https: true,
host: 'gitlab.example.com',
port: '4567',
relative_url_root: nil
)
end
subject { described_class.build_url('abcdef1234567890', 778) }
it 'returns a valid URL' do
allow(Settings).to receive(:gitlab).and_return(gitlab)
allow(Settings).to receive(:gitlab_on_standard_port?).and_return(false)
is_expected.to eq('https://abcdef1234567890@gitlab.example.com:4567/api/v4/error_tracking/collector/778')
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment