本文整理汇总了Golang中github.com/cloudfoundry/dropsonde/emitter.Wrap函数的典型用法代码示例。如果您正苦于以下问题:Golang Wrap函数的具体用法?Golang Wrap怎么用?Golang Wrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Wrap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Run
func (r *TruncatingBuffer) Run() {
for msg := range r.inputChannel {
r.lock.Lock()
select {
case r.outputChannel <- msg:
default:
messageCount := len(r.outputChannel)
r.droppedMessageCount += int64(messageCount)
r.outputChannel = make(chan *events.Envelope, cap(r.outputChannel))
appId := envelope_extensions.GetAppId(msg)
lm := generateLogMessage(fmt.Sprintf("Log message output too high. We've dropped %d messages", messageCount), appId)
env, err := emitter.Wrap(lm, r.dropsondeOrigin)
if err == nil {
r.outputChannel <- env
} else {
r.logger.Warnf("Error marshalling message: %v", err)
}
r.outputChannel <- msg
if r.logger != nil {
r.logger.Warn(fmt.Sprintf("TB: Output channel too full. Dropped %d messages for app %s.", messageCount, appId))
}
}
r.lock.Unlock()
}
close(r.outputChannel)
}
示例2: Emit
func (e *EventWriter) Emit(event events.Event) error {
envelope, err := emitter.Wrap(event, e.origin)
if err != nil {
return err
}
return e.EmitEnvelope(envelope)
}
示例3: emitMessage
func (r *TruncatingBuffer) emitMessage(outputChannel chan *events.Envelope, event events.Event) {
env, err := emitter.Wrap(event, r.context.DropsondeOrigin())
if err == nil {
outputChannel <- env
} else {
r.logger.Warnf("Error marshalling message: %v", err)
}
}
示例4: SendSyslogErrorToLoggregator
func (sinkManager *SinkManager) SendSyslogErrorToLoggregator(errorMsg string, appId string) {
sinkManager.logger.Warn(errorMsg)
logMessage := factories.NewLogMessage(events.LogMessage_ERR, errorMsg, appId, "LGR")
envelope, err := emitter.Wrap(logMessage, sinkManager.dropsondeOrigin)
if err != nil {
sinkManager.logger.Warnf("Error marshalling message: %v", err)
return
}
sinkManager.errorChannel <- envelope
}
示例5: DropsondeLogMessage
func DropsondeLogMessage(messageString string, appId string) []byte {
currentTime := time.Now()
logMessage := &events.LogMessage{
Message: []byte(messageString),
MessageType: events.LogMessage_ERR.Enum(),
Timestamp: proto.Int64(currentTime.UnixNano()),
SourceType: proto.String("DOP"),
AppId: &appId,
}
envelope, _ := emitter.Wrap(logMessage, "doppler")
msg, _ := proto.Marshal(envelope)
return msg
}
示例6: main
func main() {
flag.Parse()
la, err := net.ResolveUDPAddr("udp", *destination)
if err != nil {
log.Fatalf("Error resolving loggregator address %s, %s", *destination, err)
}
// connection, err := net.DialUDP("udp", nil, la)
connection, err := net.ListenPacket("udp4", "")
if err != nil {
log.Fatalf("Error opening udp stuff")
}
msg := make([]byte, *size, *size)
envelope, err := emitter.Wrap(&events.LogMessage{
Message: msg,
MessageType: events.LogMessage_OUT.Enum(),
Timestamp: proto.Int64(time.Now().UnixNano()),
}, "origin")
if err != nil {
log.Fatal(err.Error())
}
buf, err := proto.Marshal(envelope)
if err != nil {
log.Fatal(err.Error())
}
finalBytes := signature.SignMessage(buf, []byte(*secret))
// println("marshal time", t2.Sub(t1).String())
var i int
d := time.Duration(*duration)
time.AfterFunc(d*time.Second, func() {
fmt.Printf("%d, %d, %d, ", *duration, len(finalBytes), i)
connection.Close()
os.Exit(0)
})
t := time.NewTicker(time.Second / time.Duration(*rate))
for {
<-t.C
connection.WriteTo(finalBytes, la)
i++
}
}
示例7: SendEventTCP
func SendEventTCP(event events.Event, conn net.Conn) error {
envelope, err := emitter.Wrap(event, "origin")
Expect(err).NotTo(HaveOccurred())
bytes, err := proto.Marshal(envelope)
if err != nil {
return err
}
err = binary.Write(conn, binary.LittleEndian, uint32(len(bytes)))
if err != nil {
return err
}
_, err = conn.Write(bytes)
return err
}
示例8: droppedLogMessage
func (w *Writer) droppedLogMessage() []byte {
droppedMessages := atomic.LoadUint64(&w.droppedMessages)
logMessage := &events.LogMessage{
Message: []byte(fmt.Sprintf("Dropped %d message(s) from MetronAgent to Doppler", droppedMessages)),
MessageType: events.LogMessage_ERR.Enum(),
AppId: proto.String(envelope_extensions.SystemAppId),
Timestamp: proto.Int64(time.Now().UnixNano()),
}
env, err := emitter.Wrap(logMessage, "MetronAgent")
if err != nil {
w.logger.Fatalf("Failed to emitter.Wrap a log message: %s", err)
}
marshaled, err := proto.Marshal(env)
if err != nil {
w.logger.Fatalf("Failed to marshal generated dropped log message: %s", err)
}
prefixedBytes, err := w.prefixMessage(marshaled)
if err != nil {
w.logger.Fatalf("Failed to prefix dropped log message: %s", err)
}
return prefixedBytes
}
示例9: MarshalEvent
func MarshalEvent(event events.Event, secret string) []byte {
envelope, _ := emitter.Wrap(event, "origin")
envelopeBytes := MarshalProtoBuf(envelope)
return signature.SignMessage(envelopeBytes, []byte(secret))
}
示例10:
Describe("SendTo", func() {
It("sends to all known sinks", func() {
sink1 := &channelSink{appId: "myApp",
identifier: "myAppChan1",
done: make(chan struct{}),
}
sink2 := &channelSink{appId: "myApp",
identifier: "myAppChan2",
done: make(chan struct{}),
}
sinkManager.RegisterSink(sink1)
sinkManager.RegisterSink(sink2)
expectedMessageString := "Some Data"
expectedMessage, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, expectedMessageString, "myApp", "App"), "origin")
go sinkManager.SendTo("myApp", expectedMessage)
Eventually(sink1.Received).Should(HaveLen(1))
Eventually(sink2.Received).Should(HaveLen(1))
Expect(sink1.Received()[0]).To(Equal(expectedMessage))
Expect(sink2.Received()[0]).To(Equal(expectedMessage))
})
It("only sends to sinks that match the appID", func(done Done) {
sink1 := &channelSink{appId: "myApp1",
identifier: "myAppChan1",
done: make(chan struct{}),
}
sink2 := &channelSink{appId: "myApp2",
identifier: "myAppChan2",
示例11:
Expect(websocketSink.ShouldReceiveErrors()).To(BeTrue())
})
})
Describe("Run", func() {
var inputChan chan *events.Envelope
BeforeEach(func() {
inputChan = make(chan *events.Envelope, 10)
})
It("forwards messages", func(done Done) {
defer close(done)
go websocketSink.Run(inputChan)
message, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "hello world", "appId", "App"), "origin")
messageBytes, _ := proto.Marshal(message)
inputChan <- message
Eventually(fakeWebsocket.ReadMessages).Should(HaveLen(1))
Expect(fakeWebsocket.ReadMessages()[0]).To(Equal(messageBytes))
messageTwo, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "goodbye world", "appId", "App"), "origin")
messageTwoBytes, _ := proto.Marshal(messageTwo)
inputChan <- messageTwo
Eventually(fakeWebsocket.ReadMessages).Should(HaveLen(2))
Expect(fakeWebsocket.ReadMessages()[1]).To(Equal(messageTwoBytes))
})
It("sets write deadline", func() {
go websocketSink.Run(inputChan)
示例12:
goRoutineSpawned.Wait()
})
AfterEach(func() {
sinkManager.Stop()
TestMessageRouter.Stop()
TestWebsocketServer.Stop()
services.Wait()
})
It("dumps all messages for an app user", func() {
expectedFirstMessageString := "Some data 1"
lm := factories.NewLogMessage(events.LogMessage_OUT, expectedFirstMessageString, "myOtherApp", "APP")
env1, _ := emitter.Wrap(lm, "ORIGIN")
expectedSecondMessageString := "Some data 2"
lm = factories.NewLogMessage(events.LogMessage_OUT, expectedSecondMessageString, "myOtherApp", "APP")
env2, _ := emitter.Wrap(lm, "ORIGIN")
dataReadChannel <- env1
dataReadChannel <- env2
receivedChan := make(chan []byte, 2)
_, stopKeepAlive, droppedChannel := AddWSSink(receivedChan, serverPort, "/apps/myOtherApp/recentlogs")
Eventually(droppedChannel).Should(Receive())
var firstMarshalledEnvelope, secondMarshalledEnvelope []byte
Eventually(receivedChan).Should(Receive(&firstMarshalledEnvelope))
示例13:
})
Describe("failed connections", func() {
It("fails without an appId", func() {
_, connectionDropped = AddWSSink(wsReceivedChan, fmt.Sprintf("ws://%s/apps//stream", apiEndpoint))
Expect(connectionDropped).To(BeClosed())
})
It("fails with bad path", func() {
_, connectionDropped = AddWSSink(wsReceivedChan, fmt.Sprintf("ws://%s/apps/my-app/junk", apiEndpoint))
Expect(connectionDropped).To(BeClosed())
})
})
It("dumps buffer data to the websocket client with /recentlogs", func(done Done) {
lm, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "my message", appId, "App"), "origin")
sinkManager.SendTo(appId, lm)
AddWSSink(wsReceivedChan, fmt.Sprintf("ws://%s/apps/%s/recentlogs", apiEndpoint, appId))
rlm, err := receiveEnvelope(wsReceivedChan)
Expect(err).NotTo(HaveOccurred())
Expect(rlm.GetLogMessage().GetMessage()).To(Equal(lm.GetLogMessage().GetMessage()))
close(done)
})
It("dumps container metric data to the websocket client with /containermetrics", func(done Done) {
cm := factories.NewContainerMetric(appId, 0, 42.42, 1234, 123412341234)
envelope, _ := emitter.Wrap(cm, "origin")
sinkManager.SendTo(appId, envelope)
示例14:
"github.com/cloudfoundry/dropsonde/factories"
"github.com/cloudfoundry/sonde-go/events"
"github.com/cloudfoundry/dropsonde/emitter"
"github.com/cloudfoundry/loggregatorlib/loggertesthelper"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Truncating Buffer", func() {
It("works like a channel", func() {
inMessageChan := make(chan *events.Envelope)
buffer := truncatingbuffer.NewTruncatingBuffer(inMessageChan, 2, loggertesthelper.Logger(), "dropsonde-origin")
go buffer.Run()
logMessage1, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "message 1", "appId", "App"), "origin")
inMessageChan <- logMessage1
readMessage := <-buffer.GetOutputChannel()
Expect(readMessage.GetLogMessage().GetMessage()).To(ContainSubstring("message 1"))
logMessage2, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "message 2", "appId", "App"), "origin")
inMessageChan <- logMessage2
readMessage2 := <-buffer.GetOutputChannel()
Expect(readMessage2.GetLogMessage().GetMessage()).To(ContainSubstring("message 2"))
})
It("works like a truncating channel", func() {
inMessageChan := make(chan *events.Envelope)
buffer := truncatingbuffer.NewTruncatingBuffer(inMessageChan, 2, loggertesthelper.Logger(), "dropsonde-origin")
示例15:
defer conn.Close()
Expect(tlsListener.Start).Should(Panic())
})
It("panics if you start after a stop", func() {
conn := openTLSConnection(tlsListener.Address())
defer conn.Close()
tlsListener.Stop()
Expect(tlsListener.Start).Should(Panic())
})
It("fails to send message after listener has been stopped", func() {
logMessage := factories.NewLogMessage(events.LogMessage_OUT, "some message", "appId", "source")
envelope, _ := emitter.Wrap(logMessage, "origin")
conn := openTLSConnection(tlsListener.Address())
err := send(conn, envelope)
Expect(err).ToNot(HaveOccurred())
tlsListener.Stop()
Eventually(func() error {
return send(conn, envelope)
}).Should(HaveOccurred())
conn.Close()
})
})
})