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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/google_cloud/logging_service/logger.rb')
-rw-r--r--lib/google_cloud/logging_service/logger.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/google_cloud/logging_service/logger.rb b/lib/google_cloud/logging_service/logger.rb
new file mode 100644
index 00000000000..2c6dd6ea732
--- /dev/null
+++ b/lib/google_cloud/logging_service/logger.rb
@@ -0,0 +1,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