gitlab-labkit 0.25.0 → 0.27.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1966ecde160ebd4af3cf1e131cdc2f5e7665e327bc0074128d293f4b33710c8
4
- data.tar.gz: 0abde88973ac84a12862be74f5a438ae5a76852d805dc690852f121eb0466cce
3
+ metadata.gz: 79846c3feb1b3287ef00d0f55b81ee788745bd5e9834849686061d3ec533cd8a
4
+ data.tar.gz: 527270e43d3a430155932fa3fd133be491ae572196f830e1f3464ff3fe0d4144
5
5
  SHA512:
6
- metadata.gz: 7a18b78e676c89304be5db049ea675fe2b8671d0f4eabfec557340328821dc14f56a9b4a6e80a86fa62c72a657e97dd5f667e63a5da2eedb419114ce68480eeb
7
- data.tar.gz: 2aeb4a0f9b5cd31e89d169654992977bbab625539da4511de6e989ec8dfae31693a4180e0def980437cdc89b258d18ea577d5f9ca70b06797e7080ea4b7d5e08
6
+ metadata.gz: bdf9d4b68fa887ddaf7763a21637cdafe0937dd6f6aef355c71178e8f52c960b7726f9060364c4d254e75e83014764baddfa8545a27818b92fb0409fb422dabd
7
+ data.tar.gz: 1926fbefa4446482eb4a9ed7af240eecb2c6b5af6a8e6cc444d67ca7c95bb252853729b771bf29984437e0843396bfac9e188031a29f29913cf61d9f97f6a183
data/.env.example.sh ADDED
@@ -0,0 +1,7 @@
1
+ export CI_PROJECT_ID=10947578
2
+ export CI_API_V4_URL="https://gitlab.com/api/v4"
3
+ export CI_PROJECT_URL="https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby"
4
+ # Don't keep secrets in plaintext files. Use a keyring or 1password to load
5
+ # it instead and export it as an env var.
6
+ token=$(load-your-token)
7
+ export GITLAB_TOKEN=$token
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ Gemfile.lock
2
2
  *.gem
3
3
  node_modules
4
4
  .bundle
5
+ /.env.sh
data/.gitlab-ci.yml CHANGED
@@ -20,16 +20,11 @@ workflow:
20
20
  test:3.0:
21
21
  image: ruby:3.0
22
22
  <<: *test_definition
23
- allow_failure: true # until we resolve grpc compatibility, see https://gitlab.com/gitlab-org/labkit-ruby/-/merge_requests/81
24
23
 
25
24
  test:2.7:
26
25
  image: ruby:2.7
27
26
  <<: *test_definition
28
27
 
29
- test:2.6:
30
- image: ruby:2.6
31
- <<: *test_definition
32
-
33
28
  static-analysis:
34
29
  before_script:
35
30
  - bundle install
