Skip to content

Optional tracing in azblob #134

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

Merged
merged 2 commits into from
Apr 16, 2025
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
2 changes: 0 additions & 2 deletions .github/workflows/vulncheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ on:
push:
branches:
- main
schedule: # every Tuesday at 3 a.m UTC
- cron '0 3 * * 2'

permissions:
contents: read # to fetch code (actions/checkout)
Expand Down
6 changes: 3 additions & 3 deletions azblob/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (e *Error) StatusCode() int {
if resp.Body != nil {
defer resp.Body.Close()
}
logger.Sugar.Debugf("Azblob StatusCode %d", resp.StatusCode)
logger.Sugar.Debugf("AZBlob downstream statusCode %d", resp.StatusCode)
return resp.StatusCode
}
if e.statusCode != 0 {
logger.Sugar.Debugf("Return statusCode %d", e.statusCode)
logger.Sugar.Debugf("AZBlob internal statusCode %d", e.statusCode)
return e.statusCode
}
logger.Sugar.Debugf("Return InternalServerError")
logger.Sugar.Debugf("AZBlob InternalServerError: %v", e)
return http.StatusInternalServerError
}

Expand Down
37 changes: 23 additions & 14 deletions azblob/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"context"

azStorageBlob "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"

"github.com/datatrails/go-datatrails-common/tracing"
)

