Class: LogStash::Outputs::StackdriverLogging
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::StackdriverLogging
- Defined in:
- lib/logstash/outputs/stackdriver_logging.rb
Overview
Summary
This plugin sends events to Stackdriver Logging. Events are sent in batches as they are received.
Environment configuration
Logging should already be configured on your Google Cloud Platform project. However, you will need to create a service account with permissions to write logs.
Usage
This is an example of Logstash config for this plugin:
- source,ruby
output {
stackdriver_logging { project_id => "folkloric-guru-278" (optional) <1> key_file => "/path/to/key_file.json" (optional) <2> log_name => "" (required) <3> severity_field => "severity" (optional) => "@timestamp" (optional) default_severity => "default" (optional) strip_at_fields => false (optional) strip_fields => [] (optional) }}
<1> If running on Google Compute Engine, the project ID will be automatically retrieved from the metadata server. Required
if not running within GCE.<2> If no key is provided, defaults to using cloud.google.com/docs/authentication/production[Application Default Credentials].
<3> The log name can be interpolated with log data, to extract the log name from a message.
Considerations
There is a cost to storing log data in Stackdriver Logging. See cloud.google.com/stackdriver/pricing[the pricing guide] for more information on costs involved.
All logs are stored under the
globalresource.
Instance Method Summary collapse
Instance Method Details
#multi_receive(events) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/logstash/outputs/stackdriver_logging.rb', line 124 def multi_receive(events) # Do nothing if no events are received. if events.length < 1 return end entries = [] events.each do |event| entry = Google::Apis::LoggingV2::LogEntry.new entry.severity = event.include?(@severity_field) ? event.get(@severity_field) : @default_severity entry.log_name = "projects/%{project}/logs/%{log_name}" % { :project => @project_id, :log_name => event.sprintf(@log_name) } entry. = event.get() entry.json_payload = event.to_hash # Strip the "@" fields. if @strip_at_fields or @strip_fields.length > 0 filtered = {} entry.json_payload.each do |key, value| if @strip_at_fields && key[0] == "@" next end if @strip_fields.length > 0 && @strip_fields.include?(key) next end filtered[key] = value end entry.json_payload = filtered end entries.push entry end resource = Google::Apis::LoggingV2::MonitoredResource.new resource.type = "global" resource.labels = { :project_id => @project_id } request = Google::Apis::LoggingV2::WriteLogEntriesRequest.new(entries: entries) request.resource = resource @service.write_entry_log_entries(request) do |result, error| if error @logger.error "Unable to write log entries to Stackdriver Logging." @logger.error "Received this error: " + error.to_s else @logger.debug("Wrote %{length} entries successfully." % { :length => entries.length }) end end end |
#register ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/logstash/outputs/stackdriver_logging.rb', line 88 def register @service = Google::Apis::LoggingV2::LoggingService.new scope = %w(https://www.googleapis.com/auth/logging.write) # Always load key file if provided. if @key_file @service. = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: File.open(@key_file), scope: scope) # Fall back to getting the application default credentials. else @service. = Google::Auth.get_application_default(scope) end # project_id is not defined. Try to extract it from teh metadata server. unless @project_id if Google::Auth::GCECredentials.on_gce? connection = Faraday::Connection.new("http://169.254.169.254/computeMetadata/v1/") connection.headers = { "Metadata-Flavor": "Google" } response = connection.get "project/project-id" if response.status == 200 @project_id = response.body.to_s.strip end else @logger.error "Unable to detect the Google Cloud project ID to which logs should be written." \ "Please ensure that you specify the `project_id` config parameter if not running on the Google " \ "Cloud Platform." @logger.error "You will not be able to be able to write logs to Google Cloud until this is resolved." end end end |