forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add other azure samples (Azure#24360)
* chat * phone * sms * keys * other * link
- Loading branch information
Showing
7 changed files
with
532 additions
and
80 deletions.
There are no files selected for viewing
280 changes: 200 additions & 80 deletions
280
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_app_config.py
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,50 @@ | ||
""" | ||
Examples to show usage of the azure-core-tracing-opentelemetry | ||
with the App Configuration SDK and exporting to | ||
Azure monitor backend. This example traces calls for creating | ||
a configuration setting via the App Configuration sdk. The telemetry | ||
will be collected automatically and sent to Application Insights | ||
via the AzureMonitorTraceExporter | ||
""" | ||
|
||
import os | ||
|
||
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs | ||
from azure.core.settings import settings | ||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
|
||
settings.tracing_implementation = OpenTelemetrySpan | ||
|
||
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python | ||
# for details | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
trace.set_tracer_provider(TracerProvider()) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# azure monitor trace exporter to send telemetry to appinsights | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
span_processor = BatchSpanProcessor( | ||
AzureMonitorTraceExporter.from_connection_string( | ||
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
) | ||
) | ||
trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
||
# Example with App Configs SDKs | ||
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting | ||
|
||
connection_str = "<connection_string>" | ||
client = AzureAppConfigurationClient.from_connection_string(connection_str) | ||
|
||
with tracer.start_as_current_span(name="AppConfig"): | ||
config_setting = ConfigurationSetting( | ||
key="MyKey", | ||
label="MyLabel", | ||
value="my value", | ||
content_type="my content type", | ||
tags={"my tag": "my tag value"} | ||
) | ||
added_config_setting = client.add_configuration_setting(config_setting) |
49 changes: 49 additions & 0 deletions
49
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_form_recognizer.py
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,49 @@ | ||
""" | ||
Examples to show usage of the azure-core-tracing-opentelemetry | ||
with the Form Recognizer SDK and exporting to | ||
Azure monitor backend. This example traces calls for extracting | ||
the layout of a document via the Form Recognizer sdk. The telemetry | ||
will be collected automatically and sent to Application Insights | ||
via the AzureMonitorTraceExporter | ||
""" | ||
|
||
import os | ||
|
||
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs | ||
from azure.core.settings import settings | ||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
|
||
settings.tracing_implementation = OpenTelemetrySpan | ||
|
||
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python | ||
# for details | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
trace.set_tracer_provider(TracerProvider()) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# azure monitor trace exporter to send telemetry to appinsights | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
span_processor = BatchSpanProcessor( | ||
AzureMonitorTraceExporter.from_connection_string( | ||
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
) | ||
) | ||
trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
||
# Example with Form Recognizer SDKs | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.ai.formrecognizer import DocumentAnalysisClient | ||
|
||
endpoint = "https://<my-custom-subdomain>.cognitiveservices.azure.com/" | ||
credential = AzureKeyCredential("<api_key>") | ||
document_analysis_client = DocumentAnalysisClient(endpoint, credential) | ||
|
||
with open("<path to your document>", "rb") as fd: | ||
document = fd.read() | ||
|
||
with tracer.start_as_current_span(name="DocAnalysis"): | ||
poller = document_analysis_client.begin_analyze_document("prebuilt-layout", document) | ||
result = poller.result() |
57 changes: 57 additions & 0 deletions
57
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_key_cert.py
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,57 @@ | ||
""" | ||
Examples to show usage of the azure-core-tracing-opentelemetry | ||
with the KeyVault Certificate SDK and exporting to Azure monitor backend. | ||
This example traces calls for creating a certificate using the | ||
KeyVault Certificate SDK. The telemetry will be collected automatically | ||
and sent to Application Insights via the AzureMonitorTraceExporter | ||
""" | ||
|
||
import os | ||
|
||
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs | ||
from azure.core.settings import settings | ||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
|
||
settings.tracing_implementation = OpenTelemetrySpan | ||
|
||
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python | ||
# for details | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
trace.set_tracer_provider(TracerProvider()) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# azure monitor trace exporter to send telemetry to appinsights | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
span_processor = BatchSpanProcessor( | ||
AzureMonitorTraceExporter.from_connection_string( | ||
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
) | ||
) | ||
trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
||
# Example with KeyVault Certificate SDKs | ||
from azure.identity import ClientSecretCredential | ||
from azure.keyvault.certificates import CertificateClient, CertificatePolicy | ||
|
||
tenant_id = "<tenant-id>" | ||
client_id = "<client-id>" | ||
client_secret = "<client-secret>" | ||
|
||
credential = ClientSecretCredential( | ||
tenant_id=tenant_id, | ||
client_id=client_id, | ||
client_secret=client_secret | ||
) | ||
|
||
vault_url = "https://my-key-vault.vault.azure.net/" | ||
|
||
certificate_client = CertificateClient(vault_url=vault_url, credential=credential) | ||
|
||
with tracer.start_as_current_span(name="KeyVaultCertificate"): | ||
create_certificate_poller = certificate_client.begin_create_certificate( | ||
certificate_name="cert-name", policy=CertificatePolicy.get_default() | ||
) | ||
print(create_certificate_poller.result()) |
61 changes: 61 additions & 0 deletions
61
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_key_keys.py
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,61 @@ | ||
""" | ||
Examples to show usage of the azure-core-tracing-opentelemetry | ||
with the KeyVault Keys SDK and exporting to Azure monitor backend. | ||
This example traces calls for creating an rsa/ and ec key using the | ||
KeyVault Secrets SDK. The telemetry will be collected automatically | ||
and sent to Application Insights via the AzureMonitorTraceExporter | ||
""" | ||
|
||
import os | ||
|
||
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs | ||
from azure.core.settings import settings | ||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
|
||
settings.tracing_implementation = OpenTelemetrySpan | ||
|
||
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python | ||
# for details | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
trace.set_tracer_provider(TracerProvider()) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# azure monitor trace exporter to send telemetry to appinsights | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
span_processor = BatchSpanProcessor( | ||
AzureMonitorTraceExporter.from_connection_string( | ||
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
) | ||
) | ||
trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
||
# Example with KeyVault Keys SDKs | ||
from azure.identity import ClientSecretCredential | ||
from azure.keyvault.keys import KeyClient | ||
|
||
tenant_id = "<tenant-id>" | ||
client_id = "<client-id>" | ||
client_secret = "<client-secret>" | ||
|
||
credential = ClientSecretCredential( | ||
tenant_id=tenant_id, | ||
client_id=client_id, | ||
client_secret=client_secret | ||
) | ||
|
||
vault_url = "https://my-key-vault.vault.azure.net/" | ||
key_client = KeyClient(vault_url=vault_url, credential=credential) | ||
|
||
with tracer.start_as_current_span(name="KeyVaultSecret"): | ||
# Create an RSA key | ||
rsa_key = key_client.create_rsa_key("rsa-key-name", size=2048) | ||
print(rsa_key.name) | ||
print(rsa_key.key_type) | ||
|
||
# Create an elliptic curve key | ||
ec_key = key_client.create_ec_key("ec-key-name", curve="P-256") | ||
print(ec_key.name) | ||
print(ec_key.key_type) |
57 changes: 57 additions & 0 deletions
57
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_key_secret.py
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,57 @@ | ||
""" | ||
Examples to show usage of the azure-core-tracing-opentelemetry | ||
with the KeyVault Secrets SDK and exporting to Azure monitor backend. | ||
This example traces calls for setting a secret using the | ||
KeyVault Secrets SDK. The telemetry will be collected automatically | ||
and sent to Application Insights via the AzureMonitorTraceExporter | ||
""" | ||
|
||
import os | ||
|
||
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs | ||
from azure.core.settings import settings | ||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
|
||
settings.tracing_implementation = OpenTelemetrySpan | ||
|
||
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python | ||
# for details | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
trace.set_tracer_provider(TracerProvider()) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# azure monitor trace exporter to send telemetry to appinsights | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
span_processor = BatchSpanProcessor( | ||
AzureMonitorTraceExporter.from_connection_string( | ||
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
) | ||
) | ||
trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
||
# Example with KeyVault Secrets SDKs | ||
from azure.identity import ClientSecretCredential | ||
from azure.keyvault.secrets import SecretClient | ||
|
||
tenant_id = "<tenant-id>" | ||
client_id = "<client-id>" | ||
client_secret = "<client-secret>" | ||
|
||
credential = ClientSecretCredential( | ||
tenant_id=tenant_id, | ||
client_id=client_id, | ||
client_secret=client_secret | ||
) | ||
|
||
vault_url = "https://my-key-vault.vault.azure.net/" | ||
|
||
with tracer.start_as_current_span(name="KeyVaultSecret"): | ||
secret_client = SecretClient(vault_url=vault_url, credential=credential) | ||
secret = secret_client.set_secret("secret-name", "secret-value") | ||
|
||
print(secret.name) | ||
print(secret.value) | ||
print(secret.properties.version) |
58 changes: 58 additions & 0 deletions
58
sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_text_analytics.py
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,58 @@ | ||
""" | ||
Examples to show usage of the azure-core-tracing-opentelemetry | ||
with the Text Analytics SDK and exporting to Azure monitor backend. | ||
This example traces calls for extracting | ||
key phrases from input text via the Text Analytics sdk. The telemetry | ||
will be collected automatically and sent to Application Insights | ||
via the AzureMonitorTraceExporter | ||
""" | ||
|
||
import os | ||
|
||
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs | ||
from azure.core.settings import settings | ||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
|
||
settings.tracing_implementation = OpenTelemetrySpan | ||
|
||
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python | ||
# for details | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
trace.set_tracer_provider(TracerProvider()) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# azure monitor trace exporter to send telemetry to appinsights | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
span_processor = BatchSpanProcessor( | ||
AzureMonitorTraceExporter.from_connection_string( | ||
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
) | ||
) | ||
trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
||
# Example with Text Analytics SDKs | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.ai.textanalytics import TextAnalyticsClient | ||
|
||
credential = AzureKeyCredential("<api_key>") | ||
endpoint="https://<resource-name>.cognitiveservices.azure.com/" | ||
|
||
text_analytics_client = TextAnalyticsClient(endpoint, credential) | ||
|
||
documents = [ | ||
"Redmond is a city in King County, Washington, United States, located 15 miles east of Seattle.", | ||
""" | ||
I need to take my cat to the veterinarian. He has been sick recently, and I need to take him | ||
before I travel to South America for the summer. | ||
""", | ||
] | ||
|
||
with tracer.start_as_current_span(name="DocAnalysis"): | ||
response = text_analytics_client.extract_key_phrases(documents, language="en") | ||
result = [doc for doc in response if not doc.is_error] | ||
|
||
for doc in result: | ||
print(doc.key_phrases) |