Skip to content

Commit

Permalink
Add other azure samples (Azure#24360)
Browse files Browse the repository at this point in the history
* chat

* phone

* sms

* keys

* other

* link
  • Loading branch information
lzchen authored May 18, 2022
1 parent 41afdda commit 599aac7
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 80 deletions.

Large diffs are not rendered by default.

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)
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()
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())
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)
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)
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)

0 comments on commit 599aac7

Please sign in to comment.