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

move datanode to internal/node #70

Closed
wants to merge 2 commits into from
Closed
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: 3 additions & 2 deletions clog/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"

"github.com/alcionai/clues"
"github.com/alcionai/clues/internal/node"
"github.com/alcionai/clues/internal/stringify"
otellog "go.opentelemetry.io/otel/log"
"go.uber.org/zap"
Expand Down Expand Up @@ -86,15 +87,15 @@ func (b builder) log(l logLevel, msg string) {
for k, v := range cv {
zsl = zsl.With(k, v)

attr := clues.NewAttribute(k, v)
attr := node.NewAttribute(k, v)
record.AddAttributes(attr.KV())
}

// plus any values added using builder.With()
for k, v := range b.with {
zsl.With(k, v)

attr := clues.NewAttribute(stringify.Fmt(k)[0], v)
attr := node.NewAttribute(stringify.Fmt(k)[0], v)
record.AddAttributes(attr.KV())
}

Expand Down
77 changes: 44 additions & 33 deletions clues.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clues
import (
"context"

"github.com/alcionai/clues/internal/node"
"github.com/alcionai/clues/internal/stringify"
)

Expand All @@ -19,25 +20,25 @@ import (
func Initialize(
ctx context.Context,
serviceName string,
config OTELConfig,
config node.OTELConfig,
) (context.Context, error) {
nc := nodeFromCtx(ctx)
nc := node.FromCtx(ctx)

err := nc.init(ctx, serviceName, config)
err := nc.Init(ctx, serviceName, config)
if err != nil {
return ctx, err
}

return setNodeInCtx(ctx, nc), nil
return node.EmbedInCtx(ctx, nc), nil
}

// Close will flush all buffered data waiting to be read. If Initialize was not
// called, this call is a no-op. Should be called in a defer after initializing.
func Close(ctx context.Context) error {
nc := nodeFromCtx(ctx)
nc := node.FromCtx(ctx)

if nc.otel != nil {
err := nc.otel.close(ctx)
if nc.OTEL != nil {
err := nc.OTEL.Close(ctx)
if err != nil {
return Wrap(err, "closing otel client")
}
Expand All @@ -46,29 +47,40 @@ func Close(ctx context.Context) error {
return nil
}

// ---------------------------------------------------------------------------
// data access
// ---------------------------------------------------------------------------

// In retrieves the clues structured data from the context.
// TODO: turn return an interface instead of a node, have nodes
// and errors both comply with that wrapper.
func In(ctx context.Context) *node.Node {
return node.FromCtx(ctx)
}

// ---------------------------------------------------------------------------
// key-value metadata
// ---------------------------------------------------------------------------

// Add adds all key-value pairs to the clues.
func Add(ctx context.Context, kvs ...any) context.Context {
nc := nodeFromCtx(ctx)
return setNodeInCtx(ctx, nc.addValues(stringify.Normalize(kvs...)))
nc := node.FromCtx(ctx)
return node.EmbedInCtx(ctx, nc.AddValues(stringify.Normalize(kvs...)))
}

// AddMap adds a shallow clone of the map to a namespaced set of clues.
func AddMap[K comparable, V any](
ctx context.Context,
m map[K]V,
) context.Context {
nc := nodeFromCtx(ctx)
nc := node.FromCtx(ctx)

kvs := make([]any, 0, len(m)*2)
for k, v := range m {
kvs = append(kvs, k, v)
}

return setNodeInCtx(ctx, nc.addValues(stringify.Normalize(kvs...)))
return node.EmbedInCtx(ctx, nc.AddValues(stringify.Normalize(kvs...)))
}

// ---------------------------------------------------------------------------
Expand All @@ -86,29 +98,28 @@ func AddSpan(
name string,
kvs ...any,
) context.Context {
nc := nodeFromCtx(ctx)
nc := node.FromCtx(ctx)

var node *dataNode
var spanned *node.Node

if len(kvs) > 0 {
ctx, node = nc.addSpan(ctx, name)
node.id = name
node = node.addValues(stringify.Normalize(kvs...))
ctx, spanned = nc.AddSpan(ctx, name)
spanned.ID = name
spanned = spanned.AddValues(stringify.Normalize(kvs...))
} else {
ctx, node = nc.addSpan(ctx, name)
node = node.trace(name)
ctx, spanned = nc.AddSpan(ctx, name)
spanned = spanned.AppendToTree(name)
}

return setNodeInCtx(ctx, node)
return node.EmbedInCtx(ctx, spanned)
}

// CloseSpan closes the current span in the clues node. Should only be called
// following a `clues.AddSpan()` call.
func CloseSpan(ctx context.Context) context.Context {
nc := nodeFromCtx(ctx)
node := nc.closeSpan(ctx)

return setNodeInCtx(ctx, node)
return node.EmbedInCtx(
ctx,
node.FromCtx(ctx).CloseSpan(ctx))
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -141,10 +152,10 @@ func AddComment(
msg string,
vs ...any,
) context.Context {
nc := nodeFromCtx(ctx)
nn := nc.addComment(1, msg, vs...)
nc := node.FromCtx(ctx)
nn := nc.AddComment(1, msg, vs...)

return setNodeInCtx(ctx, nn)
return node.EmbedInCtx(ctx, nn)
}

// ---------------------------------------------------------------------------
Expand All @@ -168,27 +179,27 @@ func AddAgent(
ctx context.Context,
name string,
) context.Context {
nc := nodeFromCtx(ctx)
nn := nc.addAgent(name)
nc := node.FromCtx(ctx)
nn := nc.AddAgent(name)

return setNodeInCtx(ctx, nn)
return node.EmbedInCtx(ctx, nn)
}

// Relay adds all key-value pairs to the provided agent. The agent will
// record those values to the dataNode in which it was created. All relayed
// record those values to the node in which it was created. All relayed
// values are namespaced to the owning agent.
func Relay(
ctx context.Context,
agent string,
vs ...any,
) {
nc := nodeFromCtx(ctx)
ag, ok := nc.agents[agent]
nc := node.FromCtx(ctx)
ag, ok := nc.Agents[agent]

if !ok {
return
}

// set values, not add. We don't want agents to own a full clues tree.
ag.data.setValues(stringify.Normalize(vs...))
ag.Data.SetValues(stringify.Normalize(vs...))
}
8 changes: 5 additions & 3 deletions clues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/alcionai/clues"
"github.com/alcionai/clues/cecrets"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -269,9 +270,10 @@ func TestAddSpan(t *testing.T) {
ctx := context.Background()

if init {
ictx, err := clues.Initialize(ctx, test.name, clues.OTELConfig{
GRPCEndpoint: "localhost:4317",
})
ocfg, err := clues.ConfigOTEL("localhost:4317")
require.NoError(t, err, clues.ToCore(err))

ictx, err := clues.Initialize(ctx, test.name, ocfg)
if err != nil {
t.Error("initializing clues", err)
return
Expand Down
Loading
Loading