-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenTelemetry tooling.
- Loading branch information
Showing
9 changed files
with
749 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ctxutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// WithTimeout calls context.WithTimeout and logs details of the | ||
// deadline origin. | ||
func WithDeadline(ctx context.Context, deadline time.Time) (context.Context, context.CancelFunc) { | ||
_, fn, line, _ := runtime.Caller(1) | ||
|
||
logrus.WithContext(ctx). | ||
WithFields(logrus.Fields{ | ||
"deadline": deadline.Format(time.RFC3339), | ||
"source": fmt.Sprintf("%s:%d", fn, line), | ||
}). | ||
Debug("Set context deadline") | ||
|
||
return context.WithDeadline(ctx, deadline) | ||
} | ||
|
||
// WithTimeout calls context.WithTimeout and logs details of the | ||
// deadline origin. | ||
func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { | ||
deadline := time.Now().Add(timeout) | ||
_, fn, line, _ := runtime.Caller(1) | ||
|
||
logrus.WithContext(ctx). | ||
WithFields(logrus.Fields{ | ||
"deadline": deadline.Format(time.RFC3339), | ||
"source": fmt.Sprintf("%s:%d", fn, line), | ||
}). | ||
Debug("Set context deadline") | ||
|
||
return context.WithTimeout(ctx, timeout) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ctxutil_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mailgun/holster/v4/ctxutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWithDeadline(t *testing.T) { | ||
t.Run("Happy path", func(t *testing.T) { | ||
ctx := context.Background() | ||
deadline := time.Now().Add(1 * time.Hour) | ||
ctx2, cancel := ctxutil.WithDeadline(ctx, deadline) | ||
cancel() | ||
require.NotNil(t, ctx2) | ||
}) | ||
} | ||
|
||
func TestWithTimeout(t *testing.T) { | ||
t.Run("Happy path", func(t *testing.T) { | ||
ctx := context.Background() | ||
timeout := 1 * time.Hour | ||
ctx2, cancel := ctxutil.WithTimeout(ctx, timeout) | ||
cancel() | ||
require.NotNil(t, ctx2) | ||
}) | ||
} |
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
Oops, something went wrong.