-
Notifications
You must be signed in to change notification settings - Fork 195
feat(otel): client-side redaction of PII and/or secrets #584
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
Open
simonvdk-mistral
wants to merge
12
commits into
main
Choose a base branch
from
svdk/feat/client_side_masking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e090ffd
feat: first shot
simonvdk-mistral 7e82903
fix: tweaks
simonvdk-mistral e0e910c
fix: resolve redaction and policy changes
simonvdk-mistral eb46713
feat: add secret regexes to default redaction patterns
simonvdk-mistral 899129c
refactor: type RedactingSpanExporter against SpanExporter base
simonvdk-mistral cd0b13b
feat: redact string elements of homogeneous sequences
simonvdk-mistral 48d6369
test: convert redaction tests to pytest style
simonvdk-mistral 2dfee80
fix: tweaks
simonvdk-mistral 5adab3b
fix: additional sensitive keys
simonvdk-mistral 5588e01
docs: add observability docs
simonvdk-mistral e8f0122
feat: more secret patterns
simonvdk-mistral 3ca7da8
feat: make RegexRedactionPolicy the default redaction policy
simonvdk-mistral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,31 @@ | ||
| #!/usr/bin/env python | ||
| """Dedicated telemetry mode. | ||
|
|
||
| The SDK creates and owns an OTLP exporter that ships spans to the Mistral | ||
| telemetry endpoint. Spans are redacted before export. | ||
|
|
||
| Requires the telemetry extra: pip install "mistralai[telemetry]" | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from mistralai.client import Mistral | ||
| from mistralai.extra.observability import configure_telemetry | ||
|
|
||
|
|
||
| def main() -> None: | ||
| api_key = os.environ["MISTRAL_API_KEY"] | ||
|
|
||
| with Mistral(api_key=api_key) as client: | ||
| # Dedicated mode is the default; redaction is on by default. | ||
| configure_telemetry(client) | ||
|
|
||
| response = client.chat.complete( | ||
| model="mistral-small-latest", | ||
| messages=[{"role": "user", "content": "What is the best French cheese?"}], | ||
| ) | ||
| print(response.choices[0].message.content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
44 changes: 44 additions & 0 deletions
44
examples/mistral/observability/global_provider_with_redaction.py
This file contains hidden or 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,44 @@ | ||
| #!/usr/bin/env python | ||
| """Global provider mode with application-owned redaction. | ||
|
|
||
| In global (or custom TracerProvider) mode your application owns the OTEL export | ||
| pipeline, so `configure_telemetry`'s redaction argument is ignored. To redact | ||
| spans you wrap your own exporter with RedactingSpanExporter. | ||
|
|
||
| Requires the telemetry extra: pip install "mistralai[telemetry]" | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| from mistralai.client import Mistral | ||
| from mistralai.extra.observability import RedactingSpanExporter, configure_telemetry | ||
|
|
||
|
|
||
| def main() -> None: | ||
| api_key = os.environ["MISTRAL_API_KEY"] | ||
|
|
||
| # Build your own provider and wrap the exporter with redaction. | ||
| provider = TracerProvider() | ||
| provider.add_span_processor( | ||
| BatchSpanProcessor(RedactingSpanExporter(OTLPSpanExporter())) | ||
| ) | ||
| trace.set_tracer_provider(provider) | ||
|
|
||
| with Mistral(api_key=api_key) as client: | ||
| # SDK spans flow through the global provider configured above. | ||
| configure_telemetry(client, provider="global") | ||
|
|
||
| response = client.chat.complete( | ||
| model="mistral-small-latest", | ||
| messages=[{"role": "user", "content": "Say hello."}], | ||
| ) | ||
| print(response.choices[0].message.content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or 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,40 @@ | ||
| #!/usr/bin/env python | ||
| """Choosing a redaction policy in dedicated telemetry mode. | ||
|
|
||
| The `redaction` argument accepts: | ||
| - True (default): the regex (content-oriented) policy | ||
| - False: redaction disabled | ||
| - a RedactionPolicy instance (e.g. AttributeRedactionPolicy) | ||
| - a (key, value) -> value | None callback | ||
|
|
||
| Requires the telemetry extra: pip install "mistralai[telemetry]" | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from mistralai.client import Mistral | ||
| from mistralai.extra.observability import AttributeRedactionPolicy, configure_telemetry | ||
|
|
||
|
|
||
| def main() -> None: | ||
| api_key = os.environ["MISTRAL_API_KEY"] | ||
|
|
||
| with Mistral(api_key=api_key) as client: | ||
| configure_telemetry(client, redaction=AttributeRedactionPolicy()) | ||
|
|
||
| # Alternatives: | ||
| # configure_telemetry(client, redaction=False) # disable entirely | ||
| # configure_telemetry( # custom callback | ||
| # client, | ||
| # redaction=lambda key, value: None if "email" in key else value, | ||
| # ) | ||
|
|
||
| response = client.chat.complete( | ||
| model="mistral-small-latest", | ||
| messages=[{"role": "user", "content": "Say hello."}], | ||
| ) | ||
| print(response.choices[0].message.content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the various examples, worth showing how to extend the defaults and provide an example where an attribute will be redacted? e.g.