// Count counts the number of blobs filtered by the given tags filter
Expand Down Expand Up @@ -63,8 +61,11 @@ type FilterResponse struct {
//
// Returns all blobs with the specific tag filter.
func (azp *Storer) FilteredList(ctx context.Context, tagsFilter string, opts ...Option) (*FilterResponse, error) {
span, ctx := tracing.StartSpanFromContext(ctx, "FilteredList")
defer span.Finish()
var span Spanner
if azp.startSpanFromContext != nil {
span, ctx = azp.startSpanFromContext(ctx, azp.log, "FilteredList")
defer span.Close()
}

var err error

Expand All @@ -73,7 +74,7 @@ func (azp *Storer) FilteredList(ctx context.Context, tagsFilter string, opts ...
opt(options)
}

if options.listMarker != nil {
if options.listMarker != nil && span != nil {
span.SetTag("marker", *options.listMarker)
}
o := &azStorageBlob.ServiceFilterBlobsOptions{
Expand All @@ -83,7 +84,9 @@ func (azp *Storer) FilteredList(ctx context.Context, tagsFilter string, opts ...

if options.listMaxResults > 0 {
o.MaxResults = &options.listMaxResults
span.SetTag("maxResults", options.listMaxResults)
if span != nil {
span.SetTag("maxResults", options.listMaxResults)
}
}

resp, err := azp.serviceClient.FindBlobsByTags(ctx, o)
Expand All @@ -98,7 +101,7 @@ func (azp *Storer) FilteredList(ctx context.Context, tagsFilter string, opts ...
Items: resp.Blobs,
}

if r.Marker != nil {
if r.Marker != nil && span != nil {
span.SetTag("nextmarker", *r.Marker)
}

Expand All @@ -118,22 +121,26 @@ type ListerResponse struct {

func (azp *Storer) List(ctx context.Context, opts ...Option) (*ListerResponse, error) {

span, ctx := tracing.StartSpanFromContext(ctx, "ListBlobsFlat")
defer span.Finish()

var span Spanner
if azp.startSpanFromContext != nil {
span, ctx = azp.startSpanFromContext(ctx, azp.log, "ListBlobsFlat")
defer span.Close()
}
options := &StorerOptions{}
for _, opt := range opts {
opt(options)
}
if options.listMarker != nil {
if options.listMarker != nil && span != nil {
span.SetTag("marker", *options.listMarker)
}
o := azStorageBlob.ContainerListBlobsFlatOptions{
Marker: options.listMarker,
}
if options.listPrefix != "" {
o.Prefix = &options.listPrefix
span.SetTag("prefix", options.listPrefix)
if span != nil {
span.SetTag("prefix", options.listPrefix)
}
}
if options.listIncludeTags {
o.Include = append(o.Include, azStorageBlob.ListBlobsIncludeItemTags)
Expand All @@ -143,7 +150,9 @@ func (azp *Storer) List(ctx context.Context, opts ...Option) (*ListerResponse, e
}
if options.listMaxResults > 0 {
o.MaxResults = &options.listMaxResults
span.SetTag("maxResults", options.listMaxResults)
if span != nil {
span.SetTag("maxResults", options.listMaxResults)
}
}

// TODO: v1.21 feature which would be great
Expand All @@ -163,7 +172,7 @@ func (azp *Storer) List(ctx context.Context, opts ...Option) (*ListerResponse, e
}

r.Marker = resp.NextMarker
if r.Marker != nil {
if r.Marker != nil && span != nil {
span.SetTag("nextmarker", *r.Marker)
}

Expand Down
32 changes: 18 additions & 14 deletions azblob/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Reader interface {
// example:
//
// url: https://app.datatrails.ai/verifiabledata
func NewReaderNoAuth(url string, opts ...ReaderOption) (Reader, error) {
func NewReaderNoAuth(log Logger, url string, opts ...ReaderOption) (Reader, error) {
var err error
if url == "" {
return nil, errors.New("url is a required parameter and cannot be empty")
Expand All @@ -53,12 +53,14 @@ func NewReaderNoAuth(url string, opts ...ReaderOption) (Reader, error) {
readerOptions := ParseReaderOptions(opts...)

azp := &Storer{
AccountName: readerOptions.accountName, // just for logging
ResourceGroup: "", // just for logging
Subscription: "", // just for logging
Container: readerOptions.container,
credential: nil,
rootURL: url,
AccountName: readerOptions.accountName, // just for logging
ResourceGroup: "", // just for logging
Subscription: "", // just for logging
Container: readerOptions.container,
credential: nil,
rootURL: url,
startSpanFromContext: readerOptions.startSpanFromContext,
log: log,
}

azp.serviceClient, err = azStorageBlob.NewServiceClientWithNoCredential(
Expand Down Expand Up @@ -93,7 +95,7 @@ func NewReaderNoAuth(url string, opts ...ReaderOption) (Reader, error) {

// NewReaderDefaultAuth is a azure blob reader client that obtains credentials from the
// environment - including aad pod identity / workload identity.
func NewReaderDefaultAuth(url string, opts ...ReaderOption) (Reader, error) {
func NewReaderDefaultAuth(log Logger, url string, opts ...ReaderOption) (Reader, error) {
var err error
if url == "" {
return nil, errors.New("url is a required parameter and cannot be empty")
Expand All @@ -102,12 +104,14 @@ func NewReaderDefaultAuth(url string, opts ...ReaderOption) (Reader, error) {
readerOptions := ParseReaderOptions(opts...)

azp := &Storer{
AccountName: readerOptions.accountName, // just for logging
ResourceGroup: "", // just for logging
Subscription: "", // just for logging
Container: readerOptions.container,
credential: nil,
rootURL: url,
AccountName: readerOptions.accountName, // just for logging
ResourceGroup: "", // just for logging
Subscription: "", // just for logging
Container: readerOptions.container,
credential: nil,
rootURL: url,
startSpanFromContext: readerOptions.startSpanFromContext,
log: log,
}

credentials, err := azidentity.NewDefaultAzureCredential(nil)
Expand Down
8 changes: 8 additions & 0 deletions azblob/readeroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type ReaderOptions struct {
accountName string

container string

startSpanFromContext startSpanFromContextFunc
}

type ReaderOption func(*ReaderOptions)
Expand All @@ -25,6 +27,12 @@ func WithContainer(container string) ReaderOption {
}
}

func WithReaderSpanFromContext(s startSpanFromContextFunc) ReaderOption {
return func(a *ReaderOptions) {
a.startSpanFromContext = s
}
}

// ParseReaderOptions parses the given options into a ReaderOptions struct
func ParseReaderOptions(options ...ReaderOption) ReaderOptions {
readerOptions := ReaderOptions{}
Expand Down
12 changes: 12 additions & 0 deletions azblob/spanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package azblob

import (
"context"

"github.com/datatrails/go-datatrails-common/logger"
"github.com/datatrails/go-datatrails-common/spanner"
)

type Spanner = spanner.Spanner

type startSpanFromContextFunc func(context.Context, logger.Logger, string) (spanner.Spanner, context.Context)
19 changes: 14 additions & 5 deletions azblob/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

azStorageBlob "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/datatrails/go-datatrails-common/logger"
)

var (
Expand All @@ -34,6 +33,8 @@ type Storer struct {

log Logger
setReadResponseScannedStatus ReadResponseScannedStatus

startSpanFromContext startSpanFromContextFunc
}

type StorerOption func(*Storer)
Expand All @@ -44,8 +45,15 @@ func WithSetScannedStatus(s ReadResponseScannedStatus) StorerOption {
}
}

func WithStorerSpanFromContext(s startSpanFromContextFunc) StorerOption {
return func(a *Storer) {
a.startSpanFromContext = s
}
}

// New returns new az blob read/write object
func New(
log Logger,
accountName string,
resourceGroup string,
subscription string,
Expand All @@ -54,7 +62,7 @@ func New(
) (*Storer, error) {

var err error
logger.Sugar.Debugf("New Storer: %s/%s/%s/%s",
log.Debugf("New Storer: %s/%s/%s/%s",
accountName,
resourceGroup,
subscription,
Expand All @@ -72,7 +80,7 @@ func New(
rootURL := secret.URL

if container == "" {
logger.Sugar.Infof("Storer: %v", ErrUnspecifiedContainer)
log.Infof("Storer: %v", ErrUnspecifiedContainer)
return nil, ErrUnspecifiedContainer
}
azp := Storer{
Expand All @@ -82,6 +90,7 @@ func New(
Container: container,
credential: credential,
rootURL: rootURL,
log: log,
}
for _, option := range options {
option(&azp)
Expand All @@ -98,12 +107,12 @@ func New(
nil,
)
if err != nil {
logger.Sugar.Infof("unable to create serviceclient %s: %v", azp.containerURL, err)
log.Infof("unable to create serviceclient %s: %v", azp.containerURL, err)
return nil, err
}
azp.containerClient, err = azp.serviceClient.NewContainerClient(container)
if err != nil {
logger.Sugar.Infof("unable to create containerclient %s: %v", container, err)
log.Infof("unable to create containerclient %s: %v", container, err)
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions azblob/storerazurite.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func NewDev(cfg DevConfig, container string) (*Storer, error) {
Container: container,
credential: cred,
rootURL: cfg.URL,
log: logger.Sugar,
}

azp.containerURL = fmt.Sprintf(
Expand Down
Loading
Loading