Google Cloud Pub/Sub (im_googlepubsub)

Google Cloud Pub/Sub is a service that allows event producers to deliver events to subscribers asynchronously. It is commonly used for data streaming, real-time event distribution, or as a message queue for parallel workflows.

This module uses the Google Pub/Sub REST API to create a subscription and collect logs from a Google Pub/Sub topic.

im_googlepubsub trims messages containing a NULL byte (\x00), discarding all subsequent data. Ensure that messages do not contain NULL byte characters at the event source, or contact NXLog if this is not possible.
To examine the supported platforms, see the list of installation packages.

Configuring a Google Cloud service account

im_googlepubsub requires a Google Cloud service account and a corresponding private key file in JSON format to connect to the Google Cloud Pub/Sub API. Follow these instructions to create a new service account and download its private key file for an existing project.

  1. Log in to your Google Cloud account and switch to the project you want to configure.

  2. From the navigation menu, click IAM & Admin > Service Accounts.

    IAM & Admin menu
  3. Click CREATE SERVICE ACCOUNT.

  4. Enter a service account name and description and click CREATE AND CONTINUE.

    Create service account
  5. Select the Owner role from the Role drop-down and click DONE.

    Service account role
  6. Click on the newly created account on the Service accounts page to open its configuration page.

  7. Click the KEYS tab, expand the ADD KEY drop-down and select Create new key.

    Create new service key
  8. Select JSON for the key type and click CREATE to download the private key. Save the private key file to a location accessible by NXLog Agent. This file is required for the NXLog Agent configuration.

    New service key type

Configuration

The im_googlepubsub module accepts the following directives in addition to the common module directives. The CredentialsFile and Subscription directives are required.

Required directives

The following directives are required for the module to start.

CredentialsFile

This mandatory directive specifies the path to the private key file of the service account required for authenticating with the Google Pub/Sub. See Configuring a Google Cloud service account for more information.

Subscription

This mandatory directive specifies the name of the subscription from which messages should be pulled. The format is projects/{project}/subscriptions/{sub}.

HTTPS directives

The following directives configure secure data transfer via HTTPS.

HTTPSAllowExpired

Set this directive to TRUE to allow connections with remote hosts that present an expired certificate. The default is FALSE; the module rejects connections with expired certificates.

HTTPSAllowUntrusted

Set this directive to TRUE to allow connections with remote hosts regardless of certificate verification results. The default is FALSE; the module only accepts connections with trusted certificates.

HTTPSCADir

Set this directive to the path of a directory containing Certificate Authority (CA) certificates. The module uses these certificates to verify the certificate presented by the remote host. Name the certificate files using the OpenSSL hashed format: the hash of the certificate followed by .0, .1, and so on. To find the hash of a certificate using OpenSSL:

$ openssl x509 -hash -noout -in ca.crt

For example, if the certificate hash is e2f14e4a, name the certificate file e2f14e4a.0. If there is another certificate with the same hash, name it e2f14e4a.1, and so on.

To trust a remote host’s self-signed certificate, include a copy of it in this directory.

If this directive isn’t specified, NXLog Agent falls back to the default operating system root certificate store. Unix-like operating systems commonly store root certificates in /etc/ssl/certs. Windows operating systems use the Windows Certificate Store, while macOS uses the Keychain Access Application as the default certificate store. See Certification Authority (CA) certificates in the NXLog Platform User Guide for more information on using this directive.

In addition, Microsoft’s PKI repository contains root certificates for Microsoft services.

HTTPSCAFile

Set this directive to the path of the Certificate Authority (CA) certificate used to verify the certificate presented by the remote host. To trust a remote host’s self-signed certificate, specify the remote host’s certificate itself. For certificates signed by an intermediate CA, the specified certificate must contain the complete certificate chain (certificate bundle).

HTTPSCertFile

Set this directive to the path of the certificate file to present to the remote host during the HTTPS handshake.

HTTPSCertKeyFile

Set this directive to the path of the private key file corresponding to the certificate specified by HTTPSCertFile.

Optional directives

Acknowledge

This optional boolean directive specifies whether an acknowledge confirmation should be sent for received messages. The default is TRUE.

AddHeader

Set this directive to a custom header to add to each HTTP request. Specify this directive multiple times to add more than one header.

Compression

Set this directive to enable HTTP compression for outgoing HTTP messages. The accepted values are none, gzip, and deflate. The default is none; compression is disabled.

Some HTTP servers may not accept compressed HTTP requests. If the server doesn’t support the specified compression method, it may return 415 Unsupported Media Type errors.

HTTPBasicAuthPassword

Set this directive to the HTTP basic authentication password. This directive requires you to also set the HTTPBasicAuthUser directive to use HTTP authentication.

HTTPBasicAuthUser

Set this directive to the HTTP basic authentication username. If the HTTPBasicAuthPassword directive is not set, the module attempts HTTP Basic authentication using an empty password.

Proxy

Set this directive to route outgoing requests through an HTTP or SOCKS proxy host. The format is protocol://hostname:port.

Reconnect

This optional directive sets the reconnect interval in seconds. If it is set, the module attempts to reconnect in every defined second. If it is not set, the reconnect interval will start at 1 second and double with every attempt. In the latter case, when the system decides that the reconnection is successful, the reconnect interval is immediately reset to 1 sec.

The Reconnect directive must be used with caution. If it is used on multiple systems, it can send reconnect requests simultaneously to the same destination, potentially overloading the destination system. It may also cause NXLog Agent to use unusually high system resources or cause NXLog Agent to become unresponsive.

ReconnectOnData

This optional directive defines the behavior when the connection with the remote host is lost. When set to TRUE, the module only attempts to reconnect when it has data to send. The default value is FALSE; it will always keep a connection open with the remote host.

PollInterval

This directive specifies how frequently the module will check for new events in seconds. If this directive is not specified, it defaults to 20 seconds.

ReadFromLast

This boolean directive instructs the module on where to start reading events from the log source when NXLog Agent starts.

When TRUE, NXLog Agent will only read events logged after NXLog Agent started, unless SavePos is TRUE and a saved position for this log source exists in the cache file.

When FALSE, NXLog Agent will read all events from the log source, unless SavePos is TRUE and a saved position for this log source exists in the cache file.

The default is TRUE.

For this to take effect, the subscription must have the "Retain acknowledged messages" option activated. This directive is usually not required if the Acknowledge directive is TRUE (default), as the Google Cloud Pub/Sub server will manage the starting point when a connection is (re-)established.

The following matrix shows the outcome of this directive in conjunction with the SavePos directive:

ReadFromLast SavePos Saved position Outcome

TRUE

TRUE

Yes

Reads events from the saved position.

TRUE

TRUE

No

Reads events that are logged after NXLog Agent is started.

TRUE

FALSE

Yes

Reads events that are logged after NXLog Agent is started.

TRUE

FALSE

No

Reads events that are logged after NXLog Agent is started.

FALSE

TRUE

Yes

Reads events from the saved position.

FALSE

TRUE

No

Reads all events.

FALSE

FALSE

Yes

Reads all events.

FALSE

FALSE

No

Reads all events.

If the NoCache directive is TRUE, it overrides the SavePos directive. In this case, the module behaves as if SavePos is FALSE.

SavePos

This boolean directive instructs the module whether to save the timestamp of the last read event before NXLog Agent exits. Together with the ReadFromLast directive, it allows the agent to continue reading events from the saved position.

The default is TRUE; the timestamp is saved and will be read from the cache file on the next startup.

This directive is not normally required if the Acknowledge directive is set to 'TRUE' (default) as the Google Cloud Pub/Sub server will manage the starting point when a connection is (re-)established. For this to have an effect the subscription must have the "Retain acknowledged messages" option activated.

If the NoCache directive is TRUE, it overrides the SavePos directive. In this case, the module behaves as if SavePos is FALSE.

URL

Optional directive for specifying a region-specific URL. The default is https://pubsub.googleapis.com.

Fields

The following fields are created by im_googlepubsub.

$raw_event (type: string)

A list of event fields in key-value pairs.

$Attributes.* (type: string)

Attributes for this message. It contains user-defined keys.

$Data (type: string)

The message data field.

$MessageId (type: string)

ID of this message, assigned by the server when the message is published.

$OrderingKey (type: string)

If non-empty, identifies related messages for which publish order should be respected. If a Subscription has enableMessageOrdering set to true, messages published with the same non-empty orderingKey value will be delivered to subscribers in the order in which they are received by the Pub/Sub system.

$PublishTime (type: datetime)

The time at which the message was published, populated by the server when it receives the topics.publish call.

Examples

Example 1. Collecting logs from Google Pub/Sub

This configuration uses the im_googlepubsub input module to create the subscription test and collect logs from the Google Cloud project myproject.

<Input google_pubsub>
    Module             im_googlepubsub
    CredentialsFile    /path/to/credentials.json (1)
    Subscription       projects/myproject-343509/subscriptions/test
</Input>
1 Credentials file for authenticating with the Google Pub/Sub. See Configuring a Google Cloud service account for more information.