Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ func (l *sentryLogger) log(ctx context.Context, level Level, severity int, messa
} else if serverAddr, err := os.Hostname(); err == nil {
attrs["sentry.server.address"] = Attribute{Value: serverAddr, Type: "string"}
}
if l.client.options.SendDefaultPII {
scope := hub.Scope()
if scope != nil {
user := scope.user
if !user.IsEmpty() {
if user.ID != "" {
attrs["user.id"] = Attribute{Value: user.ID, Type: "string"}
}
if user.Name != "" {
attrs["user.name"] = Attribute{Value: user.Name, Type: "string"}
}
if user.Email != "" {
attrs["user.email"] = Attribute{Value: user.Email, Type: "string"}
}
}
}
}
if spanID.String() != "0000000000000000" {
attrs["sentry.trace.parent_span_id"] = Attribute{Value: spanID.String(), Type: "string"}
}
Expand Down
119 changes: 119 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,122 @@ func TestSentryLogger_DebugLogging(t *testing.T) {
})
}
}

func Test_sentryLogger_SendDefaultPII(t *testing.T) {
ctx := context.Background()
mockTransport := &MockTransport{}
mockClient, _ := NewClient(ClientOptions{
Dsn: testDsn,
Transport: mockTransport,
Release: "v1.2.3",
Environment: "testing",
ServerName: "test-server",
EnableLogs: true,
EnableTracing: true,
SendDefaultPII: true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should also have a test that does the reverse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

})
mockClient.sdkIdentifier = "sentry.go"
mockClient.sdkVersion = "0.10.0"
hub := CurrentHub()
hub.BindClient(mockClient)
hub.Scope().propagationContext.TraceID = TraceIDFromHex(LogTraceID)

hub.Scope().SetUser(User{
ID: "user123",
Name: "Test User",
Email: "test@example.com",
})

ctx = SetHubOnContext(ctx, hub)

l := NewLogger(ctx)
l.Info(ctx, "test message with PII")
Flush(20 * time.Millisecond)

events := mockTransport.Events()
if len(events) != 1 {
t.Fatalf("expected 1 event, got %d", len(events))
}

logs := events[0].Logs
if len(logs) != 1 {
t.Fatalf("expected 1 log, got %d", len(logs))
}

log := logs[0]
attrs := log.Attributes

if val, ok := attrs["user.id"]; !ok {
t.Error("missing user.id attribute")
} else if val.Value != "user123" {
t.Errorf("unexpected user.id: got %v, want %v", val.Value, "user123")
}

if val, ok := attrs["user.name"]; !ok {
t.Error("missing user.name attribute")
} else if val.Value != "Test User" {
t.Errorf("unexpected user.name: got %v, want %v", val.Value, "Test User")
}

if val, ok := attrs["user.email"]; !ok {
t.Error("missing user.email attribute")
} else if val.Value != "test@example.com" {
t.Errorf("unexpected user.email: got %v, want %v", val.Value, "test@example.com")
}
}

func Test_sentryLogger_NoUserInfoWithSendDefaultPII_False(t *testing.T) {
ctx := context.Background()
mockTransport := &MockTransport{}
mockClient, _ := NewClient(ClientOptions{
Dsn: testDsn,
Transport: mockTransport,
Release: "v1.2.3",
Environment: "testing",
ServerName: "test-server",
EnableLogs: true,
EnableTracing: true,
})
mockClient.sdkIdentifier = "sentry.go"
mockClient.sdkVersion = "0.10.0"
hub := CurrentHub()
hub.BindClient(mockClient)
hub.Scope().propagationContext.TraceID = TraceIDFromHex(LogTraceID)

hub.Scope().SetUser(User{
ID: "user123",
Name: "Test User",
Email: "test@example.com",
})

ctx = SetHubOnContext(ctx, hub)

l := NewLogger(ctx)
l.Info(ctx, "test message with PII")
Flush(20 * time.Millisecond)

events := mockTransport.Events()
if len(events) != 1 {
t.Fatalf("expected 1 event, got %d", len(events))
}

logs := events[0].Logs
if len(logs) != 1 {
t.Fatalf("expected 1 log, got %d", len(logs))
}

log := logs[0]
attrs := log.Attributes

if _, ok := attrs["user.id"]; ok {
t.Error("unexpected user.id attribute")
}

if _, ok := attrs["user.name"]; ok {
t.Error("unexpected user.name attribute")
}

if _, ok := attrs["user.email"]; ok {
t.Error("unexpected user.email attribute")
}
}
Loading