Class: Google::Cloud::Logging::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/logging/service.rb

Overview

methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials, host: nil, retries: nil, timeout: nil) ⇒ Service

Creates a new Service instance.



33
34
35
36
37
38
39
40
# File 'lib/google/cloud/logging/service.rb', line 33

def initialize project, credentials, host: nil, retries: nil,
               timeout: nil
  @project = project
  @credentials = credentials
  @host = host || "logging.googleapis.com"
  @retries = retries
  @timeout = timeout
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



29
30
31
# File 'lib/google/cloud/logging/service.rb', line 29

def credentials
  @credentials
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/google/cloud/logging/service.rb', line 29

def host
  @host
end

#mocked_loggingObject

Returns the value of attribute mocked_logging.



56
57
58
# File 'lib/google/cloud/logging/service.rb', line 56

def mocked_logging
  @mocked_logging
end

#mocked_metricsObject

Returns the value of attribute mocked_metrics.



78
79
80
# File 'lib/google/cloud/logging/service.rb', line 78

def mocked_metrics
  @mocked_metrics
end

#mocked_sinksObject

Returns the value of attribute mocked_sinks.



67
68
69
# File 'lib/google/cloud/logging/service.rb', line 67

def mocked_sinks
  @mocked_sinks
end

#projectObject

Returns the value of attribute project.



29
30
31
# File 'lib/google/cloud/logging/service.rb', line 29

def project
  @project
end

#retriesObject

Returns the value of attribute retries.



29
30
31
# File 'lib/google/cloud/logging/service.rb', line 29

def retries
  @retries
end

#timeoutObject

Returns the value of attribute timeout.



29
30
31
# File 'lib/google/cloud/logging/service.rb', line 29

def timeout
  @timeout
end

Instance Method Details

#create_metric(name, filter, description) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/google/cloud/logging/service.rb', line 199

def create_metric name, filter, description
  metric_params = {
    name: name,
    description: description,
    filter: filter
  }.delete_if { |_, v| v.nil? }

  create_req = Google::Logging::V2::CreateLogMetricRequest.new(
    parent: project_path,
    metric: Google::Logging::V2::LogMetric.new(metric_params)
  )

  execute { metrics.create_log_metric create_req }
end

#create_sink(name, destination, filter, version) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/google/cloud/logging/service.rb', line 144

def create_sink name, destination, filter, version
  sink_params = {
    name: name, destination: destination,
    filter: filter, output_version_format: version
  }.delete_if { |_, v| v.nil? }

  create_req = Google::Logging::V2::CreateSinkRequest.new(
    parent: project_path,
    sink: Google::Logging::V2::LogSink.new(sink_params)
  )

  execute { sinks.create_sink create_req }
end

#credsObject



42
43
44
45
# File 'lib/google/cloud/logging/service.rb', line 42

def creds
  GRPC::Core::ChannelCredentials.new.compose \
    GRPC::Core::CallCredentials.new credentials.client.updater_proc
end

#delete_log(name) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/google/cloud/logging/service.rb', line 113

def delete_log name
  delete_req = Google::Logging::V2::DeleteLogRequest.new(
    log_name: log_path(name)
  )

  execute { logging.delete_log delete_req }
end

#delete_metric(name) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/google/cloud/logging/service.rb', line 237

def delete_metric name
  delete_req = Google::Logging::V2::DeleteLogMetricRequest.new(
    metric_name: metric_path(name)
  )

  execute { metrics.delete_log_metric delete_req }
end

#delete_sink(name) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/google/cloud/logging/service.rb', line 180

def delete_sink name
  delete_req = Google::Logging::V2::DeleteSinkRequest.new(
    sink_name: sink_path(name)
  )

  execute { sinks.delete_sink delete_req }
end

#get_metric(name) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/google/cloud/logging/service.rb', line 214

def get_metric name
  get_req = Google::Logging::V2::GetLogMetricRequest.new(
    metric_name: metric_path(name)
  )

  execute { metrics.get_log_metric get_req }
end

#get_sink(name) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/google/cloud/logging/service.rb', line 158

def get_sink name
  get_req = Google::Logging::V2::GetSinkRequest.new(
    sink_name: sink_path(name)
  )

  execute { sinks.get_sink get_req }
end

#inspectObject



245
246
247
# File 'lib/google/cloud/logging/service.rb', line 245

def inspect
  "#{self.class}(#{@project})"
end

#list_entries(projects: nil, filter: nil, order: nil, token: nil, max: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/google/cloud/logging/service.rb', line 80

def list_entries projects: nil, filter: nil, order: nil, token: nil,
                 max: nil
  list_params = { project_ids: Array(projects || @project),
                  filter: filter,
                  order_by: order,
                  page_token: token,
                  page_size: max
                }.delete_if { |_, v| v.nil? }

  list_req = Google::Logging::V2::ListLogEntriesRequest.new(list_params)

  execute { logging.list_log_entries list_req }
end

#list_metrics(token: nil, max: nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/google/cloud/logging/service.rb', line 188

def list_metrics token: nil, max: nil
  list_params = { parent: project_path,
                  page_token: token,
                  page_size: max
                }.delete_if { |_, v| v.nil? }

  list_req = Google::Logging::V2::ListLogMetricsRequest.new(list_params)

  execute { metrics.list_log_metrics list_req }
end

#list_resource_descriptors(token: nil, max: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/google/cloud/logging/service.rb', line 121

def list_resource_descriptors token: nil, max: nil
  list_params = { page_token: token,
                  page_size: max
                }.delete_if { |_, v| v.nil? }

  list_req = \
    Google::Logging::V2::ListMonitoredResourceDescriptorsRequest.new(
      list_params)

  execute { logging.list_monitored_resource_descriptors list_req }
end

#list_sinks(token: nil, max: nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/google/cloud/logging/service.rb', line 133

def list_sinks token: nil, max: nil
  list_params = { parent: project_path,
                  page_token: token,
                  page_size: max
                }.delete_if { |_, v| v.nil? }

  list_req = Google::Logging::V2::ListSinksRequest.new(list_params)

  execute { sinks.list_sinks list_req }
end

#loggingObject



47
48
49
50
51
52
53
54
55
# File 'lib/google/cloud/logging/service.rb', line 47

def logging
  return mocked_logging if mocked_logging
  @logging ||= begin
    require "google/logging/v2/logging_services_pb"

    Google::Logging::V2::LoggingServiceV2::Stub.new(
      host, creds, timeout: timeout)
  end
end

#metricsObject



69
70
71
72
73
74
75
76
77
# File 'lib/google/cloud/logging/service.rb', line 69

def metrics
  return mocked_metrics if mocked_metrics
  @metrics ||= begin
    require "google/logging/v2/logging_metrics_services_pb"

    Google::Logging::V2::MetricsServiceV2::Stub.new(
      host, creds, timeout: timeout)
  end
end

#sinksObject



58
59
60
61
62
63
64
65
66
# File 'lib/google/cloud/logging/service.rb', line 58

def sinks
  return mocked_sinks if mocked_sinks
  @sinks ||= begin
    require "google/logging/v2/logging_config_services_pb"

    Google::Logging::V2::ConfigServiceV2::Stub.new(
      host, creds, timeout: timeout)
  end
end

#update_metric(name, description, filter) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/google/cloud/logging/service.rb', line 222

def update_metric name, description, filter
  metric_params = {
    name: name,
    description: description,
    filter: filter
  }.delete_if { |_, v| v.nil? }

  update_req = Google::Logging::V2::UpdateLogMetricRequest.new(
    metric_name: metric_path(name),
    metric: Google::Logging::V2::LogMetric.new(metric_params)
  )

  execute { metrics.update_log_metric update_req }
end

#update_sink(name, destination, filter, version) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/google/cloud/logging/service.rb', line 166

def update_sink name, destination, filter, version
  sink_params = {
    name: name, destination: destination,
    filter: filter, output_version_format: version
  }.delete_if { |_, v| v.nil? }

  update_req = Google::Logging::V2::UpdateSinkRequest.new(
    sink_name: sink_path(name),
    sink: Google::Logging::V2::LogSink.new(sink_params)
  )

  execute { sinks.update_sink update_req }
end

#write_entries(entries, log_name: nil, resource: nil, labels: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/google/cloud/logging/service.rb', line 94

def write_entries entries, log_name: nil, resource: nil, labels: nil
  # Fix log names so they are the full path
  entries = Array(entries).each do |entry|
    entry.log_name = log_path(entry.log_name)
  end
  resource = resource.to_grpc if resource
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels

  write_params = { entries: entries,
                   log_name: log_path(log_name),
                   resource: resource, labels: labels
                 }.delete_if { |_, v| v.nil? }

  write_req = Google::Logging::V2::WriteLogEntriesRequest.new(
    write_params)

  execute { logging.write_log_entries write_req }
end