Class: Logtail::Contexts::Release

Inherits:
Logtail::Context show all
Defined in:
lib/logtail/contexts/release.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Release

Returns a new instance of Release.



34
35
36
37
38
# File 'lib/logtail/contexts/release.rb', line 34

def initialize(attributes)
  @commit_hash = attributes[:commit_hash]
  @created_at = attributes[:created_at]
  @version = attributes[:version]
end

Instance Attribute Details

#commit_hashObject (readonly)

Returns the value of attribute commit_hash.



32
33
34
# File 'lib/logtail/contexts/release.rb', line 32

def commit_hash
  @commit_hash
end

#created_atObject (readonly)

Returns the value of attribute created_at.



32
33
34
# File 'lib/logtail/contexts/release.rb', line 32

def created_at
  @created_at
end

#versionObject (readonly)

Returns the value of attribute version.



32
33
34
# File 'lib/logtail/contexts/release.rb', line 32

def version
  @version
end

Class Method Details

.from_envObject

Builds a release context based on environment variables. Simply add the ‘RELEASE_COMMIT`, `RELEASE_CREATED_AT`, or the `RELEASE_VERSION` env vars to get this context automatially. All are optional, but at least one must be present.

If you’re on Heroku, simply enable dyno metadata to get this automatically: devcenter.heroku.com/articles/dyno-metadata



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/logtail/contexts/release.rb', line 17

def from_env
  commit_hash = ENV['RELEASE_COMMIT'] || ENV['HEROKU_SLUG_COMMIT']
  created_at = ENV['RELEASE_CREATED_AT'] || ENV['HEROKU_RELEASE_CREATED_AT']
  version = ENV['RELEASE_VERSION'] || ENV['HEROKU_RELEASE_VERSION']

  if commit_hash || created_at || version
    Logtail::Config.instance.debug { "Release env vars detected, adding to context" }
    new(commit_hash: commit_hash, created_at: created_at, version: version)
  else
    Logtail::Config.instance.debug { "Release env vars _not_ detected" }
    nil
  end
end

Instance Method Details

#to_hashObject

Builds a hash representation containing simple objects, suitable for serialization (JSON).



41
42
43
44
45
46
47
48
49
# File 'lib/logtail/contexts/release.rb', line 41

def to_hash
  @to_hash ||= {
    release: Util::NonNilHashBuilder.build do |h|
      h.add(:commit_hash, commit_hash)
      h.add(:created_at, created_at)
      h.add(:version, version)
    end
  }
end