Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit d00fa0c

Browse files
alexamiesrghetia
authored andcommitted
Added payload size to http server integration (#1129)
* Added payload size to http server integration, logged in the LogExporter, and modified example to use LogExporter * Fixed format problems * Corrected comment
1 parent 6161d2e commit d00fa0c

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

examples/exporter/logexporter.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ func (e *LogExporter) ExportSpan(sd *trace.SpanData) {
177177
e.tLogger.Printf("Status: %v [%v]\n", sd.Status.Message, sd.Status.Code)
178178
e.tLogger.Printf("Elapsed: %v\n", sd.EndTime.Sub(sd.StartTime).Round(time.Millisecond))
179179

180+
spanKinds := map[int]string{
181+
1: "Server",
182+
2: "Client",
183+
}
184+
if spanKind, ok := spanKinds[sd.SpanKind]; ok {
185+
e.tLogger.Printf("SpanKind: %s\n", spanKind)
186+
}
187+
180188
if len(sd.Annotations) > 0 {
181189
e.tLogger.Println()
182190
e.tLogger.Println("Annotations:")
@@ -196,4 +204,21 @@ func (e *LogExporter) ExportSpan(sd *trace.SpanData) {
196204
e.tLogger.Printf("%v- %v=%v\n", indent, k, v)
197205
}
198206
}
207+
208+
if len(sd.MessageEvents) > 0 {
209+
eventTypes := map[trace.MessageEventType]string{
210+
trace.MessageEventTypeSent: "Sent",
211+
trace.MessageEventTypeRecv: "Received",
212+
}
213+
e.tLogger.Println()
214+
e.tLogger.Println("MessageEvents:")
215+
for _, item := range sd.MessageEvents {
216+
if eventType, ok := eventTypes[item.EventType]; ok {
217+
e.tLogger.Print(eventType)
218+
}
219+
e.tLogger.Printf("UncompressedByteSize: %v", item.UncompressedByteSize)
220+
e.tLogger.Printf("CompressedByteSize: %v", item.CompressedByteSize)
221+
e.tLogger.Println()
222+
}
223+
}
199224
}

examples/http/helloworld_server/main.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import (
2828
"go.opencensus.io/trace"
2929
)
3030

31+
const (
32+
metricsLogFile = "/tmp/metrics.log"
33+
tracesLogFile = "/tmp/trace.log"
34+
)
35+
3136
func main() {
3237
// Start z-Pages server.
3338
go func() {
@@ -36,10 +41,18 @@ func main() {
3641
log.Fatal(http.ListenAndServe("127.0.0.1:8081", mux))
3742
}()
3843

39-
// Register stats and trace exporters to export the collected data.
40-
exporter := &exporter.PrintExporter{}
41-
view.RegisterExporter(exporter)
42-
trace.RegisterExporter(exporter)
44+
// Using log exporter to export metrics but you can choose any supported exporter.
45+
exporter, err := exporter.NewLogExporter(exporter.Options{
46+
ReportingInterval: time.Duration(10 * time.Second),
47+
MetricsLogFile: metricsLogFile,
48+
TracesLogFile: tracesLogFile,
49+
})
50+
if err != nil {
51+
log.Fatalf("Error creating log exporter: %v", err)
52+
}
53+
exporter.Start()
54+
defer exporter.Stop()
55+
defer exporter.Close()
4356

4457
// Always trace for this demo. In a production application, you should
4558
// configure this to a trace.ProbabilitySampler set at the desired

plugin/ochttp/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Requ
124124
}
125125
}
126126
span.AddAttributes(requestAttrs(r)...)
127+
if r.Body == nil {
128+
// TODO: Handle cases where ContentLength is not set.
129+
} else if r.ContentLength > 0 {
130+
span.AddMessageReceiveEvent(0, /* TODO: messageID */
131+
int64(r.ContentLength), -1)
132+
}
127133
return r.WithContext(ctx), span.End
128134
}
129135

@@ -201,6 +207,9 @@ func (t *trackingResponseWriter) Header() http.Header {
201207
func (t *trackingResponseWriter) Write(data []byte) (int, error) {
202208
n, err := t.writer.Write(data)
203209
t.respSize += int64(n)
210+
// Add message event for request bytes sent.
211+
span := trace.FromContext(t.ctx)
212+
span.AddMessageSendEvent(0 /* TODO: messageID */, int64(n), -1)
204213
return n, err
205214
}
206215

0 commit comments

Comments
 (0)