Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log stream responses with progress. #49

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

carlbraganza
Copy link
Contributor

Fixes: #41

@k8s-ci-robot k8s-ci-robot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Oct 8, 2024
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 8, 2024
@carlbraganza
Copy link
Contributor Author

/release-note-none

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Oct 8, 2024
@carlbraganza
Copy link
Contributor Author

/assign @PrasadG193

@carlbraganza
Copy link
Contributor Author

Selective test output illustrating the logging:

=== RUN   TestStreamGetMetadataDeltaResponse/success
I1008 18:53:31.351617 1089848 util.go:362] GetSecretReference namespaceTemplate secret-ns, namespaceParams: map[volumesnapshot.namespace: volumesnapshotcontent.name:snapcontent-snap-2], resolved secret-ns
I1008 18:53:31.351628 1089848 util.go:391] GetSecretReference validated Secret: &SecretReference{Name:secret-name,Namespace:secret-ns,}
I1008 18:53:31.351642 1089848 get_metadata_delta.go:198] "stream response" op="GetMetadataDelta-1" namespace="" baseSnapshotName="snap-1" targetSnapshotName="snap-2" startingOffset=0 maxResults=2 blockMetadataType="FIXED_LENGTH" responseNum=1 volumeCapacityBytes=1073741824 blockMetadata="[{0,1024}]" numBlockMetadata=1
I1008 18:53:31.351649 1089848 get_metadata_delta.go:198] "stream response" op="GetMetadataDelta-1" namespace="" baseSnapshotName="snap-1" targetSnapshotName="snap-2" startingOffset=0 maxResults=2 blockMetadataType="FIXED_LENGTH" responseNum=2 volumeCapacityBytes=1073741824 blockMetadata="[{1,1024}{2,1024}]" numBlockMetadata=2
I1008 18:53:31.351654 1089848 get_metadata_delta.go:159] "stream EOF" op="GetMetadataDelta-1" namespace="" baseSnapshotName="snap-1" targetSnapshotName="snap-2" startingOffset=0 maxResults=2 blockMetadataType="FIXED_LENGTH" lastByteOffset=2 lastSize=1024 lastResponseNum=2 volumeCapacityBytes=1073741824

and

=== RUN   TestStreamGetMetadataAllocatedResponse/success
I1008 18:53:31.349395 1089848 util.go:362] GetSecretReference namespaceTemplate secret-ns, namespaceParams: map[volumesnapshot.namespace: volumesnapshotcontent.name:snapcontent-snap-1], resolved secret-ns
I1008 18:53:31.349419 1089848 util.go:391] GetSecretReference validated Secret: &SecretReference{Name:secret-name,Namespace:secret-ns,}
I1008 18:53:31.349448 1089848 get_metadata_allocated.go:177] "stream response" op="GetMetadataAllocated-1" namespace="" snapshotName="snap-1" startingOffset=0 maxResults=2 blockMetadataType="FIXED_LENGTH" responseNum=1 volumeCapacityBytes=1073741824 blockMetadata="[{0,1024}]" numBlockMetadata=1
I1008 18:53:31.349454 1089848 get_metadata_allocated.go:177] "stream response" op="GetMetadataAllocated-1" namespace="" snapshotName="snap-1" startingOffset=0 maxResults=2 blockMetadataType="FIXED_LENGTH" responseNum=2 volumeCapacityBytes=1073741824 blockMetadata="[{1,1024}{2,1024}]" numBlockMetadata=2
I1008 18:53:31.349459 1089848 get_metadata_allocated.go:138] "stream EOF" op="GetMetadataAllocated-1" namespace="" snapshotName="snap-1" startingOffset=0 maxResults=2 blockMetadataType="FIXED_LENGTH" lastByteOffset=2 lastSize=1024 lastResponseNum=2 volumeCapacityBytes=1073741824

Comment on lines +187 to +191
b.WriteString("[")
for _, bmd := range clientResp.BlockMetadata {
b.WriteString(fmt.Sprintf("{%d,%d}", bmd.ByteOffset, bmd.SizeBytes))
}
b.WriteString("]")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we not use json encoding?

Copy link
Contributor Author

@carlbraganza carlbraganza Oct 10, 2024

Choose a reason for hiding this comment

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

I had first used a JSON dump of the entire data structure but then reconsidered. JSON encoding is very verbose and would repeat the byte_offset and size_bytes labels for each entry. This is more compact and expresses the same data and we don't have to trim the output.

For example, if I replace

b.WriteString("[")
for _, bmd := range clientResp.BlockMetadata {
	b.WriteString(fmt.Sprintf("{%d,%d}", bmd.ByteOffset, bmd.SizeBytes))
}
b.WriteString("]")
...
logger.WithValues(
	...
	"blockMetadata", b.String(),
	...
).Info("stream response")

with

enc := json.NewEncoder(&b)
_ = enc.Encode(clientResp.BlockMetadata)

...
logger.WithValues(
	...
	"blockMetadata", strings.TrimSuffix(b.String(), "\n"),
	...
).Info("stream response")

then the output changes from

blockMetadata="[{1,1024}{2,1024}]"

to

blockMetadata="[{\"byte_offset\":1,\"size_bytes\":1024},{\"byte_offset\":2,\"size_bytes\":1024}]"

Also, the autogenerated field names for JSON do not use camel case but instead use underscores, which looks a bit ugly to me.

@@ -38,7 +38,8 @@ import (
)

const (
HandlerTraceLogLevel = 4
HandlerTraceLogLevel = 4
HandlerDetailedTraceLogLevel = 5
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a detailed trace level for the contents of the stream response loop.

Copy link
Contributor

Choose a reason for hiding this comment

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

@carlbraganza I think the logs level is not specific to handler methods, right? I mean the level can be set in log in any package. Should we give generic names like - debug, info, verbose, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Klog uses a concept of a process wide verbosity level (the V function), and each -v flag increments the level used. There are no clear definitions for info, debug, etc. - these are "severity" values in the log which could be logged at any verbosity level.

I defined these constants for the handler package, with the only constraint being that

1 <= HandlerTraceLogLevel < HandlerDetailedTraceLogLevel

We could define common process wide constants to assign values to these constants later if we see the need, but for now I think most of the business logic is concentrated in the grpc/server package.

@PrasadG193
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 16, 2024
@PrasadG193
Copy link
Contributor

/approve

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 16, 2024
@carlbraganza
Copy link
Contributor Author

I applied the permission change to .prow.sh and removed the invocation of the linter on the test target.

@PrasadG193
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 16, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: carlbraganza, PrasadG193

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [PrasadG193,carlbraganza]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit d36c13e into kubernetes-csi:main Oct 16, 2024
8 checks passed
@carlbraganza carlbraganza deleted the log-progress branch November 12, 2024 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add logging with progress indication in stream handlers
3 participants