Browse code

Merge pull request #24586 from farcaller/gcplogs

Added optional flags to init gcp logger metadata

Sven Dowideit authored on 2016/09/08 12:47:48
Showing 2 changed files
... ...
@@ -21,6 +21,9 @@ const (
21 21
 	logLabelsKey  = "labels"
22 22
 	logEnvKey     = "env"
23 23
 	logCmdKey     = "gcp-log-cmd"
24
+	logZoneKey    = "gcp-meta-zone"
25
+	logNameKey    = "gcp-meta-name"
26
+	logIDKey      = "gcp-meta-id"
24 27
 )
25 28
 
26 29
 var (
... ...
@@ -142,6 +145,12 @@ func New(ctx logger.Context) (logger.Logger, error) {
142 142
 			Name: instanceName,
143 143
 			ID:   instanceID,
144 144
 		}
145
+	} else if ctx.Config[logZoneKey] != "" || ctx.Config[logNameKey] != "" || ctx.Config[logIDKey] != "" {
146
+		l.instance = &instanceInfo{
147
+			Zone: ctx.Config[logZoneKey],
148
+			Name: ctx.Config[logNameKey],
149
+			ID:   ctx.Config[logIDKey],
150
+		}
145 151
 	}
146 152
 
147 153
 	// The logger "overflows" at a rate of 10,000 logs per second and this
... ...
@@ -163,7 +172,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
163 163
 func ValidateLogOpts(cfg map[string]string) error {
164 164
 	for k := range cfg {
165 165
 		switch k {
166
-		case projectOptKey, logLabelsKey, logEnvKey, logCmdKey:
166
+		case projectOptKey, logLabelsKey, logEnvKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
167 167
 		default:
168 168
 			return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
169 169
 		}
... ...
@@ -37,6 +37,10 @@ The `--gcp-project` takes precedence over information discovered from the metada
37 37
 so a Docker daemon running in a Google Cloud Project can be overridden to log to a different
38 38
 Google Cloud Project using `--gcp-project`.
39 39
 
40
+Docker fetches the values for zone, instance name and instance id from Google
41
+Cloud metadata server. Those values can be provided via options if metadata
42
+server is not available. They will not override the values from metadata server.
43
+
40 44
 ## gcplogs options
41 45
 
42 46
 You can use the `--log-opt NAME=VALUE` flag to specify these additional Google
... ...
@@ -48,6 +52,9 @@ Cloud Logging driver options:
48 48
 | `gcp-log-cmd`               | optional | Whether to log the command that the container was started with. Defaults to false.                                                          |
49 49
 | `labels`                    | optional | Comma-separated list of keys of labels, which should be included in message, if these labels are specified for container.                   |
50 50
 | `env`                       | optional | Comma-separated list of keys of environment variables, which should be included in message, if these variables are specified for container. |
51
+| `gcp-meta-zone`             | optional | Zone name for the instance. |
52
+| `gcp-meta-name`             | optional | Instance name. |
53
+| `gcp-meta-id`               | optional | Instance ID. |
51 54
 
52 55
 If there is collision between `label` and `env` keys, the value of the `env`
53 56
 takes precedence. Both options add additional fields to the attributes of a
... ...
@@ -57,13 +64,22 @@ Below is an example of the logging options required to log to the default
57 57
 logging destination which is discovered by querying the GCE metadata server.
58 58
 
59 59
     docker run --log-driver=gcplogs \
60
-        --log-opt labels=location
61
-        --log-opt env=TEST
62
-        --log-opt gcp-log-cmd=true
63
-        --env "TEST=false"
64
-        --label location=west
60
+        --log-opt labels=location \
61
+        --log-opt env=TEST \
62
+        --log-opt gcp-log-cmd=true \
63
+        --env "TEST=false" \
64
+        --label location=west \
65 65
         your/application
66 66
 
67 67
 This configuration also directs the driver to include in the payload the label
68 68
 `location`, the environment variable `ENV`, and the command used to start the
69 69
 container.
70
+
71
+An example of the logging options for running outside of GCE (the daemon must be
72
+configured with GOOGLE_APPLICATION_CREDENTIALS):
73
+
74
+    docker run --log-driver=gcplogs \
75
+        --log-opt gcp-project=test-project
76
+        --log-opt gcp-meta-zone=west1 \
77
+        --log-opt gcp-meta-name=`hostname` \
78
+        your/application