Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logger.rb « logging_service « google_cloud « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c6dd6ea73260e30121ba03e86f9706335b15e1b (plain)
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
# frozen_string_literal: true

module GoogleCloud
  module LoggingService
    class Logger
      WRITE_URL = "https://logging.googleapis.com/v2/entries:write"
      SCOPE = "https://www.googleapis.com/auth/logging.write"

      def initialize
        @auth = GoogleCloud::Authentication.new(scope: SCOPE)
      end

      def log(client_email, private_key, payload)
        access_token = @auth.generate_access_token(client_email, private_key)

        return unless access_token

        headers = build_headers(access_token)

        post(WRITE_URL, body: payload, headers: headers)
      end

      private

      def build_headers(access_token)
        { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' }
      end

      def post(url, body:, headers:)
        Gitlab::HTTP.post(
          url,
          body: body,
          headers: headers
        )
      rescue URI::InvalidURIError => e
        Gitlab::ErrorTracking.log_exception(e)
      rescue *Gitlab::HTTP::HTTP_ERRORS
      end
    end
  end
end