Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

Commit

Permalink
Merge pull request #816 from mwielgus/log-sink-events
Browse files Browse the repository at this point in the history
Log sink for events
  • Loading branch information
mwielgus committed Dec 23, 2015
2 parents be9d447 + d9ffc3f commit 07f1d7d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
51 changes: 51 additions & 0 deletions events/sinks/log/log_sink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logsink

import (
"bytes"
"fmt"

"github.com/golang/glog"
"k8s.io/heapster/events/core"
)

type LogSink struct {
}

func (this *LogSink) Name() string {
return "LogSink"
}

func (this *LogSink) Stop() {
// Do nothing.
}

func batchToString(batch *core.EventBatch) string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("EventBatch Timestamp: %s\n", batch.Timestamp))
for _, event := range batch.Events {
buffer.WriteString(fmt.Sprintf(" %s (cnt:%d): %s\n", event.LastTimestamp, event.Count, event.Message))
}
return buffer.String()
}

func (this *LogSink) ExportEvents(batch *core.EventBatch) {
glog.Info(batchToString(batch))
}

func NewLogSink() *LogSink {
return &LogSink{}
}
49 changes: 49 additions & 0 deletions events/sinks/log/log_sink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logsink

import (
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
kube_api "k8s.io/kubernetes/pkg/api"
kube_api_unversioned "k8s.io/kubernetes/pkg/api/unversioned"

"k8s.io/heapster/events/core"
)

func TestSimpleWrite(t *testing.T) {
now := time.Now()
event := kube_api.Event{
Message: "bzium",
Count: 251,
LastTimestamp: kube_api_unversioned.NewTime(now),
FirstTimestamp: kube_api_unversioned.NewTime(now),
}
batch := core.EventBatch{
Timestamp: now,
Events: []*kube_api.Event{&event},
}

log := batchToString(&batch)
fmt.Printf(log)

assert.True(t, strings.Contains(log, "bzium"))
assert.True(t, strings.Contains(log, "251"))
assert.True(t, strings.Contains(log, fmt.Sprintf("%s", now)))
}

0 comments on commit 07f1d7d

Please sign in to comment.