<jupyter_start><jupyter_text>Enabling App Insights for Services in ProductionWith this notebook, you can learn how to enable App Insights for standard service monitoring, plus, we provide examples for doing custom logging within a scoring files in a model.  What does Application Insights monitor?It monitors request rates, response times, failure rates, etc. For more information visit [App Insights docs.](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-overview) What is different compared to standard production deployment process?If you want to enable generic App Insights for a service run:```pythonaks_service= Webservice(ws, "aks-w-dc2")aks_service.update(enable_app_insights=True)```Where "aks-w-dc2" is your service name. You can also do this from the Azure Portal under your Workspace--> deployments--> Select deployment--> Edit--> Advanced Settings--> Select "Enable AppInsights diagnostics"If you want to log custom traces, you will follow the standard deplyment process for AKS and you will:1. Update scoring file.2. Update aks configuration.3. Build new image and deploy it.   1. Import your dependencies<jupyter_code>from azureml.core import Workspace, Run
from azureml.core.compute import AksCompute, ComputeTarget
from azureml.core.webservice import Webservice, AksWebservice
from azureml.core.image import Image
from azureml.core.model import Model

import azureml.core
print(azureml.core.VERSION)<jupyter_output><empty_output><jupyter_text>2. Set up your configuration and create a workspace<jupyter_code>ws = Workspace.from_config()
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\n')<jupyter_output><empty_output><jupyter_text>3. Register ModelRegister an existing trained model, add descirption and tags.<jupyter_code>#Register the model
from azureml.core.model import Model
model = Model.register(model_path = "sklearn_regression_model.pkl", # this points to a local file
                       model_name = "sklearn_regression_model.pkl", # this is the name the model is registered as
                       tags = {'area': "diabetes", 'type': "regression"},
                       description = "Ridge regression model to predict diabetes",
                       workspace = ws)

print(model.name, model.description, model.version)<jupyter_output><empty_output><jupyter_text>4. *Update your scoring file with custom print statements*Here is an example: a. In your init function add:```pythonprint ("model initialized" + time.strftime("%H:%M:%S"))``` b. In your run function add:```pythonprint ("Prediction created" + time.strftime("%H:%M:%S"))```<jupyter_code>%%writefile score.py
import pickle
import json
import numpy 
from sklearn.externals import joblib
from sklearn.linear_model import Ridge
from azureml.core.model import Model
import time

def init():
    global model
    #Print statement for appinsights custom traces:
    print ("model initialized" + time.strftime("%H:%M:%S"))
    
    # note here "sklearn_regression_model.pkl" is the name of the model registered under the workspace
    # this call should return the path to the model.pkl file on the local disk.
    model_path = Model.get_model_path(model_name = 'sklearn_regression_model.pkl')
    
    # deserialize the model file back into a sklearn model
    model = joblib.load(model_path)
  

# note you can pass in multiple rows for scoring
def run(raw_data):
    try:
        data = json.loads(raw_data)['data']
        data = numpy.array(data)
        result = model.predict(data)
        print ("Prediction created" + time.strftime("%H:%M:%S"))
        # you can return any datatype as long as it is JSON-serializable
        return result.tolist()
    except Exception as e:
        error = str(e)
        print (error + time.strftime("%H:%M:%S"))
        return error<jupyter_output><empty_output><jupyter_text>5. *Create myenv.yml file*<jupyter_code>from azureml.core.conda_dependencies import CondaDependencies 

myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])

with open("myenv.yml","w") as f:
    f.write(myenv.serialize_to_string())<jupyter_output><empty_output><jupyter_text>6. Create your new Image<jupyter_code>from azureml.core.image import ContainerImage

image_config = ContainerImage.image_configuration(execution_script = "score.py",
                                                  runtime = "python",
                                                  conda_file = "myenv.yml",
                                                  description = "Image with ridge regression model",
                                                  tags = {'area': "diabetes", 'type': "regression"}
                                                 )

image = ContainerImage.create(name = "myimage1",
                              # this is the model object
                              models = [model],
                              image_config = image_config,
                              workspace = ws)

image.wait_for_creation(show_output = True)<jupyter_output><empty_output><jupyter_text>Deploy to ACI (Optional)<jupyter_code>from azureml.core.webservice import AciWebservice

aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, 
                                               memory_gb = 1, 
                                               tags = {'area': "diabetes", 'type': "regression"}, 
                                               description = 'Predict diabetes using regression model',
                                               enable_app_insights = True)
from azureml.core.webservice import Webservice

aci_service_name = 'my-aci-service-4'
print(aci_service_name)
aci_service = Webservice.deploy_from_image(deployment_config = aciconfig,
                                           image = image,
                                           name = aci_service_name,
                                           workspace = ws)
aci_service.wait_for_deployment(True)
print(aci_service.state)
%%time
import json

test_sample = json.dumps({'data': [
    [1,28,13,45,54,6,57,8,8,10], 
    [101,9,8,37,6,45,4,3,2,41]
]})
test_sample = bytes(test_sample,encoding='utf8')
if aci_service.state == "Healthy":
    prediction = aci_service.run(input_data=test_sample)
    print(prediction)
else:
    raise ValueError("Service deployment isn't healthy, can't call the service")<jupyter_output><empty_output><jupyter_text>7. Deploy to AKS service  Create AKS compute if you haven't done so.<jupyter_code># Use the default configuration (can also provide parameters to customize)
prov_config = AksCompute.provisioning_configuration()

aks_name = 'my-aks-test3' 
# Create the cluster
aks_target = ComputeTarget.create(workspace = ws, 
                                  name = aks_name, 
                                  provisioning_configuration = prov_config)
%%time
aks_target.wait_for_completion(show_output = True)
print(aks_target.provisioning_state)
print(aks_target.provisioning_errors)<jupyter_output><empty_output><jupyter_text>If you already have a cluster you can attach the service to it: ```python %%timeresource_id = '/subscriptions//resourcegroups//providers/Microsoft.ContainerService/managedClusters/'create_name= 'myaks4'attach_config = AksCompute.attach_configuration(resource_id=resource_id)aks_target = ComputeTarget.attach(workspace = ws,                                   name = create_name,                                   attach_configuration=attach_config) Wait for the operation to completeaks_target.wait_for_provisioning(True)```  a. *Activate App Insights through updating AKS Webservice configuration*In order to enable App Insights in your service you will need to update your AKS configuration file:<jupyter_code>#Set the web service configuration
aks_config = AksWebservice.deploy_configuration(enable_app_insights=True)<jupyter_output><empty_output><jupyter_text>b. Deploy your service<jupyter_code>if aks_target.provisioning_state== "Succeeded": 
    aks_service_name ='aks-w-dc5'
    aks_service = Webservice.deploy_from_image(workspace = ws, 
                                               name = aks_service_name,
                                               image = image,
                                               deployment_config = aks_config,
                                               deployment_target = aks_target
                                               )
    aks_service.wait_for_deployment(show_output = True)
    print(aks_service.state)
else:
    raise ValueError("AKS provisioning failed.")<jupyter_output><empty_output><jupyter_text>8. Test your service<jupyter_code>%%time
import json

test_sample = json.dumps({'data': [
    [1,28,13,45,54,6,57,8,8,10], 
    [101,9,8,37,6,45,4,3,2,41]
]})
test_sample = bytes(test_sample,encoding='utf8')

if aks_service.state == "Healthy":
    prediction = aks_service.run(input_data=test_sample)
    print(prediction)
else:
     raise ValueError("Service deployment isn't healthy, can't call the service")<jupyter_output><empty_output><jupyter_text>9. See your service telemetry in App Insights1. Go to the [Azure Portal](https://portal.azure.com/)2. All resources--> Select the subscription/resource group where you created your Workspace--> Select the App Insights type3. Click on the AppInsights resource. You'll see a highlevel dashboard with information on Requests, Server response time and availability.4. Click on the top banner "Analytics"5. In the "Schema" section select "traces" and run your query.6. Voila! All your custom traces should be there.  Disable App Insights<jupyter_code>aks_service.update(enable_app_insights=False)<jupyter_output><empty_output><jupyter_text>Clean up<jupyter_code>%%time
aks_service.delete()
aci_service.delete()
image.delete()
model.delete()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup