Class: Gcloud::Logging::Service

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

Overview

methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials) ⇒ Service

Creates a new Service instance.



31
32
33
34
35
# File 'lib/gcloud/logging/service.rb', line 31

def initialize project, credentials
  @project = project
  @credentials = credentials
  @host = "logging.googleapis.com"
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



27
28
29
# File 'lib/gcloud/logging/service.rb', line 27

def credentials
  @credentials
end

#hostObject

Returns the value of attribute host.



27
28
29
# File 'lib/gcloud/logging/service.rb', line 27

def host
  @host
end

#mocked_loggingObject

Returns the value of attribute mocked_logging.



46
47
48
# File 'lib/gcloud/logging/service.rb', line 46

def mocked_logging
  @mocked_logging
end

#mocked_metricsObject

Returns the value of attribute mocked_metrics.



58
59
60
# File 'lib/gcloud/logging/service.rb', line 58

def mocked_metrics
  @mocked_metrics
end

#mocked_sinksObject

Returns the value of attribute mocked_sinks.



52
53
54
# File 'lib/gcloud/logging/service.rb', line 52

def mocked_sinks
  @mocked_sinks
end

#projectObject

Returns the value of attribute project.



27
28
29
# File 'lib/gcloud/logging/service.rb', line 27

def project
  @project
end

Instance Method Details

#create_metric(name, filter, description) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/gcloud/logging/service.rb', line 178

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(
    project_name: project_path,
    metric: Google::Logging::V2::LogMetric.new(metric_params)
  )

  backoff { metrics.create_log_metric create_req }
end

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



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gcloud/logging/service.rb', line 123

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(
    project_name: project_path,
    sink: Google::Logging::V2::LogSink.new(sink_params)
  )

  backoff { sinks.create_sink create_req }
end

#credsObject



37
38
39
40
# File 'lib/gcloud/logging/service.rb', line 37

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

#delete_log(name) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/gcloud/logging/service.rb', line 92

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

  backoff { logging.delete_log delete_req }
end

#delete_metric(name) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/gcloud/logging/service.rb', line 216

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

  backoff { metrics.delete_log_metric delete_req }
end

#delete_sink(name) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/gcloud/logging/service.rb', line 159

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

  backoff { sinks.delete_sink delete_req }
end

#get_metric(name) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/gcloud/logging/service.rb', line 193

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

  backoff { metrics.get_log_metric get_req }
end

#get_sink(name) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/gcloud/logging/service.rb', line 137

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

  backoff { sinks.get_sink get_req }
end

#inspectObject



224
225
226
# File 'lib/gcloud/logging/service.rb', line 224

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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gcloud/logging/service.rb', line 60

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)

  backoff { logging.list_log_entries list_req }
end

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



167
168
169
170
171
172
173
174
175
176
# File 'lib/gcloud/logging/service.rb', line 167

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

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

  backoff { metrics.list_log_metrics list_req }
end

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gcloud/logging/service.rb', line 100

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)

  backoff { logging.list_monitored_resource_descriptors list_req }
end

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



112
113
114
115
116
117
118
119
120
121
# File 'lib/gcloud/logging/service.rb', line 112

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

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

  backoff { sinks.list_sinks list_req }
end

#loggingObject



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

def logging
  return mocked_logging if mocked_logging
  @logging ||= Google::Logging::V2::LoggingServiceV2::Stub.new host, creds
end

#metricsObject



54
55
56
57
# File 'lib/gcloud/logging/service.rb', line 54

def metrics
  return mocked_metrics if mocked_metrics
  @metrics ||= Google::Logging::V2::MetricsServiceV2::Stub.new host, creds
end

#sinksObject



48
49
50
51
# File 'lib/gcloud/logging/service.rb', line 48

def sinks
  return mocked_sinks if mocked_sinks
  @sinks ||= Google::Logging::V2::ConfigServiceV2::Stub.new host, creds
end

#update_metric(name, description, filter) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/gcloud/logging/service.rb', line 201

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)
  )

  backoff { metrics.update_log_metric update_req }
end

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



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

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)
  )

  backoff { sinks.update_sink update_req }
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gcloud/logging/service.rb', line 74

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

  backoff { logging.write_log_entries write_req }
end