Skip to content
Open
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
29 changes: 29 additions & 0 deletions docs/ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,35 @@ foreach (var s in summaries)

---

## SDK Integration Header

When using the managed client, the `X-Weaviate-Client-Integration` header is automatically sent with every request so the Weaviate server can identify and track managed client traffic in metrics.

### DI path

`AddWeaviateContext` sets the header automatically. If you are building a higher-level framework on top of the managed client, append your own identity at the core DI layer:

```csharp
// X-Weaviate-Client-Integration: weaviate-client-csharp-managed/1.x.x my-framework/2.3.0
builder.Services.AddWeaviate(opts =>
opts.AddIntegration("my-framework/2.3.0"));
builder.Services.AddWeaviateContext<MyContext>();
```

### Non-DI path

When constructing `WeaviateContext` directly (without DI), call `WithManagedIntegrationHeader()` on your `ClientConfiguration`:

```csharp
var config = new ClientConfiguration("localhost")
.WithManagedIntegrationHeader();

var client = new WeaviateClient(config);
var context = new MyContext(client);
```

---

## Collection Lifecycle Hooks

The `OnCollectionConfig` pattern allows intercepting collection creation.
Expand Down
13 changes: 13 additions & 0 deletions docs/DEPENDENCY_INJECTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ builder.Services.AddWeaviateContext<BlogContext>(
);
```

### SDK Integrations (X-Weaviate-Client-Integration Header)

When `AddWeaviateContext` is called, it automatically sets the `X-Weaviate-Client-Integration` header so the Weaviate server can identify traffic from the managed client.

If you are building a higher-level SDK or framework on top of `Weaviate.Client.Managed`, append your own identity at the core DI layer via `AddWeaviate()`. Multiple tokens are space-separated in the header value:

```csharp
// Results in: X-Weaviate-Client-Integration: weaviate-client-csharp-managed/1.x.x my-framework/2.3.0
builder.Services.AddWeaviate(opts =>
opts.AddIntegration("my-framework/2.3.0"));
builder.Services.AddWeaviateContext<BlogContext>();
```

### OnConfiguring Override

Context instances can override `OnConfiguring()` to set defaults, which take precedence over DI configuration:
Expand Down
7 changes: 7 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="/tmp/local-nuget" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
6 changes: 3 additions & 3 deletions src/Example/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@
},
"Weaviate.Client": {
"type": "Transitive",
"resolved": "1.0.1",
"contentHash": "Ff7/q5DpshU532DDZJ5Zh86G1nxnlO6zwTqzo4x/UOBY1s3zLRfbNyArQ012gMHtxx/odpULgi2Gld7zfwVI5A==",
"resolved": "1.0.2",
"contentHash": "B/QmsqYDf/R2m3/GJtg+SuKStg/mQ6Dkv1C5KcROLyqpyt96gH3RWLUFGB6SKFxhJAAyBEp/+Hk+eSFOcoShSg==",
"dependencies": {
"Duende.IdentityModel": "7.1.0",
"Google.Protobuf": "3.30.2",
Expand All @@ -383,7 +383,7 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "[9.0.8, )",
"Microsoft.Extensions.Hosting.Abstractions": "[9.0.8, )",
"Microsoft.Extensions.Options": "[9.0.8, )",
"Weaviate.Client": "[1.0.1, )"
"Weaviate.Client": "[1.0.2, )"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Weaviate.Client.DependencyInjection;
using Xunit;

Expand Down Expand Up @@ -233,6 +234,24 @@ public void AddWeaviateContext_ExposesUnderlyingClient()
Assert.Same(client, context.Client);
}

[Fact]
public void AddWeaviateContext_SetsIntegrationHeader()
{
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();

var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<WeaviateOptions>>().Value;

Assert.NotNull(options.Headers);
Assert.True(options.Headers.ContainsKey("X-Weaviate-Client-Integration"));
Assert.Matches(
@"^weaviate-client-csharp-managed/\d+",
options.Headers["X-Weaviate-Client-Integration"]
);
}

#region Test Types

[WeaviateCollection("Products")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Weaviate.Client.Managed.Extensions;
using Xunit;

namespace Weaviate.Client.Managed.Tests.Extensions;

public class WeaviateClientExtensionsTests
{
[Fact]
public void WithManagedIntegrationHeader_AddsHeader()
{
var config = new ClientConfiguration();
var result = config.WithManagedIntegrationHeader();

Assert.NotNull(result.Headers);
Assert.True(result.Headers.ContainsKey(WeaviateDefaults.IntegrationHeader));
Assert.Matches(
@"^weaviate-client-csharp-managed/\d+",
result.Headers[WeaviateDefaults.IntegrationHeader]
);
}

[Fact]
public void WithManagedIntegrationHeader_DoesNotOverwriteExistingValue()
{
var config = new ClientConfiguration(
Headers: new Dictionary<string, string>
{
[WeaviateDefaults.IntegrationHeader] = "existing/1.0",
}
);
var result = config.WithManagedIntegrationHeader();

var value = result.Headers![WeaviateDefaults.IntegrationHeader];
// Existing value is preserved; managed segment is appended
Assert.Contains("existing/1.0", value);
Assert.Contains("weaviate-client-csharp-managed/", value);
}

[Fact]
public void WithManagedIntegrationHeader_DoesNotMutateOriginal()
{
var config = new ClientConfiguration();
var result = config.WithManagedIntegrationHeader();

// Original is unchanged (record with syntax returns new instance)
Assert.Null(config.Headers);
Assert.NotNull(result.Headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<ItemGroup>
<ProjectReference Include="..\Weaviate.Client.Managed\Weaviate.Client.Managed.csproj" />
<PackageReference Include="Weaviate.Client" Version="1.0.1" />
<PackageReference Include="Weaviate.Client" Version="1.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Weaviate.Client.Managed.Context;
using Weaviate.Client.DependencyInjection;

namespace Weaviate.Client.Managed.DependencyInjection;

Expand All @@ -12,6 +12,11 @@ namespace Weaviate.Client.Managed.DependencyInjection;
/// </summary>
public static class WeaviateManagedServiceCollectionExtensions
{
/// <summary>
/// The integration package name for the managed client.
/// </summary>
internal const string IntegrationName = "weaviate-client-csharp-managed";

/// <summary>
/// Registers a <see cref="WeaviateContext"/> subclass with the dependency injection container.
/// </summary>
Expand All @@ -32,6 +37,14 @@ public static IServiceCollection AddWeaviateContext<TContext>(
)
where TContext : WeaviateContext
{
// Append managed client identity to X-Weaviate-Client-Integration via IConfigureOptions
// so it is applied before WeaviateClient constructs its HttpClient/gRPC client.
services
.AddOptions<WeaviateOptions>()
.Configure(opts =>
opts.AddIntegration(WeaviateDefaults.IntegrationAgent(IntegrationName))
);

// Configure typed options for this context type
if (configureOptions != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ namespace Weaviate.Client.Managed.Extensions;
/// </summary>
public static class WeaviateClientExtensions
{
/// <summary>
/// Returns a new <see cref="ClientConfiguration"/> with the
/// <c>X-Weaviate-Client-Integration</c> header set for the managed client.
/// Use this when constructing <see cref="WeaviateContext"/> without dependency injection.
/// </summary>
/// <param name="config">The base configuration.</param>
public static ClientConfiguration WithManagedIntegrationHeader(
this ClientConfiguration config
) =>
config.WithIntegration(
WeaviateDefaults.IntegrationAgent(
DependencyInjection.WeaviateManagedServiceCollectionExtensions.IntegrationName
)
);

/// <summary>
/// Creates a Weaviate collection from a C# class decorated with ORM attributes.
/// The class must have a [WeaviateCollection] attribute or the class name will be used as the collection name.
Expand Down
1 change: 1 addition & 0 deletions src/Weaviate.Client.Managed/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ static Weaviate.Client.Managed.Query.WeaviateQueryableExtensions.WithMetadata<T>
static Weaviate.Client.Managed.Query.WeaviateQueryableExtensions.WithReferences<T>(this System.Linq.IQueryable<T!>! source, params System.Linq.Expressions.Expression<System.Func<T!, object!>!>![]! references) -> Weaviate.Client.Managed.Query.WeaviateQueryable<T!>!
static Weaviate.Client.Managed.Query.WeaviateQueryableExtensions.WithCancellation<T>(this System.Linq.IQueryable<T!>! source, System.Threading.CancellationToken cancellationToken) -> Weaviate.Client.Managed.Query.WeaviateQueryable<T!>!
static Weaviate.Client.Managed.Query.WeaviateQueryableExtensions.WithVectors<T>(this System.Linq.IQueryable<T!>! source, params System.Linq.Expressions.Expression<System.Func<T!, object!>!>![]! vectors) -> Weaviate.Client.Managed.Query.WeaviateQueryable<T!>!
static Weaviate.Client.Managed.Extensions.WeaviateClientExtensions.WithManagedIntegrationHeader(this Weaviate.Client.ClientConfiguration! config) -> Weaviate.Client.ClientConfiguration!
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Weaviate.Client" Version="1.0.1" />
<PackageReference Include="Weaviate.Client" Version="1.0.2" />
</ItemGroup>

<!-- Include Roslyn analyzers in the package -->
Expand Down
Loading