Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aws logstream #27707

Merged
merged 1 commit into from
Nov 3, 2016
Merged

Aws logstream #27707

merged 1 commit into from
Nov 3, 2016

Conversation

FrenchBen
Copy link
Contributor

- What I did
Added support for tags to the AWS log driver - The logstream flag will still over-write the tag, allowing for backwards compatibility. The tag parses the regular tag as seen in syslog, etc.

- How I did it
I updated the default of logStream from container ID to use the tag parsing default.

- How to verify it
make test (although I need to add some test for tags passed as flag)
make

- Description for the changelog
Update of AWS log driver to support tags. This enables support for things like --log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}" as a tag for CloudWatch.

- A picture of a cute animal (not mandatory but encouraged)
Corgi

cc @samuelkarp

@samuelkarp
Copy link
Member

@FrenchBen Thanks for writing this and thanks for tagging me! At first glance this looks like #25633 and #27480. While many of the log drivers are using tag, I'd still lean against it for the reason I wrote here: #17747 (comment) (tags have a different meaning in the context of AWS).

@FrenchBen
Copy link
Contributor Author

FrenchBen commented Oct 25, 2016

@samuelkarp Thanks for the quick review.
Keeping things simple and consistent with the existing tag functionality was my main focus, and as you mentioned many drivers currently use.
Creating an aws-specific tag, such as aws-tag to match the current options naming, would be the go-to if it needed to have the same meaning as the AWS tags in #17747 (comment).

IMHO

@vdemeester
Copy link
Member

I do agree with @FrenchBen on the two other PRs. For the naming, same things, I would have liked to keep using tag (as other logging driver do) but I'm also okay with a aws specific one if needed — we would just have to change ParseLogTag to be able to pass which name to use.

Aside from the naming, design LGTM on this one 👼

Copy link
Member

@samuelkarp samuelkarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrenchBen I'm still not wild about tag since the primitive here is a log stream, but I think you're right that we could create an awslogs-tags option if necessary.

@@ -350,6 +355,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case logGroupKey:
case logStreamKey:
case regionKey:
case "tag":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this is not a constant? It would be useful to share the constant across all the log drivers that validate option names (maybe loggerutils.TagKey?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelkarp that's a good point… not sure why this (and labels/env) are not constant. But this could be done in a follow-up PR 👼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -88,7 +89,11 @@ func init() {
// the EC2 Instance Metadata Service.
func New(ctx logger.Context) (logger.Logger, error) {
logGroupName := ctx.Config[logGroupKey]
logStreamName := ctx.ContainerID
logStreamName, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make it a validation error to specify both tag and awslogs-stream?

Copy link
Contributor Author

@FrenchBen FrenchBen Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this should trigger an error - Specifying awslogs-stream with tag will have no error, as awslogs-stream is "king" and overwrites the tag specified.

This allows you to update the driver and use the tag specified when no logstream have been specified, but not see any change in currently running containers that have the --log-opt awslogs-stream="someApp" (keeps this driver backwards compatible)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the use-case for setting --log-opt tag="{{.Name}}/{{.ID}}" (or similar) on the daemon so that it provides the template by default? If so, can we get the docs updated as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelkarp that's exactly right, the idea is to set a template on the daemon for all new containers.
I'll make sure to create a PR for the docs to show that tag is now allowed but awslogs-stream would overwrite the tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just looked at loggerutils.DefaultTemplate and it's {{.ID}}, which would be a change in behavior from the existing driver. Can you use {{.FullID}} instead to keep the existing behavior?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrenchBen Can you change the template to {{.FullID}} by default so the driver retains its existing behavior?

}
argument := <-mockClient.createLogStreamArgument

if *argument.LogStreamName != "test-container/container-ab" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ParseLogTag enforcing a length limit here? The maximum length of the stream name is 512 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tag only over-writes the logStreamName; whatever restrictions were in place before, are still in place.
Essentially I went from taking the default for logStreamName := ctx.ContainerID to use the default of the tag (which is also ctx.ContainerID), but adds the flexibility of tag parsing as seen with all other log drivers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's shortening "container-abcdefghijklmnopqrstuvwxyz01234567890" to "container-ab"? I would have expected this assertion to be "test-container/container-abcdefghijklmnopqrstuvwxyz01234567890".

Copy link
Contributor Author

@FrenchBen FrenchBen Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the docs:
https://docs.docker.com/engine/admin/logging/log_tags/
{{.ID}} The first 12 characters of the container id.
expr length "container-ab" = 12

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. It might be nicer to change the template used for the test to one that doesn't involve truncation (maybe {{.FullID}}) so it's more obvious what's going on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel super strongly about this, but having to look in another location to see why it's truncated makes the test more difficult for someone unfamiliar with the codebase to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More than happy to update the PR to show the FullID if that helps getting an LGTM from you ;)

@vdemeester vdemeester added this to the 1.13.0 milestone Oct 27, 2016
@vdemeester
Copy link
Member

/me tentatively add this to the 1.13.0 milestone 😝

@vdemeester vdemeester self-assigned this Oct 27, 2016
@@ -719,6 +719,6 @@ func TestCreateTagSuccess(t *testing.T) {
argument := <-mockClient.createLogStreamArgument

if *argument.LogStreamName != "test-container/container-ab" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's going to fail now because the assertion didn't get updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - make it fail, then show that it's working :)

@FrenchBen
Copy link
Contributor Author

@samuelkarp all green! 💚

@samuelkarp
Copy link
Member

@FrenchBen Are you planning to open a docs PR after this is merged?

@FrenchBen
Copy link
Contributor Author

@samuelkarp absolutely!

@samuelkarp
Copy link
Member

@FrenchBen Thanks for opening the docs PR! The only remaining feedback I have is about the default stream name when both awslogs-stream and tag are not specified (see this comment).

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

design LGTM 🐸

@FrenchBen
Copy link
Contributor Author

Missed the comment - Let me update the PR to reflect that

Copy link
Member

@samuelkarp samuelkarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah
Copy link
Member

@FrenchBen can you squash your commits?

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🐸 once commit squashed 👼

@FrenchBen
Copy link
Contributor Author

:shipit: ⚡️ @vdemeester

thaJeztah
thaJeztah previously approved these changes Nov 3, 2016
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

thanks for reviewing @samuelkarp!

@cpuguy83
Copy link
Member

cpuguy83 commented Nov 3, 2016

@FrenchBen It'd be nice to get these into 1 commit.

@samuelkarp
Copy link
Member

@thaJeztah Happy to help!

@thaJeztah
Copy link
Member

oh! I thought it was squashed, darn. @FrenchBen ❤️

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah
Copy link
Member

all :green: merging!

@ozbillwang
Copy link
Contributor

ozbillwang commented Jul 23, 2020

Could anyone explain how to use this new feature with awslogs-stream?

could you share the docker run command and docker version?

I did the same as --log-opt awslogs-stream="$HOSTNAME-{{.ID}}", but I didn't get the docker container id properly.

docker run --log-driver=awslogs --log-opt awslogs-region=ap-southeast-2
  --log-opt awslogs-create-group=true 
  --log-opt awslogs-group="hello-world"
  --log-opt awslogs-stream="$HOSTNAME-{{.ID}}"
  hello-world

Here is the log stream name I saw, it doesn't show container id of {{.ID}}

ip-10-0-2-123.ap-southeast-2.compute.internal-{{.ID}}

I did try these log_tags one by one, seems no one works.

https://docs.docker.com/config/containers/logging/log_tags/

{{.ID}} | The first 12 characters of the container ID.
{{.FullID}} | The full container ID.
{{.Name}} | The container name.
{{.ImageID}} | The first 12 characters of the container’s image ID.
{{.ImageFullID}} | The container’s full image ID.
{{.ImageName}} | The name of the image used by the container.
{{.DaemonName}} | The name of the docker program (docker).

@samuelkarp
Copy link
Member

Use --log-opt tag, not --log-opt awslogs-stream. See the documentation.

@ozbillwang
Copy link
Contributor

ozbillwang commented Jul 24, 2020

Thanks a lot, that works.

docker run --log-driver=awslogs --log-opt awslogs-region=ap-southeast-2
  --log-opt awslogs-create-group=true 
  --log-opt awslogs-group="hello-world"
  --log-opt tag="$HOSTNAME/{{.ID}}"
  hello-world

seems log-opt awslogs-stream has conflict with tag, they can't be used together. If used both, awslogs-stream gets the priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants