This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed trace package, need refinement
Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
- Loading branch information
1 parent
663d240
commit ad4a8d9
Showing
3 changed files
with
104 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
// executableTraceHookKey is a value key used to retrieve the trace hook function from Context. | ||
// executableTraceHookKey is a value key used to retrieve the ExecutableTrace | ||
// from Context. | ||
type executableTraceHookKey struct{} | ||
|
||
// GetTraceHookFromContext returns the trace hook function associated with the | ||
// provided context. If none, it returns nil. | ||
func GetTraceHookFromContext(ctx context.Context) func(executableName string, action string) { | ||
trace, _ := ctx.Value(executableTraceHookKey{}).(func(string, string)) | ||
// ExecutableTrace is a set of hooks used to trace the execution of binary | ||
// executables. | ||
type ExecutableTrace struct { | ||
// ExecuteStart is called before the execution of an executable. | ||
ExecuteStart func(executableName string, action string) | ||
// ExecuteEnd is called after the execution of an executable completes. | ||
ExecuteDone func(executableName string, action string) | ||
} | ||
|
||
// GetTraceHookFromContext returns the ExecutableTrace associated with the context. | ||
// If none, it returns nil. | ||
func GetTraceHookFromContext(ctx context.Context) *ExecutableTrace { | ||
trace, _ := ctx.Value(executableTraceHookKey{}).(*ExecutableTrace) | ||
return trace | ||
} | ||
|
||
// NewContextWithTraceHook takes a Context and a trace hook function of type | ||
// func(executableName string, action string), and returns a new Context with | ||
// the hook added as a Value. | ||
func NewContextWithTraceHook(ctx context.Context, hook func(executableName string, action string)) context.Context { | ||
if hook == nil { | ||
panic("nil trace") | ||
// WithTraceHook takes a Context and an ExecutableTrace, and returns a Context with | ||
// the ExecutableTrace added as a Value. | ||
func WithTraceHook(ctx context.Context, trace *ExecutableTrace) (context.Context, error) { | ||
if trace == nil { | ||
return nil, errors.New("empty ExecutableTrace") | ||
} | ||
ctx = context.WithValue(ctx, executableTraceHookKey{}, hook) | ||
return ctx | ||
ctx = context.WithValue(ctx, executableTraceHookKey{}, trace) | ||
return ctx, nil | ||
} |