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

Allow templating of AWS log stream name #25633

Closed
wants to merge 1 commit into from

Conversation

jdnurmi
Copy link

@jdnurmi jdnurmi commented Aug 11, 2016

I ended up needing something similar to 17747 and drafted this guy up - it's a fairly straight-forward implementation, but let me know if I'm missing any style-wants/etc.

There's items it could probably use (strip ':'s from ImageId's comes to mind since they're forbidden by the CW logs API), but hopefully a good enough start for others as well.

@jdnurmi
Copy link
Author

jdnurmi commented Aug 11, 2016

It would appear the win2lin test was broken for a bit this evening, if someone can retrigger that test, I believe it should pass.

@thaJeztah
Copy link
Member

/cc @samuelkarp @cpuguy83

func New(ctx logger.Context) (logger.Logger, error) {
logGroupName := ctx.Config[logGroupKey]
logStreamName := ctx.ContainerID
if ctx.Config[logStreamKey] != "" {
logStreamName = ctx.Config[logStreamKey]
} else if ctx.Config[logStreamTemplateKey] != "" {
if t, err := template.New("t").Parse(ctx.Config[logStreamTemplateKey]); err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to extract this out to a separate function for readability.

@samuelkarp
Copy link
Member

Thanks for submitting this!

Can you add some unit tests to exercise the new functionality you added? It would also be good to update the documentation (in docs/admin/logging/awslogs.md).

@jdnurmi jdnurmi force-pushed the 17747-awslogs-stream-template branch 6 times, most recently from ccac0b4 to 0993704 Compare August 22, 2016 18:27
@jdnurmi
Copy link
Author

jdnurmi commented Aug 22, 2016

I think I got everything after some fighting with the code vetter, let me know if I left anything to be desired.

# awslogs-stream-template

Configure a [log stream](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html)
to a templated name. The template is executed with a [logger.Context](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html)
Copy link
Member

Choose a reason for hiding this comment

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

I think you have the wrong link for logger.Context.

Copy link
Author

Choose a reason for hiding this comment

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

indeed - will fix

@cpuguy83
Copy link
Member

Templating seems fairly generic enough to support in all drivers.
Otherwise I'm ok with the idea.

@cpuguy83
Copy link
Member

That is to say, +1. There isn't a need to implement all drivers here, but I don't think we need to namespace the option? WDYT?

@thaJeztah
Copy link
Member

I think that makes sense; we should take care to properly document that it's currently only supported in this driver, but otherwise sgtm

@thaJeztah
Copy link
Member

ping @jdnurmi can you address the review comments?

@jdnurmi jdnurmi force-pushed the 17747-awslogs-stream-template branch from 0993704 to 675b057 Compare September 28, 2016 12:41
@jdnurmi
Copy link
Author

jdnurmi commented Sep 28, 2016

Ok, I think I've got everything; Originally I'd thought that putting this on the Logger.Context directly might make sense, but I didn't want to muck with anything too deep unless there seemed like consensus.

It would be short work to do that if that's what people would like.

EDIT: And sorry for the delay!

@jdnurmi jdnurmi force-pushed the 17747-awslogs-stream-template branch 2 times, most recently from 91c0e39 to 068d2cb Compare September 28, 2016 12:56
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.

Changes look great! I'm going to ask our doc writer if he has any suggestions on the documentation, but otherwise it looks good.

name = buff.String()
}
if err == nil && strings.IndexAny(name, ":*") >= 0 {
// Arguably, we could let AWS reject this later, but by then your container is
Copy link
Member

Choose a reason for hiding this comment

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

(*logStream).create() synchronously calls CreateLogStream which should reject invalid names. Since errors resulting from that call cause New() to return an error, the container should not actually start running. However, I don't object to local validity checks in the name like this.

Copy link

@nrdlngr nrdlngr left a comment

Choose a reason for hiding this comment

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

Thanks @samuelkarp for suggesting for doc review. Just a few doc suggestions for the option for consistency with the existing docs.

to a templated name. The template is executed with a [logger.Context](https://godoc.org/github.com/docker/docker/daemon/logger#Context)
as context, so awslogs-stream-template="{{ .ContainerID }}" would be equivelant to the default behavior.

See the note above with regards to name uniqueness and performance.
Copy link

Choose a reason for hiding this comment

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

It wouldn't hurt to copy the above note to here, to avoid ambiguity.

@@ -63,6 +63,14 @@ specified, the container ID is used as the log stream.
> at a time. Using the same log stream for multiple containers concurrently
> can cause reduced logging performance.

# awslogs-stream-template

Configure a [log stream](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html)
Copy link

Choose a reason for hiding this comment

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

You might try something like this:

Specify a template to automatically generate the log stream name to use. The template supports logger.Context, so awslogs-stream-template="{{ .ContainerID }}" would be equivalent to the default behavior.

@jdnurmi jdnurmi force-pushed the 17747-awslogs-stream-template branch from 068d2cb to 5ef3a5e Compare September 29, 2016 01:21
- fixes moby#17747
- Uses the logging.Context for template context
- Add tests, check for conflicting options, gofmt
- Add documentation
- golint ++ vs +=
- testing pedantry: 'text/template.ExecError composite literal uses unkeyed fields' in tests

per @samuelkarp
- Moved location
- fixed up log message
* ahem. and fix the test
- refactor style (explict returns) to match rest of file

per @nrdlngr
- Documentation clarity improvements

- Fix logging link
- Remove spurious note

Signed-off-by: James Nurmi <[email protected]>
@jdnurmi jdnurmi force-pushed the 17747-awslogs-stream-template branch from 5ef3a5e to 2dd50e3 Compare September 29, 2016 01:23
@jdnurmi
Copy link
Author

jdnurmi commented Sep 29, 2016

Note copied, text changed as specified - let me know if I've dropped any comments here.

@thaJeztah
Copy link
Member

ping @dnephin @vdemeester PTAL

@jdnurmi
Copy link
Author

jdnurmi commented Oct 5, 2016

fwiw, I believe the windows failure is just a fluke, if someone could retrigger it, I believe this will be passing again.

@thaJeztah
Copy link
Member

ping @dnephin @vdemeester could you review this, in combination with #27480 to see which implementation is best, and what we should to to get these moving?

@vdemeester
Copy link
Member

hey @jdnurmi thanks so much for your contribution, but we are going #27707 which is a similar implementation tested and more inline with others log drivers. It will land in 1.13.0 😉

@vdemeester vdemeester closed this Nov 3, 2016
@frezbo
Copy link

frezbo commented Feb 6, 2018

@vdemeester @thaJeztah this PR is actually superior over the tags option I could state multiple reasons for this:

  • consider a service that keeps on restarting, I get multiple log stream o/p if I use tags with the container id, it is not at all easy to go through each log stream and this clutters the logstream
  • Consider a service having multiple replicas, we have a use case that we want each replica of the service to log to its own logstream (always to the same logstream whether the container dies and gets recreated)
  • The current implementation does not provide insight into which node produced the logs (we label node and restrain services on to specific nodes). Having no insight into which node produced the logs is so cumbersome and hard to debug,

I believe the current aws log driver options does not provide any useful implementation for production deployment.
I would suggest that both the logstream and tag support be there. This is what I would like to see:
awslogs-stream: "{{.Node.Hostname}}-{{.Service.Name}}-{{.Task.Name}}-{{.Task.Slot}}"

The ability to fine tune the logstream
Hope you get the issue

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.

9 participants