Skip to content
Open
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
5 changes: 5 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2022 The zapcl Authors
// SPDX-License-Identifier: BSD-3-Clause

// Package zapcloudlogging provides the Cloud Logging integration for Zap.
package zapcl
71 changes: 71 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
logtypepb "google.golang.org/genproto/googleapis/logging/type"
"google.golang.org/protobuf/testing/protocmp"
)

func TestHTTP(t *testing.T) {
t.Parallel()

req := &HTTPPayload{}
field := HTTP(req)

if diff := cmp.Diff(field, zap.Object("httpRequest", req)); diff != "" {
t.Fatalf("(-want, +got)\n%s\n", diff)
}
}

func TestHTTPRequestField(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -189,3 +202,61 @@ func TestHTTPRequestField(t *testing.T) {
})
}
}

func TestHTTPPayload_MarshalLogObject(t *testing.T) {
t.Parallel()

req := httptest.NewRequest("POST", "/", strings.NewReader("12345"))
req.Header.Set("User-Agent", "test-user-agent")
req.Header.Set("Referer", "test-referer")
resp := &http.Response{
Body: io.NopCloser(strings.NewReader("6789101112")),
StatusCode: 200,
}
data := NewHTTPRequest(req, resp)

enc := zapcore.NewMapObjectEncoder()
if err := data.MarshalLogObject(enc); err != nil {
t.Fatal(err)
}

if gotMethod, want := enc.Fields["requestMethod"], http.MethodPost; gotMethod != want {
t.Fatalf("got %s but want %s", gotMethod, want)
}

if gotRequestURL, want := enc.Fields["requestUrl"], "/"; gotRequestURL != want {
t.Fatalf("got %s but want %s", gotRequestURL, want)
}

if gotRequestSize, want := enc.Fields["requestSize"], int64(5); gotRequestSize != want {
t.Fatalf("got %d but want %d", gotRequestSize, want)
}

if gotResponseSize, want := enc.Fields["responseSize"], int64(10); gotResponseSize != want {
t.Fatalf("got %d but want %d", gotResponseSize, want)
}

if gotUserAgent, want := enc.Fields["userAgent"], "test-user-agent"; gotUserAgent != want {
t.Fatalf("got %s but want %s", gotUserAgent, want)
}

if gotRemoteIP, want := enc.Fields["remoteIp"], "192.0.2.1:1234"; gotRemoteIP != want {
t.Fatalf("got %s but want %s", gotRemoteIP, want)
}

if gotServerID, want := enc.Fields["serverIp"], ""; gotServerID != want {
t.Fatalf("got %s but want %s", gotServerID, want)
}

if gotReferer, want := enc.Fields["referer"], "test-referer"; gotReferer != want {
t.Fatalf("got %s but want %s", gotReferer, want)
}

if gotProtocol, want := enc.Fields["protocol"], "HTTP/1.1"; gotProtocol != want {
t.Fatalf("got %s but want %s", gotProtocol, want)
}

if gotStatus, want := enc.Fields["status"], int32(http.StatusOK); gotStatus != want {
t.Fatalf("got %d but want %d", gotStatus, want)
}
}
34 changes: 34 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 The zapcl Authors
// SPDX-License-Identifier: BSD-3-Clause

package zapcl

import (
"go.uber.org/zap/zapcore"
)

// Option configures a core.
type Option interface {
apply(*core)
}

// optionFunc wraps a func so it satisfies the Option interface.
type optionFunc func(*core)

func (f optionFunc) apply(c *core) {
f(c)
}

// WithInitialFields configures the zap InitialFields.
func WithInitialFields(fields map[string]any) Option {
return optionFunc(func(c *core) {
c.initFields = fields
})
}

// WithWriteSyncer configures the zapcore.WriteSyncer.
func WithWriteSyncer(ws zapcore.WriteSyncer) Option {
return optionFunc(func(c *core) {
c.ws = ws
})
}
10 changes: 10 additions & 0 deletions pkg/detector/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestCloudPlatformAppEngineStandard(t *testing.T) {
t.Parallel()

d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvAppEngineFlexService: "foo",
Expand All @@ -22,6 +24,8 @@ func TestCloudPlatformAppEngineStandard(t *testing.T) {
}

func TestCloudPlatformAppEngineFlex(t *testing.T) {
t.Parallel()

d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvAppEngineFlexService: "foo",
Expand All @@ -37,6 +41,8 @@ func TestCloudPlatformAppEngineFlex(t *testing.T) {
}

func TestCloudPlatformCloudRun(t *testing.T) {
t.Parallel()

d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvCloudRunService: "foo",
Expand All @@ -52,6 +58,8 @@ func TestCloudPlatformCloudRun(t *testing.T) {
}

func TestCloudPlatformCloudRunJobs(t *testing.T) {
t.Parallel()

d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvCloudRunJobsService: "foo",
Expand All @@ -66,6 +74,8 @@ func TestCloudPlatformCloudRunJobs(t *testing.T) {
}

func TestCloudPlatformCloudFunctions(t *testing.T) {
t.Parallel()

d := NewDetector(&fakeResourceGetter{
envVars: map[string]string{
EnvCloudFunctionsTarget: "foo",
Expand Down
2 changes: 1 addition & 1 deletion pkg/detector/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package detector

// fakeResourceGetter mocks internal.ResourceAtttributesGetter interface to retrieve env vars and metadata
// fakeResourceGetter mocks internal.ResourceAtttributesGetter interface to retrieve env vars and metadata.
type fakeResourceGetter struct {
envVars map[string]string
metaVars map[string]string
Expand Down
2 changes: 2 additions & 0 deletions pkg/monitoredresource/monitoredresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,8 @@ func (r *Resource) Region() string {
}

// isMetadataActive queries valid response on "/computeMetadata/v1/" URL.
//
//nolint:unused
func (r *Resource) isMetadataActive() bool {
data := r.attrs.Metadata("")

Expand Down
5 changes: 3 additions & 2 deletions pkg/monitoredresource/monitoredresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
instanceID = "test-instance-12345"
)

// fakeResourceGetter mocks internal.ResourceAtttributesGetter interface to retrieve env vars and metadata
// fakeResourceGetter mocks internal.ResourceAtttributesGetter interface to retrieve env vars and metadata.
type fakeResourceGetter struct {
envVars map[string]string
metaVars map[string]string
Expand Down Expand Up @@ -75,7 +75,7 @@ func (g *fakeResourceGetter) ReadAll(path string) string {
return ""
}

// setupDetectResource resets sync.Once on detectResource and enforces mocked resource attribute getter
// setupDetectResource resets sync.Once on detectResource and enforces mocked resource attribute getter.
func setupDetectedResource(envVars, metaVars, fsPaths map[string]string) {
ResourceDetector.once = new(sync.Once)
fake := &fakeResourceGetter{
Expand All @@ -87,6 +87,7 @@ func setupDetectedResource(envVars, metaVars, fsPaths map[string]string) {
ResourceDetector.pb = nil
}

//nolint:paralleltest,govet
func TestResourceDetection(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading