Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package telemetry_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/log/global"
|
||||
logpb "go.opentelemetry.io/proto/otlp/collector/logs/v1"
|
||||
logsdata "go.opentelemetry.io/proto/otlp/logs/v1"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"messenger/internal/platform/telemetry"
|
||||
)
|
||||
|
||||
// Decode the wire record as a collector would: trace IDs are OTLP fields,
|
||||
// whereas request IDs are attributes and sensitive inputs are discarded.
|
||||
func TestOTLPExportsCorrelatedSafeSignals(t *testing.T) {
|
||||
payloads := map[string][]byte{}
|
||||
var mu sync.Mutex
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
payloads[r.URL.Path] = b
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/x-protobuf")
|
||||
}))
|
||||
defer srv.Close()
|
||||
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", srv.URL)
|
||||
defer otel.SetTracerProvider(otel.GetTracerProvider())
|
||||
defer otel.SetMeterProvider(otel.GetMeterProvider())
|
||||
defer otel.SetTextMapPropagator(otel.GetTextMapPropagator())
|
||||
defer global.SetLoggerProvider(global.GetLoggerProvider())
|
||||
ctx := context.Background()
|
||||
shutdown, err := telemetry.Start(ctx, "messenger-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, span := otel.Tracer("test").Start(ctx, "safe operation")
|
||||
sc := span.SpanContext()
|
||||
telemetry.Event(ctx, "http.request", attribute.String("request_id", "req-123"), attribute.String("Authorization", "secret-data"))
|
||||
telemetry.Event(ctx, "attachment.retry", attribute.String("error_code", "broker_connect"))
|
||||
telemetry.Event(ctx, "secret-event-body", attribute.String("request_id", "ignored"))
|
||||
counter, err := otel.Meter("test").Int64Counter("test.requests")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
counter.Add(ctx, 1)
|
||||
span.End()
|
||||
shutdown()
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, path := range []string{"/v1/logs", "/v1/metrics", "/v1/traces"} {
|
||||
if len(payloads[path]) == 0 {
|
||||
t.Errorf("missing export %s", path)
|
||||
}
|
||||
}
|
||||
var logs logpb.ExportLogsServiceRequest
|
||||
if err := proto.Unmarshal(payloads["/v1/logs"], &logs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(logs.ResourceLogs) != 1 || len(logs.ResourceLogs[0].ScopeLogs) != 1 {
|
||||
t.Fatal("missing logs")
|
||||
}
|
||||
resources := map[string]string{}
|
||||
for _, kv := range logs.ResourceLogs[0].Resource.Attributes {
|
||||
resources[kv.Key] = kv.Value.GetStringValue()
|
||||
}
|
||||
if resources["service.name"] != "messenger-test" || resources["service.version"] != "lab6" || resources["service.instance.id"] == "" || resources["deployment.environment.name"] != "lab" {
|
||||
t.Errorf("resource identity: %v", resources)
|
||||
}
|
||||
records := logs.ResourceLogs[0].ScopeLogs[0].LogRecords
|
||||
if len(records) != 2 {
|
||||
t.Fatalf("got %d log records", len(records))
|
||||
}
|
||||
if retry := records[1]; retry.GetEventName() != "attachment.retry" || retry.GetSeverityNumber() != logsdata.SeverityNumber_SEVERITY_NUMBER_WARN {
|
||||
t.Errorf("retry log: %v", retry)
|
||||
}
|
||||
r := records[0]
|
||||
tid, sid := sc.TraceID(), sc.SpanID()
|
||||
if !bytes.Equal(r.TraceId, tid[:]) || !bytes.Equal(r.SpanId, sid[:]) {
|
||||
t.Fatal("missing trace correlation")
|
||||
}
|
||||
if r.GetBody().GetStringValue() != "http.request" || len(r.Attributes) != 1 || r.Attributes[0].Key != "request_id" || r.Attributes[0].Value.GetStringValue() != "req-123" {
|
||||
t.Errorf("unexpected record %v", r)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user