data/README.md CHANGED
@@ -44,6 +44,20 @@ Note that LabKit-Ruby uses the [`rufo`](https://github.com/ruby-formatter/rufo)
44
44
 
45
45
  Please also review the [development section of the LabKit (go) README](https://gitlab.com/gitlab-org/labkit#developing-labkit) for details of the LabKit architectural philosophy.
46
46
 
47
+ To work on some of the scripts we use for releasing a new version,
48
+ make sure to add a new `.env.sh`.
49
+
50
+ ```console
51
+ cp .env.example.sh .env.sh`
52
+ ```
53
+
54
+ Inside `.env.sh`, add a personal acccess token for the `GITLAB_TOKEN`
55
+ environment variable. Next source the file:
56
+
57
+ ```console
58
+ . .env.sh
59
+ ```
60
+
47
61
  ### Releasing a new version
48
62
 
49
63
  Releasing a new version can be done by pushing a new tag, or creating
@@ -123,7 +123,7 @@ module Labkit
123
123
  data.merge!(attributes)
124
124
 
125
125
  # Remove keys that had their values set to `nil` in the new attributes
126
- data.keep_if { |_, value| value.present? }
126
+ data.keep_if { |_, value| valid_data?(value) }
127
127
 
128
128
  # Assign a correlation if it was missing in the first context or when
129
129
  # explicitly removed
@@ -146,12 +146,16 @@ module Labkit
146
146
  data.transform_values do |value|
147
147
  value = call_or_value(value)
148
148
 
149
- value.presence
149
+ value if valid_data?(value)
150
150
  end.compact
151
151
  end
152
152
 
153
153
  def new_id
154
154
  SecureRandom.hex
155
155
  end
156
+
157
+ def valid_data?(value)
158
+ value == false || value.present?
159
+ end
156
160
  end
157
161
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ require "time"
3
+ require "logger"
4
+ require "json"
5
+
6
+ module Labkit
7
+ module Logging
8
+ class JsonLogger < ::Logger
9
+ def self.log_level(fallback: ::Logger::DEBUG)
10
+ ENV.fetch("GITLAB_LOG_LEVEL", fallback)
11
+ end
12
+
13
+ def initialize(path, level: JsonLogger.log_level)
14
+ super
15
+ end
16
+
17
+ def format_message(severity, timestamp, progname, message)
18
+ data = default_attributes
19
+ data[:severity] = severity
20
+ data[:time] = timestamp.utc.iso8601(3)
21
+ data[Labkit::Correlation::CorrelationId::LOG_KEY] = Labkit::Correlation::CorrelationId.current_id
22
+
23
+ case message
24
+ when String
25
+ data[:message] = message
26
+ when Hash
27
+ data.merge!(message)
28
+ end
29
+
30
+ dump_json(data) << "\n"
31
+ end
32
+
33
+ private
34
+
35
+ def default_attributes
36
+ {}
37
+ end
38
+
39
+ def dump_json(data)
40
+ JSON.generate(data)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -6,5 +6,6 @@ module Labkit
6
6
  module Logging
7
7
  autoload :GRPC, "labkit/logging/grpc"
8
8
  autoload :Sanitizer, "labkit/logging/sanitizer"
9
+ autoload :JsonLogger, "labkit/logging/json_logger"
9
10
  end
10
11
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support"
4
+ require "active_support/core_ext"
5
+
3
6
  require "jaeger/client"
4
7
 
5
8
  module Labkit
@@ -22,10 +25,13 @@ module Labkit
22
25
  # The service_name parameter from GITLAB_TRACING takes precedence over the application one
23
26
  service_name = options[:service_name] if options[:service_name]
24
27
 
28
+ # parse reporter headers as necessary
29
+ headers = build_headers(options)
30
+
25
31
  kwargs = {
26
32
  service_name: service_name,
27
33
  sampler: get_sampler(options[:sampler], options[:sampler_param]),
28
- reporter: get_reporter(service_name, options[:http_endpoint], options[:udp_endpoint]),
34
+ reporter: get_reporter(service_name, options[:http_endpoint], options[:udp_endpoint], headers),
29
35
  }.compact
30
36
 
31
37
  extra_params = options.except(:sampler, :sampler_param, :http_endpoint, :udp_endpoint, :strict_parsing, :debug)
@@ -40,6 +46,24 @@ module Labkit
40
46
  Jaeger::Client.build(**kwargs)
41
47
  end
42
48
 
49
+ def self.build_headers(options)
50
+ return unless options&.key?(:http_endpoint)
51
+
52
+ http_endpoint = options[:http_endpoint]
53
+ parsed = URI.parse(http_endpoint)
54
+
55
+ headers = {}
56
+ # add basic auth header only when both user and password are setup correctly
57
+ user = parsed.user
58
+ password = parsed.password
59
+ if user.present? && password.present?
60
+ headers["Authorization"] = "Basic " + Base64.strict_encode64("#{user}:#{password}")
61
+ end
62
+
63
+ return headers
64
+ end
65
+ private_class_method :build_headers
66
+
43
67
  def self.get_sampler(sampler_type, sampler_param)
44
68
  case sampler_type
45
69
  when "probabilistic"
@@ -52,11 +76,11 @@ module Labkit
52
76
  end
53
77
  private_class_method :get_sampler
54
78
 
55
- def self.get_reporter(service_name, http_endpoint, udp_endpoint)
79
+ def self.get_reporter(service_name, http_endpoint, udp_endpoint, headers)
56
80
  encoder = Jaeger::Encoders::ThriftEncoder.new(service_name: service_name)
57
81
 
58
82
  if http_endpoint.present?
59
- sender = get_http_sender(encoder, http_endpoint)
83
+ sender = get_http_sender(encoder, http_endpoint, headers)
60
84
  elsif udp_endpoint.present?
61
85
  sender = get_udp_sender(encoder, udp_endpoint)
62
86
  else
@@ -67,8 +91,8 @@ module Labkit
67
91
  end
68
92
  private_class_method :get_reporter
69
93
 
70
- def self.get_http_sender(encoder, address)
71
- Jaeger::HttpSender.new(url: address, encoder: encoder, logger: Logger.new(STDOUT))
94
+ def self.get_http_sender(encoder, address, headers)
95
+ Jaeger::HttpSender.new(url: address, headers: headers, encoder: encoder, logger: Logger.new(STDOUT))
72
96
  end
73
97
  private_class_method :get_http_sender
74
98
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-labkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.0
4
+ version: 0.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Newdigate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-30 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -357,6 +357,7 @@ executables: []
357
357
  extensions: []
358
358
  extra_rdoc_files: []
359
359
  files:
360
+ - ".env.example.sh"
360
361
  - ".gitignore"
361
362
  - ".gitlab-ci.yml"
362
363
  - ".gitlab/CODEOWNERS"
@@ -386,6 +387,7 @@ files:
386
387
  - lib/labkit/logging.rb
387
388
  - lib/labkit/logging/grpc.rb
388
389
  - lib/labkit/logging/grpc/server_interceptor.rb
390
+ - lib/labkit/logging/json_logger.rb
389
391
  - lib/labkit/logging/sanitizer.rb
390
392
  - lib/labkit/middleware.rb
391
393
  - lib/labkit/middleware/rack.rb